pylodentitymanager 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 lodentity/cache.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 | |
is_stored
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 lodentity/cache.py
37 38 39 40 41 42 43 44 45 46 | |
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 lodentity/cache.py
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 | |
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 lodentity/cache.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
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 lodentity/cache.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
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 lodentity/cache.py
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 | |
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 lodentity/cache.py
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 | |
entity
Created on 2020-08-19.
@author: wf
EntityManager
Generic entity manager.
Source code in lodentity/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 | |
__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 lodentity/entity.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
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 lodentity/entity.py
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 | |
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 lodentity/entity.py
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 | |
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 lodentity/entity.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
getLoD()
Return the LoD of the entities in the list.
Return
list: a list of Dicts
Source code in lodentity/entity.py
341 342 343 344 345 346 347 348 349 350 351 | |
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 lodentity/entity.py
130 131 132 133 134 135 136 137 138 139 140 | |
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 lodentity/entity.py
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 | |
isCached()
Check whether there is a file containing cached data for me.
Source code in lodentity/entity.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
removeCacheFile()
Remove my cache file.
Source code in lodentity/entity.py
122 123 124 125 126 127 128 | |
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 lodentity/entity.py
172 173 174 175 176 177 178 179 | |
showProgress(msg)
Display a progress message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg(string)
|
the message to display |
required |
Source code in lodentity/entity.py
89 90 91 92 93 94 95 96 | |
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 lodentity/entity.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 | |
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 lodentity/entity.py
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 | |
storeMode()
Return my store mode.
Source code in lodentity/entity.py
85 86 87 | |
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 lodentity/jsonable.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 | |
__init__()
Constructor.
Source code in lodentity/jsonable.py
37 38 | |
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 lodentity/jsonable.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
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 lodentity/jsonable.py
163 164 165 166 167 168 169 170 171 172 173 174 | |
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 lodentity/jsonable.py
208 209 210 211 212 213 214 215 216 217 | |
fromJson(jsonStr)
Initialize me from the given JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jsonStr(str)
|
the JSON string |
required |
Source code in lodentity/jsonable.py
199 200 201 202 203 204 205 206 | |
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 lodentity/jsonable.py
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 | |
getJsonTypeSamples()
Does my class provide a "getSamples" method?
Source code in lodentity/jsonable.py
116 117 118 119 120 121 122 123 | |
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 lodentity/jsonable.py
125 126 127 128 129 130 131 132 133 134 135 136 | |
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 lodentity/jsonable.py
138 139 140 141 142 143 144 145 146 147 148 149 150 | |
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 lodentity/jsonable.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
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 lodentity/jsonable.py
189 190 191 192 193 194 195 196 197 | |
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 lodentity/jsonable.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
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 lodentity/jsonable.py
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 | |
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 lodentity/jsonable.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
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 lodentity/jsonable.py
152 153 154 155 156 157 158 159 160 161 | |
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 lodentity/jsonable.py
176 177 178 179 180 181 182 183 184 185 186 187 | |
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 lodentity/jsonable.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 | |
toJsonAbleValue(v)
return the JSON able value of the given value v Args: v(object): the value to convert
Source code in lodentity/jsonable.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
JSONAbleList
Bases: JSONAble
Container class.
Source code in lodentity/jsonable.py
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 | |
__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 lodentity/jsonable.py
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 | |
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 lodentity/jsonable.py
472 473 474 475 476 477 478 479 480 | |
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 lodentity/jsonable.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 | |
getJsonData()
Get my Jsondata.
Source code in lodentity/jsonable.py
459 460 461 462 | |
getList()
Get my list.
Source code in lodentity/jsonable.py
368 369 370 | |
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 lodentity/jsonable.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
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 lodentity/jsonable.py
447 448 449 450 451 452 453 454 455 456 457 | |
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 lodentity/jsonable.py
500 501 502 503 504 505 506 507 508 509 510 511 512 | |
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 lodentity/jsonable.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | |
restoreFromJsonFile(jsonFile)
Read my list of dicts and restore it.
Source code in lodentity/jsonable.py
486 487 488 489 | |
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 lodentity/jsonable.py
491 492 493 494 495 496 497 498 | |
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 lodentity/jsonable.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
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 lodentity/jsonable.py
464 465 466 467 468 469 470 | |
JSONAbleSettings
settings for JSONAble - put in a separate class so they would not be serialized
Source code in lodentity/jsonable.py
17 18 19 20 21 22 23 24 25 26 27 28 | |
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 lodentity/jsonable.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 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 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 | |
__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 lodentity/jsonable.py
552 553 554 555 556 557 558 559 560 561 562 563 | |
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 lodentity/jsonable.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 | |
fixListOfDicts(typeMap, listOfDicts)
Fix the type in the given list of Dicts.
Source code in lodentity/jsonable.py
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 | |
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 lodentity/jsonable.py
652 653 654 655 656 657 658 659 660 | |
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 lodentity/jsonable.py
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
getType(typeName)
Get the type for the given type name.
Source code in lodentity/jsonable.py
662 663 664 665 666 667 668 669 | |
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 lodentity/jsonable.py
602 603 604 605 606 607 608 609 610 611 612 | |
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 lodentity/jsonable.py
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 | |
jsonpicklemixin
JsonPickleMixin
Bases: object
Allow reading and writing derived objects from a jsonpickle file.
Source code in lodentity/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 | |
asJsonPickle()
Convert me to JSON.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
a JSON String with my JSON representation |
Source code in lodentity/jsonpicklemixin.py
49 50 51 52 53 54 55 56 | |
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 lodentity/jsonpicklemixin.py
12 13 14 15 16 17 18 19 20 21 22 23 24 | |
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 lodentity/jsonpicklemixin.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
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 lodentity/jsonpicklemixin.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |