pyLodStorage API Documentation
docstring_parser
Created on 2024-01-21
@author: wf
DocstringParser
A Python docstring parser.
Source code in lodstorage/docstring_parser.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
parse(docstring)
Parse the given docstring.
Source code in lodstorage/docstring_parser.py
53 54 55 56 57 58 59 60 61 62 63 | |
exception_handler
Created on 2025-12-01
@author: wf
ExceptionHandler
handle exceptions
Source code in lodstorage/exception_handler.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
handle(msg, ex, debug=False)
classmethod
Centralized exception logging (non-fatal).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
str
|
context message |
required |
ex
|
Exception
|
the exception caught |
required |
debug
|
bool
|
if True, print full traceback |
False
|
Source code in lodstorage/exception_handler.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
linkml
Created on 2024-01-28
@author: wf
Class
Represents a class in the LinkML schema.
Source code in lodstorage/linkml.py
26 27 28 29 30 31 32 33 | |
PythonTypes
python type handling
Source code in lodstorage/linkml.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
get_linkml_range(ptype)
classmethod
Determines the LinkML range for a given Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
Type
|
The Python type for which the LinkML range is required. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The corresponding LinkML range as a string. Defaults to "string" if the type is not found. |
Source code in lodstorage/linkml.py
113 114 115 116 117 118 119 120 121 122 123 124 | |
get_rdf_datatype(ptype)
classmethod
Determines the RDF (XSD) datatype for a given Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
Type
|
The Python type for which the RDF (XSD) datatype is required. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
XSD |
Optional[XSD]
|
The corresponding RDF (XSD) datatype. Returns None if the type is not found. |
Source code in lodstorage/linkml.py
126 127 128 129 130 131 132 133 134 135 136 137 | |
Schema
Represents the entire LinkML schema.
Source code in lodstorage/linkml.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
Slot
Represents a slot in the LinkML schema, equivalent to a field or property.
Source code in lodstorage/linkml.py
14 15 16 17 18 19 20 21 22 23 | |
linkml_gen
Created on 2024-01-21
@author: wf
LinkMLGen
Class for generating LinkML YAML schema from Python data models using dataclasses.
Source code in lodstorage/linkml_gen.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
__init__(schema)
Initialize the LinkMLGen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
The LinkML schema to be generated. |
required |
Source code in lodstorage/linkml_gen.py
20 21 22 23 24 25 26 27 | |
gen_schema_from_instance(data_model_instance)
Generate a LinkML YAML schema from a Python data model using dataclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_model_instance
|
An instance of the Python data model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Schema |
Schema
|
The LinkML schema generated from the data model. |
Source code in lodstorage/linkml_gen.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
lod
Created on 2021-01-31
@author: wf
LOD
Bases: object
list of Dict aka Table
Source code in lodstorage/lod.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
__init__(name)
Constructor
Source code in lodstorage/lod.py
13 14 15 16 17 18 | |
addLookup(lookup, duplicates, record, value, withDuplicates)
staticmethod
add a single lookup result
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lookup(dict)
|
the lookup map |
required | |
duplicates(list)
|
the list of duplicates |
required | |
record(dict)
|
the current record |
required | |
value(object)
|
the current value to lookup |
required | |
withDuplicates(bool)
|
if True duplicates should be allowed and lists returned if False a separate duplicates |
required |
Source code in lodstorage/lod.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
filterFields(lod, fields, reverse=False)
staticmethod
filter the given LoD with the given list of fields by either limiting the LoD to the fields or removing the fields contained in the list depending on the state of the reverse parameter
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lod(list)
|
list of dicts from which the fields should be excluded |
required | |
fields(list)
|
list of fields that should be excluded from the lod |
required | |
reverse(bool)
|
If True limit dict to the list of given fields. Otherwise exclude the fields from the dict. |
required |
Returns:
| Type | Description |
|---|---|
|
LoD |
Source code in lodstorage/lod.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
getFields(listOfDicts, sampleCount=None)
staticmethod
Extract field names from a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
listOfDicts
|
List of dictionaries to extract field names from |
required | |
sampleCount
|
int
|
Number of dictionaries to sample. If None, all are sampled |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
Field names found in the dictionaries, or None if listOfDicts is None |
Source code in lodstorage/lod.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
getLookup(lod, attrName, withDuplicates=False)
staticmethod
create a lookup dictionary by the given attribute name for the given list of dicts
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lod(list)
|
the list of dicts to get the lookup dictionary for |
required | |
attrName(str)
|
the attribute to lookup |
required | |
withDuplicates(bool)
|
whether to retain single values or lists |
required |
Return
a dictionary for lookup
Source code in lodstorage/lod.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
handleListTypes(lod, doFilter=False, separator=',')
classmethod
handle list types in the given list of dicts
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
this class |
required | |
lod(list)
|
a list of dicts |
required | |
doFilter(bool)
|
True if records containing lists value items should be filtered |
required | |
separator(str)
|
the separator to use when converting lists |
required |
Source code in lodstorage/lod.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
intersect(listOfDict1, listOfDict2, key=None)
staticmethod
get the intersection of the two lists of Dicts by the given key
Source code in lodstorage/lod.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
setNone(record, fields)
staticmethod
make sure the given fields in the given record are set to none Args: record(dict): the record to work on fields(list): the list of fields to set to None
Source code in lodstorage/lod.py
58 59 60 61 62 63 64 65 66 67 68 | |
setNone4List(listOfDicts, fields)
staticmethod
set the given fields to None for the records in the given listOfDicts if they are not set Args: listOfDicts(list): the list of records to work on fields(list): the list of fields to set to None
Source code in lodstorage/lod.py
46 47 48 49 50 51 52 53 54 55 56 | |
sortKey(d, key=None)
staticmethod
get the sort key for the given dict d with the given key
Source code in lodstorage/lod.py
74 75 76 77 78 79 80 81 | |
lod2xml
Created on 2022-06-20
see https://github.com/tyleradams/json-toolkit https://stackoverflow.com/questions/36021526/converting-an-array-dict-to-xml-in-python
@author: tyleradams @author: wf
Lod2Xml
convert a list of dicts to XML
Source code in lodstorage/lod2xml.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
__init__(lod, root='root', node_name=lambda x: 'node')
construct me with the given list of dicts
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lod
|
list
|
the list of dicts to convert to XML |
required |
root
|
str
|
the name of the root nod |
'root'
|
item_name
|
func
|
the function to use to calculate node names |
required |
Source code in lodstorage/lod2xml.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
asXml(pretty=True)
convert result to XML
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pretty
|
bool
|
if True pretty print the result |
True
|
Source code in lodstorage/lod2xml.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
lod_csv
Created 2021
@author: wf
CSV
helper for converting data in csv format to list of dicts (LoD) and vice versa
Source code in lodstorage/lod_csv.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
__new__(dialect='excel', quoting=csv.QUOTE_NONNUMERIC)
constructor to set dialect and quoting defaults
Source code in lodstorage/lod_csv.py
21 22 23 24 25 26 27 28 29 | |
fixTypes(lod)
fixes the types of the given LoD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lod(List[Dict[str,
|
Any]]
|
List of dictionaries to fix types for |
required |
Source code in lodstorage/lod_csv.py
173 174 175 176 177 178 179 180 181 182 183 | |
fromCSV(csvString, fields=None, dialect=None, quoting=None, **kwargs)
convert given csv string to list of dicts (LOD)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csvString(str)
|
csv string that should be converted to LOD |
required | |
fields(List[str])
|
Names of the headers that should be used. If None it is assumed that the header is given. |
required | |
dialect(str)
|
CSV dialect to use |
required | |
quoting(int)
|
CSV quoting behavior |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: list of dicts (LoD) containing the content of the given csv string |
Source code in lodstorage/lod_csv.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
get_instance(dialect='excel', quoting=csv.QUOTE_NONNUMERIC)
classmethod
Get singleton instance of CSV class
Source code in lodstorage/lod_csv.py
31 32 33 34 | |
readFile(filename)
Reads the given filename and returns it as string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename(str)
|
Name of the file that should be returned as string |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Content of the file as string |
Source code in lodstorage/lod_csv.py
148 149 150 151 152 153 154 155 156 157 158 159 160 | |
restoreFromCSVFile(filePath, headerNames=None, withPostfix=False)
restore LOD from given csv file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filePath(str)
|
file name |
required | |
headerNames(List[str])
|
Names of the headers that should be used. If None it is assumed that the header is given. |
required | |
withPostfix(bool)
|
If False the file type is appended to given filePath. Otherwise file type MUST be given with filePath. |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: list of dicts (LoD) containing the content of the given csv file |
Source code in lodstorage/lod_csv.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
storeToCSVFile(lod, filePath, withPostfix=False)
converts the given lod to CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lod(List[Dict[str,
|
Any]]
|
lod that should be converted to csv file |
required |
filePath(str)
|
file name the csv should be stored to |
required | |
withPostfix(bool)
|
If False the file type is appended to given filePath. Otherwise file type MUST be given with filePath. |
required |
Source code in lodstorage/lod_csv.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
toCSV(lod, includeFields=None, excludeFields=None, dialect=None, quoting=None, **kwargs)
converts the given lod to CSV string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lod(List[Dict[str,
|
Any]]
|
lod that should be converted to csv string |
required |
includeFields(List[str])
|
list of fields that should be included in the csv |
required | |
excludeFields(List[str])
|
list of fields that should be excluded from the csv |
required | |
dialect(str)
|
CSV dialect to use |
required | |
quoting(int)
|
CSV quoting behavior |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
csv string of the given lod |
Source code in lodstorage/lod_csv.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
writeFile(content, filename)
Write the given str to the given filename
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content(str)
|
string that should be written into the file |
required | |
filename(str)
|
Name of the file the given str should be written to |
required |
Source code in lodstorage/lod_csv.py
162 163 164 165 166 167 168 169 170 171 | |
mwTable
Created on 2020-08-21
@author: wf
MediaWikiTable
Bases: object
helper for https://www.mediawiki.org/wiki/Help:Tables
Source code in lodstorage/mwTable.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
__init__(wikiTable=True, colFormats=None, sortable=True, withNewLines=False)
Constructor
Source code in lodstorage/mwTable.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
addHeader(record)
add the given record as a "sample" header
Source code in lodstorage/mwTable.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
asWikiMarkup()
convert me to MediaWiki markup
Returns:
| Name | Type | Description |
|---|---|---|
string |
the MediWiki Markup for this table |
Source code in lodstorage/mwTable.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
mysql
mysql.py:
MySQL and MariaDB support
MySqlQuery
A class to manage and execute mySQL queries with optional debugging.
Attributes:
| Name | Type | Description |
|---|---|---|
endpoint_info |
Endpoint
|
endpoint configuration. |
debug |
bool
|
Flag to enable debugging. |
Source code in lodstorage/mysql.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
__init__(endpoint, debug=False)
Initializes the Query class with command-line arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
Endpoint
|
endpoint configuration. |
required |
debug
|
bool
|
Flag to enable debugging. |
False
|
Source code in lodstorage/mysql.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
decode_record(record)
Converts binary values to UTF-8 strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Raw database row data |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Data with binary values decoded to strings |
Source code in lodstorage/mysql.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
execute_sql_query(query)
Executes an SQL query using the provided connection parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SQL query to execute. |
required |
connection_params
|
dict
|
Database connection parameters. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
List[Dict[str, Any]]
|
A list of dictionaries representing the query results. |
Source code in lodstorage/mysql.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
query_generator(query)
Generator for fetching records one by one from a SQL query.
Source code in lodstorage/mysql.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
params
Created on 2024-05-06
@author: wf
Param
a parameter (input or output) for a query
Source code in lodstorage/params.py
15 16 17 18 19 20 21 22 23 24 25 | |
Params
parameter handling
Source code in lodstorage/params.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
__init__(query, illegal_chars='"[;<>&|]"\'', with_audit=True)
constructor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
the query to analyze for parameters |
required |
illegal_chars
|
str
|
chars that may not be in the values |
'"[;<>&|]"\''
|
with_audit
|
bool
|
if True audit parameters |
True
|
Source code in lodstorage/params.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
apply_parameters()
Replace Jinja templates in the query with corresponding parameter values.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The query with Jinja templates replaced by parameter values. |
Source code in lodstorage/params.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
apply_parameters_with_check(param_dict=None)
Apply parameters to the query string with parameter checking.
This method checks if the query requires parameters. If parameters are required but not provided, it raises an exception with a descriptive message. If parameters are provided, it applies them to the query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_dict
|
dict
|
A dictionary of parameter names and values. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The query string with parameters applied, if applicable. |
Raises:
| Type | Description |
|---|---|
Exception
|
If parameters are required but not provided. |
Source code in lodstorage/params.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
audit()
Audit the usage of parameters in the query.
Raises:
| Type | Description |
|---|---|
ValueError
|
If potentially malicious values are detected in the parameter dictionary. |
Source code in lodstorage/params.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
set(params_dict)
set my params
Source code in lodstorage/params.py
52 53 54 55 56 | |
StoreDictKeyPair
Bases: Action
Custom argparse action to store key-value pairs as a dictionary.
This class implements an argparse action to parse and store command-line arguments in the form of key-value pairs. The pairs should be separated by a comma and each key-value pair should be separated by an equals sign.
Example
--option key1=value1,key2=value2,key3=value3
Reference
https://stackoverflow.com/a/42355279/1497139
Source code in lodstorage/params.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
__call__(_parser, namespace, values, _option_string=None)
Parse key-value pairs and store them as a dictionary in the namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
ArgumentParser
|
The argument parser object. |
required |
namespace
|
Namespace
|
The namespace to store the parsed values. |
required |
values
|
str
|
The string containing key-value pairs separated by commas. |
required |
option_string
|
Optional[str]
|
The option string, if provided. |
required |
Source code in lodstorage/params.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
plot
Created on 2020-07-05
@author: wf
Plot
Bases: object
create Plot based on counters see https://stackoverflow.com/questions/19198920/using-counter-in-python-to-build-histogram
Source code in lodstorage/plot.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
__init__(valueList, title, xlabel=None, ylabel=None, gformat='.png', fontsize=12, plotdir=None, debug=False)
Constructor
Source code in lodstorage/plot.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
barchart(mode='show')
barchart based histogram for the given counter
Source code in lodstorage/plot.py
66 67 68 69 70 71 72 73 74 75 | |
hist(mode='show')
create histogram for the given counter
Source code in lodstorage/plot.py
83 84 85 86 87 88 89 90 91 | |
showMe(mode='show', close=True)
show me in the given mode
Source code in lodstorage/plot.py
57 58 59 60 61 62 63 64 | |
titleMe()
set my title and labels
Source code in lodstorage/plot.py
49 50 51 52 53 54 55 | |
prefix_config
Created on 2025-06-04
@author: wf
PrefixConfig
dataclass
Configuration for SPARQL prefixes
Source code in lodstorage/prefix_config.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
__post_init__()
Set default values after initialization
Source code in lodstorage/prefix_config.py
30 31 32 33 34 35 | |
as_text()
Get prefixes as formatted text block.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Newline-separated prefix declarations |
Source code in lodstorage/prefix_config.py
37 38 39 40 41 42 43 44 45 | |
PrefixConfigs
Collection of prefix configurations loaded from YAML.
Source code in lodstorage/prefix_config.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
__post_init__()
initialize all prefixes and test prefix prefix for all prefix configs
Source code in lodstorage/prefix_config.py
85 86 87 88 89 90 91 92 93 94 | |
get_instance()
classmethod
Get singleton PrefixConfigs (loads prefixes.yaml via YamlPath if needed).
Source code in lodstorage/prefix_config.py
59 60 61 62 63 64 | |
get_prefix_declarations(prefixes=None)
Convert prefixes to PREFIX declarations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefixes
|
Dict[str, str]
|
Dictionary of prefix mappings, defaults to all_prefixes |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Newline-separated PREFIX declarations |
Source code in lodstorage/prefix_config.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
get_selected_declarations(prefix_set)
Get PREFIX declarations for selected prefix sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix_set
|
List[str]
|
List of prefix set names to include |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Combined PREFIX declarations |
Source code in lodstorage/prefix_config.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
of_yaml(yaml_path=None)
classmethod
Load from YAML (uses prefixes.yaml via YamlPath if yaml_path=None).
Source code in lodstorage/prefix_config.py
73 74 75 76 77 78 79 80 81 82 83 | |
preload(prefixes_path)
classmethod
Preload singleton with specific prefixes path.
Source code in lodstorage/prefix_config.py
66 67 68 69 70 71 | |
prefixes
Created on 2024-03-02
@author: wf
Prefixes
Handles the generation of standard SPARQL prefix declarations for queries. This utility class simplifies the inclusion of common prefixes used in SPARQL queries by providing a method to generate the necessary PREFIX lines based on a list of prefix keys.
The class supports a wide range of prefixes relevant to Wikidata and general RDF/SPARQL usage, including RDF, RDFS, Wikibase, Schema.org, and more. It aims to reduce redundancy and improve clarity in SPARQL query construction by centralizing prefix management.
Methods:
| Name | Description |
|---|---|
getPrefixes |
Generates SPARQL PREFIX lines for a given list of prefix keys. |
Source code in lodstorage/prefixes.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
extract_prefixes(sparql_query)
classmethod
Extract only the explicitly declared prefixes from a SPARQL query string. Simple regex-based extraction that finds PREFIX declarations in the query text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparql_query
|
str
|
The SPARQL query containing PREFIX declarations |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Dictionary mapping prefix names to their URI strings |
Source code in lodstorage/prefixes.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
getPrefixes(prefixes=['rdf', 'rdfs', 'schema', 'wd', 'wdt', 'wikibase', 'xsd'])
classmethod
Generates SPARQL PREFIX lines for a given list of prefix keys.
This method looks up URIs for the specified prefixes from a predefined map and constructs
PREFIX lines suitable for inclusion at the beginning of a SPARQL query. It allows for easy
and flexible specification of the prefixes needed for a particular query.
Args:
prefixes (list of str): A list of prefix keys for which PREFIX lines should be generated.
Defaults to a common set of prefixes used in Wikidata queries.
Returns:
str: A string containing the SPARQL PREFIX lines for the specified prefixes, each ending
with a newline character. If a prefix key is not recognized, it is ignored.
Example:
>>> Prefixes.getPrefixes(["wd", "wdt"])
'PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: http://www.wikidata.org/prop/direct/ '
Source code in lodstorage/prefixes.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
merge_prefix_dict(query, prefix_dict)
classmethod
Merge prefixes from dict into SPARQL query by prepending missing prefix declarations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query |
required |
prefix_dict
|
dict
|
Dictionary of prefixes to merge |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
SPARQL query with missing prefixes prepended |
Source code in lodstorage/prefixes.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
merge_prefixes(query, prefixes)
classmethod
Merge prefixes from string into SPARQL query by prepending missing prefix declarations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query |
required |
prefixes
|
str
|
String containing PREFIX declarations |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
SPARQL query with missing prefixes prepended |
Source code in lodstorage/prefixes.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
profiler
Created on 2022-11-18
@author: wf
Profiler
simple profiler
Source code in lodstorage/profiler.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
__init__(msg, profile=True, with_start=True)
construct me with the given msg and profile active flag
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg(str)
|
the message to show if profiling is active |
required | |
profile(bool)
|
True if messages should be shown |
required |
Source code in lodstorage/profiler.py
15 16 17 18 19 20 21 22 23 24 25 26 | |
start()
start profiling
Source code in lodstorage/profiler.py
28 29 30 31 32 33 34 | |
time(extraMsg='')
time the action and print if profile is active
Source code in lodstorage/profiler.py
36 37 38 39 40 41 42 43 | |
query
Created on 2020-08-22
@author: wf
Endpoint
a query endpoint
Source code in lodstorage/query.py
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | |
__str__()
Returns:
| Name | Type | Description |
|---|---|---|
str |
a string representation of this Endpoint |
Source code in lodstorage/query.py
784 785 786 787 788 789 790 | |
getDefault()
classmethod
get the default endpoint cofiguration
Source code in lodstorage/query.py
753 754 755 756 757 758 759 760 | |
get_prefixes(prefix_configs=None)
Get prefix declarations for this endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix_configs
|
Optional[PrefixConfigs]
|
PrefixConfigs instance to resolve prefix_sets |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
PREFIX declarations |
Source code in lodstorage/query.py
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 | |
EndpointManager
Bases: object
manages a set of SPARQL endpoints
Source code in lodstorage/query.py
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 | |
getEndpointNames(endpointPath=None, lang=None)
staticmethod
Returns a list of all available endpoint names Args: endpointPath(str): the path to the yaml file with the endpoint configurations lang(str): if lang is given filter by the given language
Source code in lodstorage/query.py
832 833 834 835 836 837 838 839 840 841 842 843 | |
getEndpoints(endpointPath=None, lang=None, with_default=True)
classmethod
get the endpoints for the given endpointPath
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpointPath(str)
|
the path to the yaml file with the endpoint configurations |
required | |
lang(str)
|
if lang is given filter by the given language |
required | |
with_default(bool)
|
if True include the default endpoints |
required |
Source code in lodstorage/query.py
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 | |
ofYaml(yaml_path)
classmethod
Load prefix configurations from YAML file.
Source code in lodstorage/query.py
801 802 803 804 805 | |
Format
Bases: Enum
the supported formats for the results to be delivered
Source code in lodstorage/query.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
Query
A Query e.g. for SPARQL
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
the name/label of the query |
query |
str
|
the native Query text e.g. in SPARQL |
lang |
str
|
the language of the query e.g. SPARQL |
sparql(str) |
str
|
SPARQL querycode |
sql(str) |
str
|
SQL query code |
ask(atr) |
str
|
SMW ASK query code |
endpoint |
str
|
the endpoint url to use |
database |
str
|
the type of database e.g. "blazegraph" |
title |
str
|
the header/title of the query |
description |
str
|
the description of the query |
limit |
int
|
the limit of the query |
prefixes |
list
|
list of prefixes to be resolved |
tryItUrl |
str
|
the url of a "tryit" webpage |
short_urls |
dict
|
dictionary of short urls keyed by endpoint name |
formats |
list
|
key,value pairs of ValueFormatters to be applied |
debug |
bool
|
true if debug mode should be switched on |
Source code in lodstorage/query.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
add_endpoint_prefixes(endpoint, prefix_configs)
Add endpoint-specific PREFIX declarations to this query (via prefix_sets or legacy prefixes).
Merges (deduplicates by prefix name) endpoint prefixes into self.query using Prefixes.merge_prefixes(). Updates self.prefixes to full unique PREFIX lines list. Safe/idempotent (no-op if prefixes_str empty).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
Endpoint
|
Endpoint config with prefix_sets or legacy prefixes. |
required |
prefix_configs
|
PrefixConfigs
|
Loaded prefix configurations resolver. |
required |
Source code in lodstorage/query.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
apply_default_params()
apply my default parameters
Source code in lodstorage/query.py
333 334 335 336 337 338 | |
asWikiMarkup(listOfDicts)
convert the given listOfDicts result to MediaWiki markup
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
listOfDicts(list)
|
the list of Dicts to convert to MediaWiki markup |
required |
Returns:
| Name | Type | Description |
|---|---|---|
string |
the markup |
Source code in lodstorage/query.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
asWikiSourceMarkup()
convert me to Mediawiki markup for syntax highlighting using the "source" tag
Returns:
| Name | Type | Description |
|---|---|---|
string |
the Markup |
Source code in lodstorage/query.py
485 486 487 488 489 490 491 492 493 494 | |
documentQueryResult(qlod, limit=None, tablefmt='mediawiki', tryItUrl=None, withSourceCode=True, **kwArgs)
document the given query results - note that a copy of the whole list is going to be created for being able to format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qlod
|
list
|
the list of dicts result |
required |
limit(int)
|
the maximum number of records to display in result tabulate |
required | |
tablefmt(str)
|
the table format to use |
required | |
tryItUrl
|
str
|
the "try it!" url to show |
None
|
withSourceCode(bool)
|
if True document the source code |
required |
Return
str: the documentation tabular text for the given parameters
Source code in lodstorage/query.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
formatWithValueFormatters(lod, tablefmt)
format the given list of Dicts with the ValueFormatters
Source code in lodstorage/query.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
getLink(url, title, tablefmt)
convert the given url and title to a link for the given tablefmt
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url(str)
|
the url to convert |
required | |
title(str)
|
the title to show |
required | |
tablefmt(str)
|
the table format to use |
required |
Source code in lodstorage/query.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |
getTryItUrl(baseurl, database='blazegraph')
return the "try it!" url for the given baseurl
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
baseurl(str)
|
the baseurl to used |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
the "try it!" url for the given query |
Source code in lodstorage/query.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |
preFormatWithCallBacks(lod, tablefmt)
run the configured call backs to pre-format the given list of dicts for the given tableformat
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lod(list)
|
the list of dicts to handle |
required | |
tablefmt(str)
|
the table format (according to tabulate) to apply |
required |
Source code in lodstorage/query.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
prefixToLink(lod, prefix, tablefmt)
convert url prefixes to link according to the given table format TODO - refactor as preFormat callback
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lod(list)
|
the list of dicts to convert |
required | |
prefix(str)
|
the prefix to strip |
required | |
tablefmt(str)
|
the tabulate tableformat to use |
required |
Source code in lodstorage/query.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
set_default_params(params_dict)
set the default parameters for the given params_dict
Source code in lodstorage/query.py
325 326 327 328 329 330 331 | |
QueryManager
Bases: object
manages pre packaged Queries
Source code in lodstorage/query.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | |
__init__(lang=None, debug=False, queriesPath=None, with_default=True)
Constructor Args: lang(str): the language to use for the queries sql or sparql queriesPath(str): the path of the yaml file to load queries from debug(bool): True if debug information should be shown with_default(bool): if True also load the default yaml file
Source code in lodstorage/query.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
getQueries(queriesPath=None, with_default=True)
get the queries for the given queries Path
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queriesPath(str)
|
the path of the yaml file to load queries from |
required | |
with_default(bool)
|
if True also load the default yaml file |
required |
Source code in lodstorage/query.py
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | |
QueryResultDocumentation
documentation of a query result
Source code in lodstorage/query.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
__init__(query, title, tablefmt, tryItMarkup, sourceCodeHeader, sourceCode, resultHeader, result)
constructor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query(Query)
|
the query to be documented |
required | |
title(str)
|
the title markup |
required | |
tablefmt(str)
|
the tableformat that has been used |
required | |
tryItMarkup
|
str
|
the "try it!" markup to show |
required |
sourceCodeHeader(str)
|
the header title to use for the sourceCode |
required | |
sourceCode(str)
|
the sourceCode |
required | |
resultCodeHeader(str)
|
the header title to use for the result |
required | |
result(str)
|
the result header |
required |
Source code in lodstorage/query.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
__str__()
simple string representation
Source code in lodstorage/query.py
243 244 245 246 247 | |
asText()
return my text representation
Returns:
| Name | Type | Description |
|---|---|---|
str |
description, sourceCodeHeader, sourceCode, tryIt link and result table |
Source code in lodstorage/query.py
249 250 251 252 253 254 255 256 257 258 259 260 | |
uniCode2Latex(text, withConvert=False)
staticmethod
converts unicode text to latex and fixes UTF-8 chars for latex in a certain range: ₀:$_0$ ... ₉:$_9$
see https://github.com/phfaist/pylatexenc/issues/72
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text(str)
|
the string to fix |
required | |
withConvert(bool)
|
if unicode to latex libary conversion should be used |
required |
Return
str: latex presentation of UTF-8 char
Source code in lodstorage/query.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
QuerySyntaxHighlight
Syntax highlighting for queries with pygments
Source code in lodstorage/query.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
__init__(query, highlightFormat='html')
construct me for the given query and highlightFormat
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query(Query)
|
the query to do the syntax highlighting for |
required | |
highlightFormat(str)
|
the highlight format to be used |
required |
Source code in lodstorage/query.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
highlight()
Returns:
| Name | Type | Description |
|---|---|---|
str |
the result of the syntax highlighting with pygments |
Source code in lodstorage/query.py
167 168 169 170 171 172 173 | |
ValueFormatter
a value Formatter
Source code in lodstorage/query.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
applyFormat(record, key, resultFormat)
legacy delegate
Source code in lodstorage/query.py
101 102 103 104 105 | |
apply_format(record, key, resultFormat)
apply the given format to the given record
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record(dict)
|
the record to handle |
required | |
key(str)
|
the property key |
required | |
resultFormat(str)
|
the resultFormat Style to apply |
required |
Source code in lodstorage/query.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
ValueFormatters
manages a set of ValueFormatters
Source code in lodstorage/query.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
get_instance()
classmethod
Get the singleton instance.
Source code in lodstorage/query.py
119 120 121 122 123 124 | |
of_yaml(yaml_path=None)
classmethod
Load ValueFormatters from YAML file.
Source code in lodstorage/query.py
132 133 134 135 136 137 138 139 140 141 142 143 | |
preload(formats_path)
classmethod
Preload singleton with specific formats path.
Source code in lodstorage/query.py
126 127 128 129 130 | |
query_cmd
Created on 2024-08-21
@author: wf
QueryCmd
command line support queries
Source code in lodstorage/query_cmd.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
__init__(args, with_default_queries=True)
command line args
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
the command line arguments |
required |
with_default_queries
|
bool
|
should default queries be made available/listed? |
True
|
Source code in lodstorage/query_cmd.py
29 30 31 32 33 34 35 36 37 38 39 | |
format_output(qlod)
Format and print the query results.
This method formats the query results based on the specified output format (e.g., CSV, JSON, XML) and prints them to the console.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qlod
|
List[Dict[str, Any]]
|
A list of dictionaries containing the query results. Each dictionary represents a row of the query result, with column names as keys and the corresponding values. |
required |
Source code in lodstorage/query_cmd.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
handle_args()
handle the command line arguments
Source code in lodstorage/query_cmd.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
querymain
Created on 2022-02-13
@author: wf
QueryMain
Bases: QueryCmd
Commandline handler
Source code in lodstorage/querymain.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
__init__(args)
command line args
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args(list)
|
the command line arguments |
required |
Source code in lodstorage/querymain.py
38 39 40 41 42 43 44 45 46 47 48 49 50 | |
rawQuery(endpointConf, query, resultFormat, mimeType, content_type='application/sparql-query', timeout=10.0, lenient=True)
Returns raw result of the endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpointConf
|
EndPoint |
required | |
query
|
str
|
query |
required |
resultFormat
|
str
|
format of the result |
required |
mimeType
|
str
|
mimeType |
required |
content_type
|
str
|
content type of the request |
'application/sparql-query'
|
timeout
|
float
|
timeout in seconds |
10.0
|
lenient
|
bool
|
if True do not raise errors but just log |
True
|
Returns:
| Type | Description |
|---|---|
|
raw result of the query |
Source code in lodstorage/querymain.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
main(argv=None, lang=None)
main program.
commandline access to List of Dicts / Linked Open Data Queries
Source code in lodstorage/querymain.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
mainSPARQL(argv=None)
commandline for SPARQL queries
Source code in lodstorage/querymain.py
193 194 195 196 197 | |
mainSQL(argv=None)
commandline for SQL queries
Source code in lodstorage/querymain.py
186 187 188 189 190 | |
rate_limiter
Created on 2024-08-24
@author: wf
RateLimiter
Wrap the @limits decorator in a new decorator
Source code in lodstorage/rate_limiter.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
rdf
Created on 2024-01-27
@author: wf, using ChatGPT-4 prompting
RDFDumper
A class to convert instances of data models (based on a LinkML schema) into an RDF graph.
Source code in lodstorage/rdf.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
__init__(schema, instance)
Initialize the RDFDumper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
The LinkML schema defining the structure of the data models. |
required |
instance
|
object
|
The instance of the data model to be converted into RDF. |
required |
Source code in lodstorage/rdf.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
convert_to_literal(value, slot_obj)
Converts a value to an RDFLib Literal with appropriate datatype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
The value to be converted. |
required | |
slot_obj
|
The slot object containing information about the field. |
required |
Returns:
| Type | Description |
|---|---|
|
An RDFLib Literal with the value and appropriate datatype. |
Source code in lodstorage/rdf.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
convert_to_rdf()
Converts the provided instance into RDF triples based on the LinkML schema.
Source code in lodstorage/rdf.py
37 38 39 40 41 42 43 44 | |
get_instance_uri(instance_data)
Generates a URI for an instance. If the instance has an 'identifier' property, it uses that as part of the URI. Otherwise, it generates or retrieves a unique URI.
Source code in lodstorage/rdf.py
127 128 129 130 131 132 133 134 135 136 137 138 139 | |
serialize(rdf_format='turtle')
Serializes the RDF graph into a string representation in the specified format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
The serialization format (e.g., 'turtle', 'xml', 'json-ld'). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The serialized RDF graph. |
Source code in lodstorage/rdf.py
46 47 48 49 50 51 52 53 54 55 56 | |
value_iterator(value)
Iterates over values in a mapping or iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to iterate over. It can be a mapping, iterable, or a single value. |
required |
Yields:
| Type | Description |
|---|---|
|
Tuples of (key, value) from the input value. For single values, key is None. |
Source code in lodstorage/rdf.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
rdf_format
Created on 2025-06-01
@author: wf
RdfFormat
Bases: Enum
RDF serialization formats with MIME types, file extensions, and SPARQLWrapper constants.
Source code in lodstorage/rdf_format.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
by_label(label)
classmethod
Get format by label
Source code in lodstorage/rdf_format.py
28 29 30 31 32 33 34 | |
sample2
Created on 2024-01-21
@author: wf
Countries
Represents a collection of country instances.
Attributes:
| Name | Type | Description |
|---|---|---|
countries |
List[Country]
|
A list of Country instances. |
Source code in lodstorage/sample2.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
get_countries_erdem()
classmethod
get Erdem Ozkol's country list
Source code in lodstorage/sample2.py
189 190 191 192 193 194 195 196 197 198 199 | |
get_samples()
classmethod
Returns a dictionary of named samples for 'specification by example' style requirements management.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Countries]
|
A dictionary with keys as sample names |
dict[str, Countries]
|
and values as |
Source code in lodstorage/sample2.py
201 202 203 204 205 206 207 208 209 210 211 212 213 | |
Country
Represents a country with its details.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the country. |
country_code |
str
|
The country code. |
capital |
Optional[str]
|
The capital city of the country. |
timezones |
List[str]
|
List of timezones in the country. |
latlng |
List[float]
|
Latitude and longitude of the country. |
Source code in lodstorage/sample2.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
Royal
Represents a member of the royal family, with various personal details.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The full name of the royal member. |
wikidata_id |
str
|
The Wikidata identifier associated with the royal member. |
number_in_line |
Optional[int]
|
The number in line to succession, if applicable. |
born_iso_date |
Optional[str]
|
The ISO date of birth. |
died_iso_date |
Optional[str]
|
The ISO date of death, if deceased. |
last_modified_iso |
str
|
ISO timestamp of the last modification. |
age |
Optional[int]
|
The age of the royal member. |
of_age |
Optional[bool]
|
Indicates whether the member is of legal age. |
wikidata_url |
Optional[str]
|
URL to the Wikidata page of the member. |
Source code in lodstorage/sample2.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
born
property
Return the date of birth from the ISO date string.
died
property
Return the date of death from the ISO date string, if available.
identifier
property
Generates a unique identifier for the Royal instance. The identifier is a combination of a slugified name and the Wikidata ID (if available).
__post_init__()
init calculated fields
Source code in lodstorage/sample2.py
44 45 46 47 48 49 50 51 52 | |
Royals
Represents a collection of Royal family members.
Attributes:
| Name | Type | Description |
|---|---|---|
members |
List[Royal]
|
A list of Royal family members. |
Source code in lodstorage/sample2.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
get_samples()
classmethod
Returns a dictionary of named samples for 'specification by example' style requirements management.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Royals]
|
A dictionary with keys as sample names and values as |
Source code in lodstorage/sample2.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
Sample
Sample dataset provider
Source code in lodstorage/sample2.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
get(dataset_name)
staticmethod
Get the given sample dataset name
Source code in lodstorage/sample2.py
223 224 225 226 227 228 229 230 231 232 233 234 235 | |
getCities()
staticmethod
get a list of city records compatibility for old sample module return list of dicts
Source code in lodstorage/sample2.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
getRoyals()
staticmethod
compatibility for old sample module return list of dicts
Source code in lodstorage/sample2.py
256 257 258 259 260 261 262 263 264 265 266 267 268 | |
getSample(size)
staticmethod
get a generated sample of the given size
Source code in lodstorage/sample2.py
246 247 248 249 250 251 252 253 254 | |
schema
Created on 2021-01-26
@author: wf
Schema
Bases: object
a relational Schema
Source code in lodstorage/schema.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
__init__(name, title)
Constructor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name(str)
|
the name of the schema |
required | |
title(str)
|
the title of the schema |
required |
Source code in lodstorage/schema.py
38 39 40 41 42 43 44 45 46 47 48 | |
generalizeColumn(tableList, colName)
staticmethod
remove the column with the given name from all tables in the tablelist and return it
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tableList(list)
|
a list of Tables |
required | |
colName(string)
|
the name of the column to generalize |
required |
Returns:
| Name | Type | Description |
|---|---|---|
string |
the column having been generalized and removed |
Source code in lodstorage/schema.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
getGeneral(tableList, name, debug=False)
staticmethod
derive a general table from the given table list Args: tableList(list): a list of tables name(str): name of the general table debug(bool): True if column names should be shown
Returns:
| Type | Description |
|---|---|
|
a table dict for the generalized table |
Source code in lodstorage/schema.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
getGeneralViewDDL(tableList, name, debug=False)
staticmethod
get the DDL statement to create a general view
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tableList
|
the list of tables |
required | |
name
|
str
|
the name of the view |
required |
debug
|
bool
|
True if debug should be set |
False
|
Source code in lodstorage/schema.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
SchemaManager
Bases: object
a manager for schemas
Source code in lodstorage/schema.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
__init__(schemaDefs=None, baseUrl=None)
constructor Args: schemaDefs(dict): a dictionary of schema names baseUrl(str): the base url to use for links
Source code in lodstorage/schema.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
sparql
Created on 2020-08-14
@author: wf
SPARQL
Bases: object
wrapper for SPARQL e.g. Apache Jena, Virtuoso, Blazegraph
:ivar url: full endpoint url (including mode) :ivar mode: 'query' or 'update' :ivar debug: True if debugging is active :ivar typedLiterals: True if INSERT should be done with typedLiterals :ivar profile(boolean): True if profiling / timing information should be displayed :ivar sparql: the SPARQLWrapper2 instance to be used :ivar method(str): the HTTP method to be used 'POST' or 'GET'
Source code in lodstorage/sparql.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 | |
__init__(url, mode='query', debug=False, isFuseki=False, typedLiterals=False, profile=False, agent='PyLodStorage', method='POST', calls_per_minute=None)
Construct a SPARQL wrapper
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
string
|
the base URL of the endpoint - the mode query/update is going to be appended |
required |
mode
|
string
|
'query' or 'update' |
'query'
|
debug
|
bool
|
True if debugging is to be activated |
False
|
typedLiterals
|
bool
|
True if INSERT should be done with typedLiterals |
False
|
profile
|
boolean
|
True if profiling / timing information should be displayed |
False
|
agent
|
string
|
the User agent to use |
'PyLodStorage'
|
method
|
string
|
the HTTP method to be used 'POST' or 'GET' |
'POST'
|
Source code in lodstorage/sparql.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
addAuthentication(username, password, method='BASIC')
Add Http Authentication credentials to the sparql wrapper Args: username: name of the user password: password of the user method: HTTP Authentication method
Source code in lodstorage/sparql.py
98 99 100 101 102 103 104 105 106 107 108 109 110 | |
asListOfDicts(records, fixNone=False, sampleCount=None)
convert SPARQL result back to python native
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record(list)
|
the list of bindings |
required | |
fixNone(bool)
|
if True add None values for empty columns in Dict |
required | |
sampleCount(int)
|
the number of samples to check |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
a list of Dicts |
Source code in lodstorage/sparql.py
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
controlEscape(s)
staticmethod
escape control characters
see https://stackoverflow.com/a/9778992/1497139
Source code in lodstorage/sparql.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | |
fix_comments(query_string)
make sure broken SPARQLWrapper will find comments
Source code in lodstorage/sparql.py
186 187 188 189 190 191 192 | |
fromEndpointConf(endpointConf)
classmethod
create a SPARQL endpoint from the given EndpointConfiguration
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpointConf
|
Endpoint
|
the endpoint configuration to be used |
required |
Source code in lodstorage/sparql.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
getFirst(qLod, attr)
get the column attr of the first row of the given qLod list
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qLod(list)
|
the list of dicts (returned by a query) |
required | |
attr(str)
|
the attribute to retrieve |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
the value |
Source code in lodstorage/sparql.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
getLocalName(name)
retrieve valid localname from a string based primary key https://www.w3.org/TR/sparql11-query/#prefNames
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name(string)
|
the name to convert |
required |
Returns:
| Name | Type | Description |
|---|---|---|
string |
a valid local name |
Source code in lodstorage/sparql.py
284 285 286 287 288 289 290 291 292 293 294 295 296 | |
getResults(jsonResult)
get the result from the given jsonResult
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jsonResult
|
the JSON encoded result |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
the list of bindings |
Source code in lodstorage/sparql.py
248 249 250 251 252 253 254 255 256 257 258 | |
getValue(sparqlQuery, attr)
get the value for the given SPARQL query using the given attr
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparql(SPARQL)
|
the SPARQL endpoint to ge the value for |
required | |
sparqlQuery(str)
|
the SPARQL query to run |
required | |
attr(str)
|
the attribute to get |
required |
Source code in lodstorage/sparql.py
194 195 196 197 198 199 200 201 202 203 204 205 206 | |
getValues(sparqlQuery, attrList)
get Values for the given sparlQuery and attribute list
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparqlQuery(str)
|
the query which did not return any values |
required | |
attrList(list)
|
the list of attributes |
required |
Source code in lodstorage/sparql.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
insert(insertCommand)
run an insert
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
insertCommand(string)
|
the SPARQL INSERT command |
required |
Returns:
| Type | Description |
|---|---|
|
a response |
Source code in lodstorage/sparql.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
insertListOfDicts(listOfDicts, entityType, primaryKey, prefixes, limit=None, batchSize=None, profile=False)
insert the given list of dicts mapping datatypes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entityType(string)
|
the entityType to use as a |
required | |
primaryKey(string)
|
the name of the primary key attribute to use |
required | |
prefix(string)
|
any PREFIX statements to be used |
required | |
limit(int)
|
maximum number of records to insert |
required | |
batchSize(int)
|
number of records to send per request |
required |
Return
a list of errors which should be empty on full success
datatype maping according to https://www.w3.org/TR/xmlschema-2/#built-in-datatypes
mapped from https://docs.python.org/3/library/stdtypes.html
compare to https://www.w3.org/2001/sw/rdb2rdf/directGraph/ http://www.bobdc.com/blog/json2rdf/ https://www.w3.org/TR/json-ld11-api/#data-round-tripping https://stackoverflow.com/questions/29030231/json-to-rdf-xml-file-in-python
Source code in lodstorage/sparql.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
insertListOfDictsBatch(listOfDicts, entityType, primaryKey, prefixes, title='batch', batchIndex=None, total=None, startTime=None)
insert a Batch part of listOfDicts
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entityType(string)
|
the entityType to use as a |
required | |
primaryKey(string)
|
the name of the primary key attribute to use |
required | |
prefix(string)
|
any PREFIX statements to be used |
required | |
title(string)
|
the title to display for the profiling (if any) |
required | |
batchIndex(int)
|
the start index of the current batch |
required | |
total(int)
|
the total number of records for all batches |
required | |
starttime(datetime)
|
the start of the batch processing |
required |
Return
a list of errors which should be empty on full success
Source code in lodstorage/sparql.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
post_query_direct(query, rdf_format='n3', timeout=60)
Fetch raw RDF response via direct HTTP POST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SPARQL CONSTRUCT query |
required |
rdf_format
|
str
|
RDF format label (e.g. 'turtle', 'rdf-xml', 'json-ld', 'n3') |
'n3'
|
timeout
|
int
|
timeout in seconds (default: 60) |
60
|
Returns:
| Type | Description |
|---|---|
str
|
Raw RDF content as string |
Source code in lodstorage/sparql.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
printErrors(errors)
print the given list of errors
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
errors(list)
|
a list of error strings |
required |
Returns:
| Name | Type | Description |
|---|---|---|
boolean |
True if the list is empty else false |
Source code in lodstorage/sparql.py
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 | |
query(queryString, method=POST)
get a list of results for the given query
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queryString(string)
|
the SPARQL query to execute |
required | |
method(string)
|
the method eg. POST to use |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list of bindings |
Source code in lodstorage/sparql.py
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |
queryAsListOfDicts(queryString, fixNone=False, sampleCount=None, param_dict=None)
Get a list of dicts for the given query (to allow round-trip results for insertListOfDicts)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queryString
|
str
|
the SPARQL query to execute |
required |
fixNone
|
bool
|
if True add None values for empty columns in Dict |
False
|
sampleCount
|
int
|
the number of samples to check |
None
|
param_dict
|
dict
|
dictionary of parameter names and values to be applied to the query |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
a list of Dicts |
Raises:
| Type | Description |
|---|---|
Exception
|
If the query requires parameters but they are not provided |
Source code in lodstorage/sparql.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
rawQuery(queryString, method=POST)
query with the given query string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queryString(str)
|
the SPARQL query to be performed |
required | |
method(str)
|
POST or GET - POST is mandatory for update queries |
required |
Returns: list: the raw query result as bindings
Source code in lodstorage/sparql.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
strToDatetime(value, debug=False)
staticmethod
convert a string to a datetime Args: value(str): the value to convert Returns: datetime: the datetime
Source code in lodstorage/sparql.py
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | |
test_query(query='SELECT * WHERE { ?s ?p ?o } LIMIT 1', expected_bindings=1)
Check if the SPARQL endpoint is available using a standard SPARQL query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
the SPARQL query to use for testing |
'SELECT * WHERE { ?s ?p ?o } LIMIT 1'
|
Returns:
| Type | Description |
|---|---|
Exception
|
Exception if the endpoint fails |
Source code in lodstorage/sparql.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
sql
Created on 2020-08-24
@author: wf
EntityInfo
Bases: object
holds entity meta Info
:ivar name(string): entity name = table name
:ivar primaryKey(string): the name of the primary key column
:ivar typeMap(dict): maps column names to python types
:ivar debug(boolean): True if debug information should be shown
Source code in lodstorage/sql.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 | |
__init__(sampleRecords, name, primaryKey=None, quiet=False, debug=False)
construct me from the given name and primary key
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampleRecords
|
a list of sample record dicts |
required | |
name(string)
|
the name of the entity |
required | |
primaryKey(string)
|
the name of the primary key column |
required | |
quite(boolean)
|
if True suppress all print messages |
required | |
debug(boolean)
|
True if debug information should be shown |
required |
Source code in lodstorage/sql.py
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | |
addType(column, valueType, sqlType)
add the python type for the given column to the typeMap
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column(string)
|
the name of the column |
required | |
valueType(type)
|
the python type of the column |
required |
Source code in lodstorage/sql.py
625 626 627 628 629 630 631 632 633 634 635 636 | |
fixDates(resultList)
fix date entries in the given resultList by parsing the date content e.g. converting '1926-04-21' back to datetime.date(1926, 4, 21)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resultList(list)
|
the list of records to be fixed |
required |
Source code in lodstorage/sql.py
638 639 640 641 642 643 644 645 646 647 648 649 650 651 | |
getCreateTableCmd(sampleRecords)
get the CREATE TABLE DDL command for the given sample records
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampleRecords(list)
|
a list of Dicts of sample Records |
required |
Returns:
| Name | Type | Description |
|---|---|---|
string |
CREATE TABLE DDL command for this entity info |
Example:
.. code-block:: sql
CREATE TABLE Person(name TEXT PRIMARY KEY,born DATE,numberInLine INTEGER,wikidataurl TEXT,age FLOAT,ofAge BOOLEAN)
Source code in lodstorage/sql.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
getInsertCmd(replace=False)
get the INSERT command for this entityInfo
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replace(bool)
|
if True allow replace for insert |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
the INSERT INTO SQL command for his entityInfo e.g. |
Example:
.. code-block:: sql
INSERT INTO Person (name,born,numberInLine,wikidataurl,age,ofAge) values (?,?,?,?,?,?).
Source code in lodstorage/sql.py
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | |
SQLDB
Bases: object
Structured Query Language Database wrapper
:ivar dbname(string): name of the database :ivar debug(boolean): True if debug info should be provided :ivar errorDebug(boolean): True if debug info should be provided on errors (should not be used for production since it might reveal data)
Source code in lodstorage/sql.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
__init__(dbname=':memory:', connection=None, check_same_thread=True, timeout=5, debug=False, errorDebug=False)
Construct me for the given dbname and debug
Args:
dbname(string): name of the database - default is a RAM based database connection(Connection): an optional connection to be reused check_same_thread(boolean): True if object handling needs to be on the same thread see https://stackoverflow.com/a/48234567/1497139 timeout(float): number of seconds for connection timeout debug(boolean): if True switch on debug errorDebug(boolean): True if debug info should be provided on errors (should not be used for production since it might reveal data)
Source code in lodstorage/sql.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
backup(backupDB, action='Backup', profile=False, showProgress=200, doClose=True)
create backup of this SQLDB to the given backup db
see https://stackoverflow.com/a/59042442/1497139
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backupDB(string)
|
the path to the backupdb or SQLDB.RAM for in memory |
required | |
action(string)
|
the action to display |
required | |
profile(boolean)
|
True if timing information shall be shown |
required | |
showProgress(int)
|
show progress at each showProgress page (0=show no progress) |
required |
Source code in lodstorage/sql.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
close()
close my connection
Source code in lodstorage/sql.py
75 76 77 | |
copyTo(copyDB, profile=True)
copy my content to another database
Args:
copyDB(Connection): the target database profile(boolean): if True show profile information
Source code in lodstorage/sql.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
createTable(listOfRecords, entityName, primaryKey=None, withCreate=True, withDrop=False, sampleRecordCount=1, failIfTooFew=True)
Derive Data Definition Language CREATE TABLE command from list of Records by examining first record as defining sample record and execute DDL command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
listOfRecords
|
list
|
A list of Dicts. |
required |
entityName
|
str
|
The entity / table name to use. |
required |
primaryKey
|
str
|
The key/column to use as a primary key. |
None
|
withDrop
|
bool
|
True if the existing Table should be dropped. |
False
|
withCreate
|
bool
|
True if the create Table command should be executed. |
True
|
sampleRecordCount
|
int
|
Number of sample records expected and to be inspected. |
1
|
failIfTooFew
|
bool
|
Raise an Exception if too few sample records, else warn only. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
EntityInfo |
Meta data information for the created table. |
Source code in lodstorage/sql.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
createTable4EntityInfo(entityInfo, withDrop=False, withCreate=True)
Create a table based on the provided EntityInfo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entityInfo
|
EntityInfo
|
The EntityInfo object containing table metadata. |
required |
withDrop
|
bool
|
If True, drop the existing table before creation. |
False
|
withCreate
|
bool
|
If True, execute the CREATE TABLE command. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
EntityInfo |
The provided EntityInfo object. |
Source code in lodstorage/sql.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
execute(ddlCmd)
execute the given Data Definition Command
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ddlCmd(string)
|
e.g. a CREATE TABLE or CREATE View command |
required |
Source code in lodstorage/sql.py
79 80 81 82 83 84 85 86 | |
executeDump(connection, dump, title, maxErrors=100, errorDisplayLimit=12, profile=True)
execute the given dump for the given connection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection(Connection)
|
the sqlite3 connection to use |
required | |
dump(string)
|
the SQL commands for the dump |
required | |
title(string)
|
the title of the dump |
required | |
maxErrors(int)
|
maximum number of errors to be tolerated before stopping and doing a rollback |
required | |
profile(boolean)
|
True if profiling information should be shown |
required |
Returns: a list of errors
Source code in lodstorage/sql.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
getDebugInfo(record, index, executeMany)
get the debug info for the given record at the given index depending on the state of executeMany
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record(dict)
|
the record to show |
required | |
index(int)
|
the index of the record |
required | |
executeMany(boolean)
|
if True the record may be valid else not |
required |
Source code in lodstorage/sql.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
getTableDict(tableType='table')
get the schema information from this database as a dict
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tableType(str)
|
table or view |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Lookup map of tables with columns also being converted to dict |
Source code in lodstorage/sql.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
getTableList(tableType='table')
get the schema information from this database
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tableType(str)
|
table or view |
required |
Return
list: a list as derived from PRAGMA table_info
Source code in lodstorage/sql.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
logError(msg)
log the given error message to stderr
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg(str)
|
the error messsage to display |
required |
Source code in lodstorage/sql.py
66 67 68 69 70 71 72 73 | |
progress(action, status, remaining, total)
show progress
Source code in lodstorage/sql.py
346 347 348 349 350 351 352 353 354 355 356 357 | |
query(sqlQuery, params=None)
run the given sqlQuery and return a list of Dicts
Args:
sqlQuery(string): the SQL query to be executed
params(tuple): the query params, if any
Returns:
| Name | Type | Description |
|---|---|---|
list |
a list of Dicts |
Source code in lodstorage/sql.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
queryAll(entityInfo, fixDates=True)
query all records for the given entityName/tableName
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entityName(string)
|
name of the entity/table to qury |
required | |
fixDates(boolean)
|
True if date entries should be returned as such and not as strings |
required |
Source code in lodstorage/sql.py
288 289 290 291 292 293 294 295 296 297 298 299 300 | |
queryGen(sqlQuery, params=None)
run the given sqlQuery a a generator for dicts
Args:
sqlQuery(string): the SQL query to be executed
params(tuple): the query params, if any
Returns:
| Type | Description |
|---|---|
|
a generator of dicts |
Source code in lodstorage/sql.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
restore(backupDB, restoreDB, profile=False, showProgress=200, debug=False)
staticmethod
restore the restoreDB from the given backup DB
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backupDB(string)
|
path to the backupDB e.g. backup.db |
required | |
restoreDB(string)
|
path to the restoreDB or in Memory SQLDB.RAM |
required | |
profile(boolean)
|
True if timing information should be shown |
required | |
showProgress(int)
|
show progress at each showProgress page (0=show no progress) |
required |
Source code in lodstorage/sql.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
showDump(dump, limit=10)
show the given dump up to the given limit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dump(string)
|
the SQL dump to show |
required | |
limit(int)
|
the maximum number of lines to display |
required |
Source code in lodstorage/sql.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
store(listOfRecords, entityInfo, executeMany=False, fixNone=False, replace=False)
store the given list of records based on the given entityInfo
Args:
listOfRecords(list): the list of Dicts to be stored entityInfo(EntityInfo): the meta data to be used for storing executeMany(bool): if True the insert command is done with many/all records at once fixNone(bool): if True make sure empty columns in the listOfDict are filled with "None" values replace(bool): if True allow replace for insert
Source code in lodstorage/sql.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
sqlite_api
Created on 2024-08-24
@author: wf
DatetimeAdapter
Class for converting date and time formats with optional lenient error handling.
Source code in lodstorage/sqlite_api.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
__init__(lenient=False)
Initialize with optional lenient error handling.
Source code in lodstorage/sqlite_api.py
15 16 17 | |
convert_date(val)
Convert ISO 8601 date byte string to a datetime.date object.
Source code in lodstorage/sqlite_api.py
33 34 35 36 37 38 39 40 | |
convert_datetime(val)
Convert ISO 8601 datetime byte string to a datetime.datetime object.
Source code in lodstorage/sqlite_api.py
42 43 44 45 46 47 48 | |
convert_timestamp(val)
Convert Unix epoch timestamp byte string to a datetime.datetime object.
Source code in lodstorage/sqlite_api.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
SQLiteApiFixer
Class to register SQLite adapters and converters using a DatetimeAdapter instance.
Source code in lodstorage/sqlite_api.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
__init__(lenient=True)
Private constructor to initialize the singleton instance.
Source code in lodstorage/sqlite_api.py
75 76 77 78 79 | |
adapt_boolean(val)
staticmethod
Adapt boolean to int.
Source code in lodstorage/sqlite_api.py
111 112 113 114 | |
adapt_date_iso(val)
staticmethod
Adapt datetime.date to ISO 8601 date.
Source code in lodstorage/sqlite_api.py
101 102 103 104 | |
adapt_datetime_iso(val)
staticmethod
Adapt datetime.datetime to timezone-naive ISO 8601 date.
Source code in lodstorage/sqlite_api.py
106 107 108 109 | |
convert_boolean(val)
staticmethod
Convert 0 or 1 to boolean.
Source code in lodstorage/sqlite_api.py
116 117 118 119 | |
install(lenient=True)
classmethod
Install the singleton instance and register SQLite adapters and converters.
Source code in lodstorage/sqlite_api.py
81 82 83 84 85 86 | |
register_adapters()
Register the necessary SQLite adapters.
Source code in lodstorage/sqlite_api.py
88 89 90 91 92 | |
register_converters()
Register the necessary SQLite converters.
Source code in lodstorage/sqlite_api.py
94 95 96 97 98 99 | |
storageconfig
Created on 2020-08-29
@author: wf
StorageConfig
Bases: object
a storage configuration
Source code in lodstorage/storageconfig.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
__init__(mode=StoreMode.SQL, cacheRootDir=None, cacheDirName='lodstorage', cacheFile=None, withShowProgress=True, profile=True, debug=False, errorDebug=True)
Constructor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode(StoreMode)
|
the storage mode e.g. sql |
required | |
cacheRootDir(str)
|
the cache root directory to use - if None the home directory will be used |
required | |
cacheFile(string)
|
the common cacheFile to use (if any) |
required | |
withShowProgress(boolean)
|
True if progress should be shown |
required | |
profile(boolean)
|
True if timing / profiling information should be shown |
required | |
debug(boolean)
|
True if debugging information should be shown |
required | |
errorDebug(boolean)
|
True if debug info should be provided on errors (should not be used for production since it might reveal data) |
required |
Source code in lodstorage/storageconfig.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
getCachePath(ensureExists=True)
get the path to the default cache
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name(str)
|
the name of the cache to use |
required |
Source code in lodstorage/storageconfig.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
StoreMode
Bases: Enum
possible supported storage modes
Source code in lodstorage/storageconfig.py
12 13 14 15 16 17 18 19 20 21 | |
sync
Created on 2023-12-27
@author: wf
Sync
A class to help with synchronization between two sets of data, each represented as a list of dictionaries.
Source code in lodstorage/sync.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
__init__(pair)
Initialize the Sync class with the given Synchronization Pair.
Source code in lodstorage/sync.py
66 67 68 69 70 71 72 73 | |
get_keys(direction)
Get the keys for a given direction of synchronization.
Source code in lodstorage/sync.py
148 149 150 151 152 153 154 155 | |
get_record_by_key(side, key)
Retrieves a record by the given unique key from the appropriate data source as specified by direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
side
|
str
|
The side of data source, "←","l" or "left" for left and "→","r" or "right" for right. |
required |
key
|
str
|
The unique key of the record to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Optional[Dict[str, Any]]: The record if found, otherwise None. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided direction is invalid. |
Source code in lodstorage/sync.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
get_record_by_pkey(side, pkey)
Retrieves a record by primary key from the appropriate data source as specified by direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
side
|
str
|
The side of data source, "←","l" or "left" for left and "→","r" or "right" for right. |
required |
pkey
|
str
|
The primary key of the record to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Optional[Dict[str, Any]]: The record if found, otherwise None. |
Source code in lodstorage/sync.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
status_table(tablefmt='grid')
Create a table representing the synchronization status.
Source code in lodstorage/sync.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
SyncPair
dataclass
A class to represent a pair of data sources for synchronization.
Attributes: title (str): The title of the synchronization pair. l_name (str): Name of the left data source (e.g., 'local'). r_name (str): Name of the right data source (e.g., 'wikidata'). l_data (List[Dict[str, Any]]): A list of dictionaries from the left data source. r_data (List[Dict[str, Any]]): A list of dictionaries from the right data source. l_key (str): The field name in the left data source dictionaries used as a unique identifier for synchronization. r_key (str): The field name in the right data source dictionaries used as a unique identifier for synchronization. l_pkey(str): the primary key field of the left data source r_pkey(str): the primary key field of the right data source
Example usage: l_data = [{'id_l': '1', 'value': 'a'}, {'id_l': '2', 'value': 'b'}] r_data = [{'id_r': '2', 'value': 'b'}, {'id_r': '3', 'value': 'c'}] pair = SyncPair("Title", "local", "wikidata", l_data, r_data, 'id_l', 'id_r') sync = Sync(pair) print(sync.status_table())
Source code in lodstorage/sync.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
tabulateCounter
Created on 2021-06-13
@author: wf
TabulateCounter
Bases: object
helper for tabulating Counters
Source code in lodstorage/tabulateCounter.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
__init__(counter)
Constructor
Source code in lodstorage/tabulateCounter.py
15 16 17 18 19 | |
mostCommonTable(headers=['#', 'key', 'count', '%'], tablefmt='pretty', limit=50)
get the most common Table
Source code in lodstorage/tabulateCounter.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
uml
Created on 2020-09-04
@author: wf
UML
Bases: object
UML diagrams via plantuml
Source code in lodstorage/uml.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
__init__(debug=False)
Constructor Args: debug(boolean): True if debug information should be shown
Source code in lodstorage/uml.py
97 98 99 100 101 102 103 | |
mergeSchema(schemaManager, tableList, title=None, packageName=None, generalizeTo=None, withSkin=True)
merge Schema and tableList to PlantUml notation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schemaManager(SchemaManager)
|
a schema manager to be used |
required | |
tableList(list)
|
the tableList list of Dicts from getTableList() to convert |
required | |
title(string)
|
optional title to be added |
required | |
packageName(string)
|
optional packageName to be added |
required | |
generalizeTo(string)
|
optional name of a general table to be derived |
required | |
withSkin(boolean)
|
if True add default BITPlan skin parameters |
required |
Returns:
| Name | Type | Description |
|---|---|---|
string |
the Plantuml notation for the entities in columns of the given tablelist |
Source code in lodstorage/uml.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
tableListToPlantUml(tableList, title=None, packageName=None, generalizeTo=None, withSkin=True)
convert tableList to PlantUml notation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tableList(list)
|
the tableList list of Dicts from getTableList() to convert |
required | |
title(string)
|
optional title to be added |
required | |
packageName(string)
|
optional packageName to be added |
required | |
generalizeTo(string)
|
optional name of a general table to be derived |
required | |
withSkin(boolean)
|
if True add default BITPlan skin parameters |
required |
Returns:
| Name | Type | Description |
|---|---|---|
string |
the Plantuml notation for the entities in columns of the given tablelist |
Source code in lodstorage/uml.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
version
Created on 2022-03-06
@author: wf
Version
Bases: object
Version handling for pyLoDStorage
Source code in lodstorage/version.py
10 11 12 13 14 15 16 17 18 19 | |
yaml_path
Created on 2025-11-23
@author: wf
YamlPath
provide path to loading configuration or data files by checking: - a provided path or an optional user-specific location (~/.pylodstorage).
Source code in lodstorage/yaml_path.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
getDefaultPath(yamlFileName)
classmethod
Get the path to the YAML file in the default user home location (.pylodstorage).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yamlFileName
|
str
|
The name of the YAML file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The full path to the file in the user's home directory. |
Source code in lodstorage/yaml_path.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
getPaths(yamlFileName, yamlPath=None, with_default=True)
classmethod
Get a list of YAML file paths to be used for loading configuration/data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yamlFileName
|
str
|
The name of the YAML file. |
required |
yamlPath
|
str
|
The full path to read from. Defaults to None (uses getSamplePath). |
None
|
with_default
|
bool
|
Whether to include paths from the default location .pylodstorage in the Home directory. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of file paths found. |
Source code in lodstorage/yaml_path.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
getSamplePath(yamlFileName)
classmethod
Get the path to the sample YAML file usually located in ../sampledata relative to this script.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yamlFileName
|
str
|
The name of the YAML file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The absolute path to the sample file. |
Source code in lodstorage/yaml_path.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |