pyLodStorage API Documentation
cache
Created on 2024-03-09
@author: wf
refactored from https://github.com/WolfgangFahl/pyCEURmake/blob/main/ceurws/utils/json_cache.py by Tim Holzheim
Cache
Represents cache metadata and its file extension.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the cache. |
extension |
str
|
The file extension for the cache (e.g., 'json', 'csv'). |
size |
int
|
The size of the cache file in bytes. |
count |
Optional[int]
|
Optional; the number of items in the cache, if applicable. |
count_attr |
str
|
the name of the attribute to determine the number of items, if applicable |
last_accessed |
datetime
|
Optional; the last accessed timestamp of the cache. |
Source code in lodstorage/cache.py
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 |
|
is_stored: bool
property
Determines if the cache file exists and is not empty.
set_path(base_path)
Set my path based on the given base_path and ensure the parent directory is created.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_path |
str
|
The base path where the directory should be created. |
required |
Source code in lodstorage/cache.py
40 41 42 43 44 45 46 47 48 49 |
|
CacheManager
Manages multiple cache files with various extensions.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name used for the base directory where cache files are stored. |
caches |
Dict[str, Cache]
|
A dictionary to track each cache's metadata. |
Source code in lodstorage/cache.py
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 |
|
base_path()
Fetches the base path for this cache manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache |
The cache for which to generate the file path. |
required |
Returns:
Type | Description |
---|---|
str
|
The base path |
Source code in lodstorage/cache.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
get_cache_by_name(lod_name, ext='.json')
Retrieves or creates a cache object by name and extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache_name |
str
|
The name of the cache to retrieve or create. |
required |
ext |
str
|
The file extension for the cache. |
'.json'
|
Returns:
Name | Type | Description |
---|---|---|
Cache |
Cache
|
An existing or newly created Cache object. |
Source code in lodstorage/cache.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
load(lod_name, ext='.json', cls=None, count_attr=None)
Load data from a cache file. This method supports JSON and, if a relevant class is provided, other formats like YAML.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lod_name |
str
|
The name of the list of dicts or class instances to read from cache. |
required |
ext |
str
|
The extension of the cache file, indicating the format (default is ".json"). |
'.json'
|
cls |
Optional[Type[YamlAble]]
|
The class type for deserialization. This class must have from_json() or from_yaml() class methods for deserialization, depending on the file extension. |
None
|
count_attr(str) |
the name of attribute data_to_store for updating the cache.count s |
required |
Returns: Union[List, Dict, None]: A list of dicts, a list of class instances, a single dict, or None if the cache is not stored.
Source code in lodstorage/cache.py
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 |
|
store(cache_name, data_to_store, ext='.json', count_attr=None)
Stores data into a cache file, handling serialization based on the specified file extension. Supports JSON and YAML formats, and custom serialization for classes that provide specific serialization methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache_name |
str
|
The identifier for the cache where the data will be stored. |
required |
data_to_store |
Union[List, Dict]
|
The data to be stored in the cache. This can be a list of dictionaries,
a single dictionary, or instances of data classes if |
required |
ext |
str
|
The file extension indicating the serialization format (e.g., '.json', '.yaml'). Defaults to '.json'. |
'.json'
|
count_attr(str) |
the name of attribute data_to_store for updating the cache.count s |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the file extension is unsupported or if required methods for serialization are not implemented in |
Source code in lodstorage/cache.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 |
|
docstring_parser
Created on 2024-01-21
@author: wf
DocstringParser
A Python docstring parser.
Source code in lodstorage/docstring_parser.py
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 |
|
parse(docstring)
Parse the given docstring.
Source code in lodstorage/docstring_parser.py
52 53 54 55 56 57 58 59 60 61 62 |
|
entity
Created on 2020-08-19
@author: wf
EntityManager
Bases: YamlAbleMixin
, JsonPickleMixin
, JSONAbleList
generic entity manager
Source code in lodstorage/entity.py
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 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 |
|
__init__(name, entityName, entityPluralName, listName=None, clazz=None, tableName=None, primaryKey=None, config=None, handleInvalidListTypes=False, filterInvalidListTypes=False, listSeparator='⇹', debug=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name(string) |
name of this eventManager |
required | |
entityName(string) |
entityType to be managed e.g. Country |
required | |
entityPluralName(string) |
plural of the the entityType e.g. Countries |
required | |
config(StorageConfig) |
the configuration to be used if None a default configuration will be used |
required | |
handleInvalidListTypes(bool) |
True if invalidListTypes should be converted or filtered |
required | |
filterInvalidListTypes(bool) |
True if invalidListTypes should be deleted |
required | |
listSeparator(str) |
the symbol to use as a list separator |
required | |
debug(boolean) |
override debug setting when default of config is used via config=None |
required |
Source code in lodstorage/entity.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 |
|
fromCache(force=False, getListOfDicts=None, append=False, sampleRecordCount=-1)
get my entries from the cache or from the callback provided
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force(bool) |
force ignoring the cache |
required | |
getListOfDicts(callable) |
a function to call for getting the data |
required | |
append(bool) |
True if records should be appended |
required | |
sampleRecordCount(int) |
the number of records to analyze for type information |
required |
Returns:
Type | Description |
---|---|
the list of Dicts and as a side effect setting self.cacheFile |
Source code in lodstorage/entity.py
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 |
|
fromStore(cacheFile=None, setList=True)
restore me from the store Args: cacheFile(String): the cacheFile to use if None use the pre configured cachefile setList(bool): if True set my list with the data from the cache file
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
list of dicts or JSON entitymanager |
Source code in lodstorage/entity.py
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 |
|
getCacheFile(config=None, mode=StoreMode.SQL)
get the cache file for this event manager Args: config(StorageConfig): if None get the cache for my mode mode(StoreMode): the storeMode to use
Source code in lodstorage/entity.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
getLoD()
Return the LoD of the entities in the list
Return
list: a list of Dicts
Source code in lodstorage/entity.py
350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
getSQLDB(cacheFile)
get the SQL database for the given cacheFile
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cacheFile(string) |
the file to get the SQL db from |
required |
Source code in lodstorage/entity.py
135 136 137 138 139 140 141 142 143 144 145 146 |
|
initSQLDB(sqldb, listOfDicts=None, withCreate=True, withDrop=True, sampleRecordCount=-1)
initialize my sql DB
Parameters:
Name | Type | Description | Default |
---|---|---|---|
listOfDicts(list) |
the list of dicts to analyze for type information |
required | |
withDrop(boolean) |
true if the existing Table should be dropped |
required | |
withCreate(boolean) |
true if the create Table command should be executed - false if only the entityInfo should be returned |
required | |
sampleRecordCount(int) |
the number of records to analyze for type information |
required |
Return: EntityInfo: the entity information such as CREATE Table command
Source code in lodstorage/entity.py
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 |
|
isCached()
check whether there is a file containing cached data for me
Source code in lodstorage/entity.py
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 |
|
removeCacheFile()
remove my cache file
Source code in lodstorage/entity.py
127 128 129 130 131 132 133 |
|
setNone(record, fields)
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/entity.py
179 180 181 182 183 184 185 186 |
|
showProgress(msg)
display a progress message
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg(string) |
the message to display |
required |
Source code in lodstorage/entity.py
94 95 96 97 98 99 100 101 |
|
store(limit=10000000, batchSize=250, append=False, fixNone=True, sampleRecordCount=-1, replace=False)
store my list of dicts
Parameters:
Name | Type | Description | Default |
---|---|---|---|
limit(int) |
maximum number of records to store per batch |
required | |
batchSize(int) |
size of batch for storing |
required | |
append(bool) |
True if records should be appended |
required | |
fixNone(bool) |
if True make sure the dicts are filled with None references for each record |
required | |
sampleRecordCount(int) |
the number of records to analyze for type information |
required | |
replace(bool) |
if True allow replace for insert |
required |
Return
str: The cachefile being used
Source code in lodstorage/entity.py
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 |
|
storeLoD(listOfDicts, limit=10000000, batchSize=250, cacheFile=None, append=False, fixNone=True, sampleRecordCount=1, replace=False)
store my entities
Parameters:
Name | Type | Description | Default |
---|---|---|---|
listOfDicts(list) |
the list of dicts to store |
required | |
limit(int) |
maximum number of records to store |
required | |
batchSize(int) |
size of batch for storing |
required | |
cacheFile(string) |
the name of the storage e.g path to JSON or sqlite3 file |
required | |
append(bool) |
True if records should be appended |
required | |
fixNone(bool) |
if True make sure the dicts are filled with None references for each record |
required | |
sampleRecordCount(int) |
the number of records to analyze for type information |
required | |
replace(bool) |
if True allow replace for insert |
required |
Return: str: The cachefile being used
Source code in lodstorage/entity.py
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 |
|
storeMode()
return my store mode
Source code in lodstorage/entity.py
88 89 90 91 92 |
|
jsonable
This module has a class JSONAble for serialization of tables/list of dicts to and from JSON encoding
Created on 2020-09-03
@author: wf
JSONAble
Bases: object
mixin to allow classes to be JSON serializable see
- https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable
Source code in lodstorage/jsonable.py
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 |
|
__init__()
Constructor
Source code in lodstorage/jsonable.py
37 38 39 40 |
|
asJSON(asString=True, data=None)
recursively return my dict elements
Parameters:
Name | Type | Description | Default |
---|---|---|---|
asString(boolean) |
if True return my result as a string |
required |
Source code in lodstorage/jsonable.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
|
checkExtension(jsonFile, extension='.json')
make sure the jsonFile has the given extension e.g. ".json"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonFile(str) |
the jsonFile name - potentially without ".json" suffix |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
the jsonFile name with ".json" as an extension guaranteed |
Source code in lodstorage/jsonable.py
174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
fromDict(data)
initialize me from the given data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data(dict) |
the dictionary to initialize me from |
required |
Source code in lodstorage/jsonable.py
223 224 225 226 227 228 229 230 231 232 233 |
|
fromJson(jsonStr)
initialize me from the given JSON string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonStr(str) |
the JSON string |
required |
Source code in lodstorage/jsonable.py
213 214 215 216 217 218 219 220 221 |
|
getJSONValue(v)
get the value of the given v as JSON
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v(object) |
the value to get |
required |
Returns:
Type | Description |
---|---|
the the value making sure objects are return as dicts |
Source code in lodstorage/jsonable.py
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 |
|
getJsonTypeSamples()
does my class provide a "getSamples" method?
Source code in lodstorage/jsonable.py
121 122 123 124 125 126 127 128 129 130 |
|
getJsonTypeSamplesForClass()
staticmethod
return the type samples for the given class
Return
list: a list of dict that specify the types by example
Source code in lodstorage/jsonable.py
132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
readJsonFromFile(jsonFilePath)
staticmethod
read json string from the given jsonFilePath
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonFilePath(string) |
the path of the file where to read the result from |
required |
Returns:
Type | Description |
---|---|
the JSON string read from the file |
Source code in lodstorage/jsonable.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
reprDict(srcDict)
get the given srcDict as new dict with fields being converted with getJSONValue
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scrcDict(dict) |
the source dictionary |
required |
Returns dict: the converted dictionary
Source code in lodstorage/jsonable.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
restoreFromJsonFile(jsonFile)
restore me from the given jsonFile
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonFile(string) |
the jsonFile to restore me from |
required |
Source code in lodstorage/jsonable.py
202 203 204 205 206 207 208 209 210 211 |
|
singleQuoteToDoubleQuote(singleQuoted, useRegex=False)
staticmethod
convert a single quoted string to a double quoted one
Parameters:
Name | Type | Description | Default |
---|---|---|---|
singleQuoted |
str
|
a single quoted string e.g. .. highlight:: json {'cities': [{'name': "Upper Hell's Gate"}]} |
required |
useRegex |
boolean
|
True if a regular expression shall be used for matching |
False
|
Returns:
Name | Type | Description |
---|---|---|
string |
the double quoted version of the string |
Note
see - https://stackoverflow.com/questions/55600788/python-replace-single-quotes-with-double-quotes-but-leave-ones-within-double-q
Source code in lodstorage/jsonable.py
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 |
|
singleQuoteToDoubleQuoteUsingBracketLoop(singleQuoted)
staticmethod
convert a single quoted string to a double quoted one using a regular expression
Parameters:
Name | Type | Description | Default |
---|---|---|---|
singleQuoted(string) |
a single quoted string e.g. {'cities': [{'name': "Upper Hell's Gate"}]} |
required | |
useRegex(boolean) |
True if a regular expression shall be used for matching |
required |
Returns: string: the double quoted version of the string e.g. Note: see https://stackoverflow.com/a/63862387/1497139
Source code in lodstorage/jsonable.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 |
|
singleQuoteToDoubleQuoteUsingRegex(singleQuoted)
staticmethod
convert a single quoted string to a double quoted one using a regular expression
Parameters:
Name | Type | Description | Default |
---|---|---|---|
singleQuoted(string) |
a single quoted string e.g. {'cities': [{'name': "Upper Hell's Gate"}]} |
required | |
useRegex(boolean) |
True if a regular expression shall be used for matching |
required |
Returns: string: the double quoted version of the string e.g. Note: see https://stackoverflow.com/a/50257217/1497139
Source code in lodstorage/jsonable.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
storeJsonToFile(jsonStr, jsonFilePath)
staticmethod
store the given json string to the given jsonFilePath
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonStr(string) |
the string to store |
required | |
jsonFilePath(string) |
the path of the file where to store the result |
required |
Source code in lodstorage/jsonable.py
161 162 163 164 165 166 167 168 169 170 171 172 |
|
storeToJsonFile(jsonFile, extension='.json', limitToSampleFields=False)
store me to the given jsonFile
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonFile(str) |
the JSON file name (optionally without extension) |
required | |
exension(str) |
the extension to use if not part of the jsonFile name |
required | |
limitToSampleFields(bool) |
If True the returned JSON is limited to the attributes/fields that are present in the samples. Otherwise all attributes of the object will be included. Default is False. |
required |
Source code in lodstorage/jsonable.py
188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
toJSON(limitToSampleFields=False)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
limitToSampleFields(bool) |
If True the returned JSON is limited to the attributes/fields that are present in the samples. Otherwise all attributes of the object will be included. Default is False. |
required |
Returns:
Type | Description |
---|---|
a recursive JSON dump of the dicts of my objects |
Source code in lodstorage/jsonable.py
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 |
|
toJsonAbleValue(v)
return the JSON able value of the given value v Args: v(object): the value to convert
Source code in lodstorage/jsonable.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
JSONAbleList
Bases: JSONAble
Container class
Source code in lodstorage/jsonable.py
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 |
|
__init__(listName=None, clazz=None, tableName=None, initList=True, handleInvalidListTypes=False, filterInvalidListTypes=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
listName(str) |
the name of the list attribute to be used for storing the List |
required | |
clazz(class) |
a class to be used for Object relational mapping (if any) |
required | |
tableName(str) |
the name of the "table" to be used |
required | |
initList(bool) |
True if the list should be initialized |
required | |
handleInvalidListTypes(bool) |
True if invalidListTypes should be converted or filtered |
required | |
filterInvalidListTypes(bool) |
True if invalidListTypes should be deleted |
required |
Source code in lodstorage/jsonable.py
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 |
|
fromJson(jsonStr, types=None)
initialize me from the given JSON string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonStr(str) |
the JSON string |
required | |
fixType(Types) |
the types to be fixed |
required |
Source code in lodstorage/jsonable.py
504 505 506 507 508 509 510 511 512 513 |
|
fromLoD(lod, append=True, debug=False)
load my entityList from the given list of dicts
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lod(list) |
the list of dicts to load |
required | |
append(bool) |
if True append to my existing entries |
required |
Return
list: a list of errors (if any)
Source code in lodstorage/jsonable.py
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 |
|
getJsonData()
get my Jsondata
Source code in lodstorage/jsonable.py
487 488 489 490 491 492 |
|
getList()
get my list
Source code in lodstorage/jsonable.py
389 390 391 392 393 |
|
getLoDfromJson(jsonStr, types=None, listName=None)
get a list of Dicts form the given JSON String
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonStr(str) |
the JSON string |
required | |
fixType(Types) |
the types to be fixed |
required |
Returns: list: a list of dicts
Source code in lodstorage/jsonable.py
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 |
|
getLookup(attrName, withDuplicates=False)
create a lookup dictionary by the given attribute name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attrName(str) |
the attribute to lookup |
required | |
withDuplicates(bool) |
whether to retain single values or lists |
required |
Return
a dictionary for lookup or a tuple dictionary,list of duplicates depending on withDuplicates
Source code in lodstorage/jsonable.py
474 475 476 477 478 479 480 481 482 483 484 485 |
|
readLodFromJsonFile(jsonFile, extension='.json')
read the list of dicts from the given jsonFile
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonFile(string) |
the jsonFile to read from |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
a list of dicts |
Source code in lodstorage/jsonable.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
|
readLodFromJsonStr(jsonStr)
restore me from the given jsonStr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
storeFilePrefix(string) |
the prefix for the JSON file name |
required |
Source code in lodstorage/jsonable.py
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
|
restoreFromJsonFile(jsonFile)
read my list of dicts and restore it
Source code in lodstorage/jsonable.py
519 520 521 522 523 524 |
|
restoreFromJsonStr(jsonStr)
restore me from the given jsonStr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonStr(str) |
the json string to restore me from |
required |
Source code in lodstorage/jsonable.py
526 527 528 529 530 531 532 533 534 |
|
setListFromLoD(lod)
set my list from the given list of dicts
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
a list of dicts if no clazz is set otherwise a list of objects |
Source code in lodstorage/jsonable.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
|
toJsonAbleValue(v)
make sure we don't store our meta information clazz, tableName and listName but just the list we are holding
Source code in lodstorage/jsonable.py
494 495 496 497 498 499 500 501 502 |
|
JSONAbleSettings
settings for JSONAble - put in a separate class so they would not be serialized
Source code in lodstorage/jsonable.py
15 16 17 18 19 20 21 22 23 24 25 26 |
|
indent = 4
class-attribute
instance-attribute
regular expression to be used for conversion from singleQuote to doubleQuote see https://stackoverflow.com/a/50257217/1497139
Types
Bases: JSONAble
Types
holds entity meta Info
:ivar name(string): entity name = table name
Source code in lodstorage/jsonable.py
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 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 678 679 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 |
|
__init__(name, warnOnUnsupportedTypes=True, debug=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name(str) |
the name of the type map |
required | |
warnOnUnsupportedTypes(bool) |
if TRUE warn if an item value has an unsupported type |
required | |
debug(bool) |
if True - debugging information should be shown |
required |
Source code in lodstorage/jsonable.py
591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
addType(listName, field, valueType)
add the python type for the given field to the typeMap
Parameters:
Name | Type | Description | Default |
---|---|---|---|
listName(string) |
the name of the list of the field |
required | |
field(string) |
the name of the field |
required | |
valueType(type) |
the python type of the field |
required |
Source code in lodstorage/jsonable.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
|
fixListOfDicts(typeMap, listOfDicts)
fix the type in the given list of Dicts
Source code in lodstorage/jsonable.py
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 |
|
fixTypes(lod, listName)
fix the types in the given data structure
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lod(list) |
a list of dicts |
required | |
listName(str) |
the types to lookup by list name |
required |
Source code in lodstorage/jsonable.py
697 698 699 700 701 702 703 704 705 706 |
|
forTable(instance, listName, warnOnUnsupportedTypes=True, debug=False)
staticmethod
get the types for the list of Dicts (table) in the given instance with the given listName Args: instance(object): the instance to inspect listName(string): the list of dicts to inspect warnOnUnsupportedTypes(bool): if TRUE warn if an item value has an unsupported type debug(bool): True if debuggin information should be shown
Returns:
Name | Type | Description |
---|---|---|
Types |
a types object |
Source code in lodstorage/jsonable.py
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
|
getType(typeName)
get the type for the given type name
Source code in lodstorage/jsonable.py
708 709 710 711 712 713 714 715 716 717 |
|
getTypes(listName, sampleRecords, limit=10)
determine the types for the given sample records
Parameters:
Name | Type | Description | Default |
---|---|---|---|
listName(str) |
the name of the list |
required | |
sampleRecords(list) |
a list of items |
required | |
limit(int) |
the maximum number of items to check |
required |
Source code in lodstorage/jsonable.py
643 644 645 646 647 648 649 650 651 652 653 654 |
|
getTypesForItems(listName, items, warnOnNone=False)
get the types for the given items side effect is setting my types
Parameters:
Name | Type | Description | Default |
---|---|---|---|
listName(str) |
the name of the list |
required | |
items(list) |
a list of items |
required | |
warnOnNone(bool) |
if TRUE warn if an item value is None |
required |
Source code in lodstorage/jsonable.py
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 |
|
jsonpicklemixin
JsonPickleMixin
Bases: object
allow reading and writing derived objects from a jsonpickle file
Source code in lodstorage/jsonpicklemixin.py
7 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 |
|
asJsonPickle()
convert me to JSON
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
a JSON String with my JSON representation |
Source code in lodstorage/jsonpicklemixin.py
52 53 54 55 56 57 58 59 60 |
|
checkExtension(jsonFile, extension='.json')
staticmethod
make sure the jsonFile has the given extension e.g. ".json"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonFile(str) |
the jsonFile name - potentially without ".json" suffix |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
the jsonFile name with ".json" as an extension guaranteed |
Source code in lodstorage/jsonpicklemixin.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
readJsonPickle(jsonFileName, extension='.jsonpickle')
staticmethod
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonFileName(str) |
name of the file (optionally without ".json" postfix) |
required | |
extension(str) |
default file extension |
required |
Source code in lodstorage/jsonpicklemixin.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
writeJsonPickle(jsonFileName, extension='.jsonpickle')
write me to the json file with the given name (optionally without postfix)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonFileName(str) |
name of the file (optionally without ".json" postfix) |
required | |
extension(str) |
default file extension |
required |
Source code in lodstorage/jsonpicklemixin.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
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 |
|
__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
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 |
|
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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
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
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 |
|
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
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
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
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 |
|
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
49 50 51 52 53 54 55 56 57 58 59 |
|
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
37 38 39 40 41 42 43 44 45 46 47 |
|
sortKey(d, key=None)
staticmethod
get the sort key for the given dict d with the given key
Source code in lodstorage/lod.py
65 66 67 68 69 70 71 72 |
|
lod_csv
CSV
Bases: LOD
helper for converting data in csv format to list of dicts (LoD) and vice versa
Source code in lodstorage/lod_csv.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 |
|
fixTypes(lod)
staticmethod
fixes the types of the given LoD.
Source code in lodstorage/lod_csv.py
145 146 147 148 149 150 151 152 153 154 155 |
|
fromCSV(csvString, fields=None, delimiter=',', quoting=csv.QUOTE_NONNUMERIC, **kwargs)
staticmethod
convert given csv string to list of dicts (LOD)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csvStr(str) |
csv string that should be converted to LOD |
required | |
headerNames(list) |
Names of the headers that should be used. If None it is assumed that the header is given. |
required |
Returns:
Type | Description |
---|---|
list of dicts (LoD) containing the content of the given csv string |
Source code in lodstorage/lod_csv.py
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 |
|
readFile(filename)
staticmethod
Reads the given filename and returns it as string Args: filename: Name of the file that should be returned as string
Returns:
Type | Description |
---|---|
str
|
Content of the file as string |
Source code in lodstorage/lod_csv.py
118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
restoreFromCSVFile(filePath, headerNames=None, withPostfix=False)
staticmethod
restore LOD from given csv file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filePath(str) |
file name |
required | |
headerNames(list) |
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 of dicts (LoD) containing the content of the given csv file |
Source code in lodstorage/lod_csv.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
storeToCSVFile(lod, filePath, withPostfix=False)
staticmethod
converts the given lod to CSV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lod(list) |
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 |
Returns: csv string of the given lod
Source code in lodstorage/lod_csv.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
toCSV(lod, includeFields=None, excludeFields=None, delimiter=',', quoting=csv.QUOTE_NONNUMERIC, **kwargs)
staticmethod
converts the given lod to CSV string. For details about the csv dialect parameters see https://docs.python.org/3/library/csv.html#csv-fmt-params
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lod(list) |
lod that should be converted to csv string |
required | |
includeFields(list) |
list of fields that should be included in the csv (positive list) |
required | |
excludeFields(list) |
list of fields that should be excluded from the csv (negative list) |
required | |
kwargs |
csv dialect parameters |
{}
|
Returns: csv string of the given lod
Source code in lodstorage/lod_csv.py
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 |
|
writeFile(content, filename)
staticmethod
Write the given str to the given filename Args: content(str): string that should be written into the file filename: Name of the file the given str should be written to Returns: Nothing
Source code in lodstorage/lod_csv.py
132 133 134 135 136 137 138 139 140 141 142 143 |
|
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
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 |
|
__init__(wikiTable=True, colFormats=None, sortable=True, withNewLines=False)
Constructor
Source code in lodstorage/mwTable.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 |
|
addHeader(record)
add the given record as a "sample" header
Source code in lodstorage/mwTable.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
asWikiMarkup()
convert me to MediaWiki markup
Returns:
Name | Type | Description |
---|---|---|
string |
the MediWiki Markup for this table |
Source code in lodstorage/mwTable.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
params
Created on 2024-05-06
@author: wf
Params
parameter handling
Source code in lodstorage/params.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 |
|
__init__(query, illegal_chars='"[;<>&|]"\'')
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 |
'"[;<>&|]"\''
|
Source code in lodstorage/params.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
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
51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
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
37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
set(params_dict)
set my params
Source code in lodstorage/params.py
31 32 33 34 35 |
|
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
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 |
|
__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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
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
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 |
|
__init__(valueList, title, xlabel=None, ylabel=None, gformat='.png', fontsize=12, plotdir=None, debug=False)
Constructor
Source code in lodstorage/plot.py
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 |
|
barchart(mode='show')
barchart based histogram for the given counter
Source code in lodstorage/plot.py
65 66 67 68 69 70 71 72 73 74 |
|
hist(mode='show')
create histogram for the given counter
Source code in lodstorage/plot.py
82 83 84 85 86 87 88 89 90 |
|
showMe(mode='show', close=True)
show me in the given mode
Source code in lodstorage/plot.py
56 57 58 59 60 61 62 63 |
|
titleMe()
set my title and labels
Source code in lodstorage/plot.py
48 49 50 51 52 53 54 |
|
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
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 |
|
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
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 |
|
profiler
Created on 2022-11-18
@author: wf
Profiler
simple profiler
Source code in lodstorage/profiler.py
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 |
|
__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
14 15 16 17 18 19 20 21 22 23 24 25 |
|
start()
start profiling
Source code in lodstorage/profiler.py
27 28 29 30 31 32 33 |
|
time(extraMsg='')
time the action and print if profile is active
Source code in lodstorage/profiler.py
35 36 37 38 39 40 41 42 |
|
query
Created on 2020-08-22
@author: wf
Endpoint
Bases: JSONAble
a query endpoint
Source code in lodstorage/query.py
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 |
|
__init__()
constructor for setting defaults
Source code in lodstorage/query.py
746 747 748 749 750 751 |
|
__str__()
Returns:
Name | Type | Description |
---|---|---|
str |
a string representation of this Endpoint |
Source code in lodstorage/query.py
753 754 755 756 757 758 759 |
|
EndpointManager
Bases: object
manages a set of SPARQL endpoints
Source code in lodstorage/query.py
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 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 |
|
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
698 699 700 701 702 703 704 705 706 707 708 |
|
getEndpoints(endpointPath=None, lang=None, with_default=True)
staticmethod
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
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
|
Format
Bases: Enum
the supported formats for the results to be delivered
Source code in lodstorage/query.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
Query
Bases: object
a Query e.g. for SPAQRL
Source code in lodstorage/query.py
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 |
|
__init__(name, query, lang='sparql', endpoint=None, database='blazegraph', title=None, description=None, limit=None, prefixes=None, tryItUrl=None, formats=None, debug=False)
constructor Args: name(string): the name/label of the query query(string): the native Query text e.g. in SPARQL lang(string): the language of the query e.g. SPARQL endpoint(string): the endpoint url to use database(string): the type of database e.g. "blazegraph" title(string): the header/title of the query description(string): the description of the query limit(int): the limit of the query default: None prefixes(list): list of prefixes to be resolved tryItUrl(str): the url of a "tryit" webpage formats(list): key,value pairs of ValueFormatters to be applied debug(boolean): true if debug mode should be switched on
Source code in lodstorage/query.py
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 |
|
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
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
|
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
465 466 467 468 469 470 471 472 473 474 |
|
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
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 |
|
formatWithValueFormatters(lod, tablefmt)
format the given list of Dicts with the ValueFormatters
Source code in lodstorage/query.py
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 |
|
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
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
|
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
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|
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
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
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
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 |
|
QueryManager
Bases: object
manages pre packaged Queries
Source code in lodstorage/query.py
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 652 653 654 655 656 657 658 659 660 661 |
|
__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
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 |
|
getQueries(queriesPath=None, with_default=True)
staticmethod
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
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 |
|
QueryResultDocumentation
documentation of a query result
Source code in lodstorage/query.py
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 |
|
__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
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 |
|
__str__()
simple string representation
Source code in lodstorage/query.py
266 267 268 269 270 |
|
asText()
return my text representation
Returns:
Name | Type | Description |
---|---|---|
str |
description, sourceCodeHeader, sourceCode, tryIt link and result table |
Source code in lodstorage/query.py
272 273 274 275 276 277 278 279 280 281 282 283 |
|
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
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
QuerySyntaxHighlight
Syntax highlighting for queries with pygments
Source code in lodstorage/query.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 |
|
__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
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
highlight()
Returns:
Name | Type | Description |
---|---|---|
str |
the result of the syntax highlighting with pygments |
Source code in lodstorage/query.py
193 194 195 196 197 198 199 |
|
ValueFormatter
a value Formatter
Source code in lodstorage/query.py
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 |
|
__init__(name, formatString, regexps=None)
constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fstring(str) |
the format String to use |
required | |
regexps(list) |
the regular expressions to apply |
required |
Source code in lodstorage/query.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
applyFormat(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
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 |
|
fromDict(name, record)
classmethod
create a ValueFormatter from the given dict
Source code in lodstorage/query.py
98 99 100 101 102 103 104 105 106 107 108 |
|
getFormats(formatsPath=None)
classmethod
get the available ValueFormatters
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatsPath(str) |
the path to the yaml file to read the format specs from |
required |
Returns: dict: a map for ValueFormatters by formatter Name
Source code in lodstorage/query.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
YamlPath
Source code in lodstorage/query.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
getPaths(yamlFileName, yamlPath=None, with_default=True)
staticmethod
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yamlFileName |
str
|
The name of the YAML file to read from if (any) - legacy way to specify name |
required |
yamlPath |
str
|
The full path to read from. Defaults to None. |
None
|
with_default |
bool
|
Whether to include paths from the default location .pylodstorage in the Home directory. Defaults to True. |
True
|
Source code in lodstorage/query.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
querymain
Created on 2022-02-13
@author: wf
QueryMain
Commandline handler
Source code in lodstorage/querymain.py
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 |
|
main(args)
classmethod
command line activation with parsed args
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args(list) |
the command line arguments |
required |
Source code in lodstorage/querymain.py
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 |
|
rawQuery(endpointConf, query, resultFormat, mimeType, timeout=10.0)
staticmethod
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 | |
timoeout(float) |
timeout in seconds |
required |
Returns:
Type | Description |
---|---|
raw result of the query |
Source code in lodstorage/querymain.py
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 |
|
main(argv=None, lang=None)
main program.
commandline access to List of Dicts / Linked Open Data Queries
Source code in lodstorage/querymain.py
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 |
|
mainSPARQL(argv=None)
commandline for SPARQL queries
Source code in lodstorage/querymain.py
201 202 203 204 205 |
|
mainSQL(argv=None)
commandline for SQL queries
Source code in lodstorage/querymain.py
194 195 196 197 198 |
|
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
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 |
|
__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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
convert_to_rdf()
Converts the provided instance into RDF triples based on the LinkML schema.
Source code in lodstorage/rdf.py
36 37 38 39 40 41 42 43 |
|
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
126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
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
45 46 47 48 49 50 51 52 53 54 55 |
|
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
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
sample
Created on 2020-08-24
@author: wf
Royal
Bases: JSONAble
i am a single Royal
Source code in lodstorage/sample.py
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 |
|
Royals
Bases: JSONAbleList
a non ORM Royals list
Source code in lodstorage/sample.py
76 77 78 79 80 81 82 83 84 85 86 |
|
Sample
Bases: object
Sample dataset generator
Source code in lodstorage/sample.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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
__init__()
Constructor
Source code in lodstorage/sample.py
20 21 22 23 |
|
dob(isoDateString)
staticmethod
get the date of birth from the given iso date state
Source code in lodstorage/sample.py
52 53 54 55 56 57 58 59 |
|
getCities()
staticmethod
get a list of cities
Source code in lodstorage/sample.py
39 40 41 42 43 44 45 46 47 48 49 50 |
|
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
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 |
|
get_countries_erdem()
classmethod
get Erdem Ozkol's country list
Source code in lodstorage/sample2.py
182 183 184 185 186 187 188 189 190 191 192 |
|
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
194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
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
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 |
|
born: date
property
Return the date of birth from the ISO date string.
died: Optional[date]
property
Return the date of death from the ISO date string, if available.
identifier: str
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
43 44 45 46 47 48 49 50 51 52 53 |
|
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
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 |
|
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
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 |
|
Sample
Sample dataset provider
Source code in lodstorage/sample2.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
get(dataset_name)
staticmethod
Get the given sample dataset name
Source code in lodstorage/sample2.py
214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
schema
Created on 2021-01-26
@author: wf
Schema
Bases: object
a relational Schema
Source code in lodstorage/schema.py
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 |
|
__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
37 38 39 40 41 42 43 44 45 46 47 |
|
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
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 |
|
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 |
---|---|
at table dict for the generalized table |
Source code in lodstorage/schema.py
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 |
|
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 |
required |
Source code in lodstorage/schema.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
SchemaManager
Bases: object
a manager for schemas
Source code in lodstorage/schema.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
__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
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
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
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 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 |
|
__init__(url, mode='query', debug=False, isFuseki=False, typedLiterals=False, profile=False, agent='PyLodStorage', method='POST')
Constructor 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' |
required | |
debug(bool) |
True if debugging is to be activated |
required | |
typedLiterals(bool) |
True if INSERT should be done with typedLiterals |
required | |
profile(boolean) |
True if profiling / timing information should be displayed |
required | |
agent(string) |
the User agent to use |
required | |
method(string) |
the HTTP method to be used 'POST' or 'GET' |
required |
Source code in lodstorage/sparql.py
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 |
|
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
85 86 87 88 89 90 91 92 93 94 95 96 |
|
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
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 |
|
controlEscape(s)
staticmethod
escape control characters
see https://stackoverflow.com/a/9778992/1497139
Source code in lodstorage/sparql.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
|
fix_comments(query_string)
make sure broken SPARQLWrapper will find comments
Source code in lodstorage/sparql.py
114 115 116 117 118 119 120 |
|
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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
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
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
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
212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
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
176 177 178 179 180 181 182 183 184 185 186 |
|
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
122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
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
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
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
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 |
|
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
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 |
|
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
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
|
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
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
|
queryAsListOfDicts(queryString, fixNone=False, sampleCount=None)
get a list of dicts for the given query (to allow round-trip results for insertListOfDicts)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
queryString(string) |
the SPARQL query to execute |
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 ofDicts |
Source code in lodstorage/sparql.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
|
rawQuery(queryString, method=POST)
query with the given query string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
queryString(string) |
the SPARQL query to be performed |
required | |
method(string) |
POST or GET - POST is mandatory for update queries |
required |
Returns: list: the raw query result as bindings
Source code in lodstorage/sparql.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
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
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
|
sql
Created on 2020-08-24
@author: wf
DatetimeAdapter
Singleton class for converting date and time formats with optional lenient error handling.
Attributes:
Name | Type | Description |
---|---|---|
lenient |
bool
|
Whether to handle conversion errors leniently, returning None and logging a warning. |
Source code in lodstorage/sql.py
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 791 792 793 794 795 796 797 798 799 800 801 |
|
__new__(lenient=False)
Ensure only one instance of the adapter exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lenient |
bool
|
If True, the adapter will not raise exceptions on conversion failures. |
False
|
Returns:
Name | Type | Description |
---|---|---|
DatetimeAdapter |
The singleton instance of the adapter. |
Source code in lodstorage/sql.py
693 694 695 696 697 698 699 700 701 702 703 704 705 |
|
convert_date(val)
Convert ISO 8601 date byte string to a datetime.date object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
bytes
|
The ISO 8601 date string in bytes. |
required |
Returns:
Type | Description |
---|---|
date
|
datetime.date: The converted date object. |
Source code in lodstorage/sql.py
742 743 744 745 746 747 748 749 750 751 752 753 754 755 |
|
convert_datetime(val)
Convert ISO 8601 datetime byte string to a datetime.datetime object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
bytes
|
The ISO 8601 datetime string in bytes. |
required |
Returns:
Type | Description |
---|---|
datetime
|
datetime.datetime: The converted datetime object. |
Source code in lodstorage/sql.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
|
convert_timestamp(val)
Convert Unix epoch timestamp byte string to a datetime.datetime object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
bytes
|
The Unix epoch timestamp in bytes. |
required |
Returns:
Type | Description |
---|---|
datetime
|
datetime.datetime: The converted datetime object. |
Source code in lodstorage/sql.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
|
set_lenient(lenient)
Set the lenient mode of the adapter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lenient |
bool
|
True to enable lenient mode, False to disable it. |
required |
Source code in lodstorage/sql.py
795 796 797 798 799 800 801 |
|
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
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 650 651 652 653 |
|
__init__(sampleRecords, name, primaryKey=None, debug=False)
construct me from the given name and primary key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name(string) |
the name of the entity |
required | |
primaryKey(string) |
the name of the primary key column |
required | |
debug(boolean) |
True if debug information should be shown |
required |
Source code in lodstorage/sql.py
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
|
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
627 628 629 630 631 632 633 634 635 636 637 638 |
|
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
640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
|
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
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 |
|
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
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
|
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
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 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 |
|
__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
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 |
|
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
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 |
|
close()
close my connection
Source code in lodstorage/sql.py
73 74 75 |
|
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
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
|
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
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 |
|
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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
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
77 78 79 80 81 82 83 84 |
|
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
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 |
|
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
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
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
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
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
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
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
64 65 66 67 68 69 70 71 |
|
progress(action, status, remaining, total)
show progress
Source code in lodstorage/sql.py
344 345 346 347 348 349 350 351 352 353 354 355 |
|
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
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
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
286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
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
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 |
|
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
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
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
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
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
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 |
|
adapt_boolean(val)
Adapt boolean to int
Source code in lodstorage/sql.py
672 673 674 |
|
adapt_date_iso(val)
Adapt datetime.date to ISO 8601 date.
Source code in lodstorage/sql.py
657 658 659 |
|
adapt_datetime_epoch(val)
Adapt datetime.datetime to Unix timestamp.
Source code in lodstorage/sql.py
667 668 669 |
|
adapt_datetime_iso(val)
Adapt datetime.datetime to timezone-naive ISO 8601 date.
Source code in lodstorage/sql.py
662 663 664 |
|
convert_boolean(val)
Convert 0 or 1 to boolean
Source code in lodstorage/sql.py
823 824 825 826 827 |
|
convert_date(val)
Convert byte string to date using the DatetimeAdapter.
Source code in lodstorage/sql.py
805 806 807 808 |
|
convert_datetime(val)
Convert byte string to datetime using the DatetimeAdapter.
Source code in lodstorage/sql.py
811 812 813 814 |
|
convert_timestamp(val)
Convert byte string to timestamp using the DatetimeAdapter.
Source code in lodstorage/sql.py
817 818 819 820 |
|
sql_cache
Created on 2024-03-16
@author: wf
Cached
Manage cached entities.
Source code in lodstorage/sql_cache.py
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 |
|
__init__(clazz, sparql, sql_db, query_name, max_errors=0, debug=False)
Initializes the Manager with class reference, SPARQL endpoint URL, SQL database connection string, query name, and an optional debug flag.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clazz |
Type[Any]
|
The class reference for the type of objects managed by this manager. |
required |
sparql |
SPARQL
|
a SPARQL endpoint. |
required |
sql_db |
str
|
The connection string for the SQL database. |
required |
query_name |
str
|
The name of the query to be executed. |
required |
debug |
bool
|
Flag to enable debug mode. Defaults to False. |
False
|
Source code in lodstorage/sql_cache.py
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 |
|
check_local_cache()
Checks if there is data in the local cache (SQL database).
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if there is at least one record in the local SQL cache table |
Source code in lodstorage/sql_cache.py
92 93 94 95 96 97 98 99 100 101 |
|
fetch_from_local()
Fetches data from the local SQL database as list of dicts and entities.
Returns:
Type | Description |
---|---|
List[Dict]
|
List[Dict]: List of records from the SQL database in dictionary form. |
Source code in lodstorage/sql_cache.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
fetch_or_query(qm, force_query=False)
Fetches data from the local cache if available. If the data is not in the cache or if force_query is True, it queries via SPARQL and caches the results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qm |
QueryManager
|
The query manager object used for making SPARQL queries. |
required |
force_query |
bool
|
A flag to force querying via SPARQL even if the data exists in the local cache. Defaults to False. |
False
|
Returns: List: list of records from the SQL database
Source code in lodstorage/sql_cache.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
get_lod(qm)
Fetches data using the SPARQL query specified by my query_name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qm |
QueryManager
|
The query manager object used for making SPARQL queries. |
required |
Returns:
Type | Description |
---|---|
List[Dict]
|
List[Dict]: A list of dictionaries representing the data fetched. |
Source code in lodstorage/sql_cache.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
store(max_errors=None)
Stores the fetched data into the local SQL database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_errors |
int
|
Maximum allowed validation errors. Defaults to 0. |
None
|
Returns:
Type | Description |
---|---|
List[Any]
|
List[Any]: A list of entity instances that were stored in the database. |
Source code in lodstorage/sql_cache.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
to_entities(max_errors=None, cached=True)
Converts records fetched from the LOD into entity instances, applying validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_errors |
int
|
Maximum allowed validation errors. Defaults to 0. |
None
|
cached(bool) |
if True use existing entries |
required |
Returns: List[Any]: A list of entity instances that have passed validation.
Source code in lodstorage/sql_cache.py
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 |
|
SqlDB
general SQL database access using SQL Alchemy
Source code in lodstorage/sql_cache.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
get_session()
Provide a session for database operations.
Returns:
Name | Type | Description |
---|---|---|
Session |
Session
|
A SQLAlchemy Session object bound to the engine for database operations. |
Source code in lodstorage/sql_cache.py
26 27 28 29 30 31 32 33 |
|
storageconfig
Created on 2020-08-29
@author: wf
StorageConfig
Bases: object
a storage configuration
Source code in lodstorage/storageconfig.py
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 |
|
__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
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 |
|
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
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
StoreMode
Bases: Enum
possible supported storage modes
Source code in lodstorage/storageconfig.py
11 12 13 14 15 16 17 18 19 20 |
|
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
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 |
|
__init__(counter)
Constructor
Source code in lodstorage/tabulateCounter.py
14 15 16 17 18 |
|
mostCommonTable(headers=['#', 'key', 'count', '%'], tablefmt='pretty', limit=50)
get the most common Table
Source code in lodstorage/tabulateCounter.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
uml
Created on 2020-09-04
@author: wf
UML
Bases: object
UML diagrams via plantuml
Source code in lodstorage/uml.py
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 219 220 221 222 223 224 225 226 |
|
__init__(debug=False)
Constructor Args: debug(boolean): True if debug information should be shown
Source code in lodstorage/uml.py
96 97 98 99 100 101 102 |
|
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
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 |
|
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
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 |
|
version
Created on 2022-03-06
@author: wf
Version
Bases: object
Version handling for pyLoDStorage
Source code in lodstorage/version.py
9 10 11 12 13 14 15 16 17 18 |
|
xml
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/xml.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 |
|
__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/xml.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
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/xml.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
yamlable
Created on 2023-12-08, Extended on 2023-16-12 and 2024-01-25
@author: wf, ChatGPT
Prompts for the development and extension of the 'YamlAble' class within the 'yamable' module:
- Develop 'YamlAble' class in 'yamable' module. It should convert dataclass instances to/from YAML.
- Implement methods for YAML block scalar style and exclude None values in 'YamlAble' class.
- Add functionality to remove None values from dataclass instances before YAML conversion.
- Ensure 'YamlAble' processes only dataclass instances, with error handling for non-dataclass objects.
- Extend 'YamlAble' for JSON serialization and deserialization.
- Add methods for saving/loading dataclass instances to/from YAML and JSON files in 'YamlAble'.
- Implement loading of dataclass instances from URLs for both YAML and JSON in 'YamlAble'.
- Write tests for 'YamlAble' within the pyLodStorage context. Use 'samples 2' example from pyLoDStorage https://github.com/WolfgangFahl/pyLoDStorage/blob/master/lodstorage/sample2.py as a reference.
- Ensure tests cover YAML/JSON serialization, deserialization, and file I/O operations, using the sample-based approach..
- Use Google-style docstrings, comments, and type hints in 'YamlAble' class and tests.
- Adhere to instructions and seek clarification for any uncertainties.
- Add @lod_storable annotation support that will automatically YamlAble support and add @dataclass and @dataclass_json prerequisite behavior to a class
DateConvert
date converter
Source code in lodstorage/yamlable.py
76 77 78 79 80 81 82 83 84 |
|
YamlAble
Bases: Generic[T]
An extended YAML handler class for converting dataclass objects to and from YAML format, and handling loading from and saving to files and URLs.
Source code in lodstorage/yamlable.py
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 |
|
from_dict2(data)
classmethod
Creates an instance of a dataclass from a dictionary, typically used in deserialization.
Source code in lodstorage/yamlable.py
318 319 320 321 322 323 324 325 326 |
|
from_yaml(yaml_str)
classmethod
Deserializes a YAML string to a dataclass instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_str |
str
|
A string containing YAML formatted data. |
required |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in lodstorage/yamlable.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
load_from_json_file(filename)
classmethod
Loads a dataclass instance from a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
The path to the JSON file. |
required |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in lodstorage/yamlable.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
load_from_json_url(url)
classmethod
Loads a dataclass instance from a JSON string obtained from a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The URL pointing to the JSON data. |
required |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in lodstorage/yamlable.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
load_from_yaml_file(filename)
classmethod
Loads a dataclass instance from a YAML file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
The path to the YAML file. |
required |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in lodstorage/yamlable.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
load_from_yaml_url(url)
classmethod
Loads a dataclass instance from a YAML string obtained from a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The URL pointing to the YAML data. |
required |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in lodstorage/yamlable.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
read_from_url(url)
classmethod
Helper method to fetch content from a URL.
Source code in lodstorage/yamlable.py
253 254 255 256 257 258 259 260 261 262 |
|
remove_ignored_values(value, ignore_none=True, ignore_underscore=False, ignore_empty=True)
classmethod
Recursively removes specified types of values from a dictionary or list. By default, it removes keys with None values. Optionally, it can also remove keys starting with an underscore.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any
|
The value to process (dictionary, list, or other). |
required |
ignore_none |
bool
|
Flag to indicate whether None values should be removed. |
True
|
ignore_underscore |
bool
|
Flag to indicate whether keys starting with an underscore should be removed. |
False
|
ignore_empty |
bool
|
Flag to indicate whether empty collections should be removed. |
True
|
Source code in lodstorage/yamlable.py
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 |
|
represent_literal(dumper, data)
Custom representer for block scalar style for strings.
Source code in lodstorage/yamlable.py
111 112 113 114 115 116 117 |
|
represent_none(_, __)
Custom representer for ignoring None values in the YAML output.
Source code in lodstorage/yamlable.py
105 106 107 108 109 |
|
save_to_json_file(filename, **kwargs)
Saves the current dataclass instance to a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
The path where the JSON file will be saved. |
required |
**kwargs |
Additional keyword arguments for the |
{}
|
Source code in lodstorage/yamlable.py
241 242 243 244 245 246 247 248 249 250 251 |
|
save_to_yaml_file(filename)
Saves the current dataclass instance to a YAML file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
The path where the YAML file will be saved. |
required |
Source code in lodstorage/yamlable.py
199 200 201 202 203 204 205 206 207 208 |
|
to_yaml(ignore_none=True, ignore_underscore=True, allow_unicode=True, sort_keys=False)
Converts this dataclass object to a YAML string, with options to omit None values and/or underscore-prefixed variables, and using block scalar style for strings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ignore_none |
bool
|
Flag to indicate whether None values should be removed from the YAML output. |
True
|
ignore_underscore |
bool
|
Flag to indicate whether attributes starting with an underscore should be excluded from the YAML output. |
True
|
allow_unicode |
bool
|
Flag to indicate whether to allow unicode characters in the output. |
True
|
sort_keys |
bool
|
Flag to indicate whether to sort the dictionary keys in the output. |
False
|
Returns:
Type | Description |
---|---|
str
|
A string representation of the dataclass object in YAML format. |
Source code in lodstorage/yamlable.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 147 148 149 150 151 |
|
lod_storable(cls)
Decorator to make a class LoDStorable by inheriting from YamlAble. This decorator also ensures the class is a dataclass and has JSON serialization/deserialization capabilities.
Source code in lodstorage/yamlable.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
yamlablemixin
YamlAbleMixin
Bases: object
allow reading and writing derived objects from a yaml file
Source code in lodstorage/yamlablemixin.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 |
|