bronx.datagrip.datastore¶
A simplified key/value embedded-database.
See the DataStore class docstring for an example.
Classes¶
- class bronx.datagrip.datastore.DataStore(default_picklefile='datastore.pickled')[source]¶
Bases:
objectAn object that can store any pickable data. It acts like a small key/value database.
Keys are of
_DataStoreEntryKeyclass. They contain a mandatory kind attribute plus key/value pairs that describe the stored data more precisely.Various methods are provided to access the entries.
Keys are indexed in order to perform fast searches (see the grep method).
Data should always be pickalable so that the DataStore could be dumped to disk using the
pickle_dump()method.- Example:
Data should be inserted this way:
ds = DataStore() ds.insert('kind_of_data', dict(key1='meaningful'), 'The data themselves...', readonly=True) ds.insert('kind_of_data', dict(key1='meaningful', key2='breathtaking'), 'More date...', readonly=True) ds.insert('kind_of_data', dict(), 'Another One', readonly=True)
It could later be accessed:
data = ds.get('kind_of_data', dict(key1='meaningful', key2='breathtaking')) print data More date...
A search can be performed:
dict_of_results = ds.grep('kind_of_data', dict(key1='meaningful')) print dict_of_results {<_DataStoreEntryKey object | kind='kind_of_data' key1='meaningful' key2='breathtaking'>: 'More date...', <_DataStoreEntryKey object | kind='kind_of_data' key1='meaningful'>: 'The data themselves...'}
Finally the DataStore can be dumped/loaded to/from disk:
ds.pickle_dump() another_ds = DataStore() another_ds.pickle_load()
- Parameters:
default_picklefile (str) – default name for the pickle dump file
- check(kind, extras)[source]¶
Check if a data described by
kindandextrasexists in this DataStore.- Parameters:
kind (object) – The kind of the expected data
extras (dict) – Any key/value pairs that describe the expected data
- delete(kind, extras, force=False)[source]¶
Delete data from the current DataStore.
- Parameters:
kind (object) – The kind of the expected data
extras (dict) – Any key/value pairs that describe the expected data
- get(kind, extras, default_payload=None, readonly=True)[source]¶
Retrieve data from the current DataStore.
if the desired data is missing and
default_payloadis not None, a new entry is added to the DataStore using thedefault_payloadandreadonlyarguments.- Parameters:
kind (object) – The kind of the expected data
extras (dict) – Any key/value pairs that describe the expected data
default_payload (object) – Default data that may be stored and returned
readonly (bool) – Is the default data readonly ?
- grep(kind, extras)[source]¶
Search for items that matches both
kindandextras.- Note:
When matching
extras, supernumerary attributes are ignored (e.g.extras=dict(a=1)will matchdict(a=1, b=2))- Parameters:
kind (object) – The kind of the expected data
extras (dict) – Any key/value pairs that describe the expected data
- grep_delete(kind, extras, force=False)[source]¶
Search for items that matches both
kindandextrasand delete them.The dictionary of the removed key/data is returned.
- Note:
When matching
extras, supernumerary attributes are ignored (e.g.extras=dict(a=1)will matchdict(a=1, b=2))- Parameters:
kind (object) – The kind of the expected data
extras (dict) – Any key/value pairs that describe the expected data
- insert(kind, extras, payload, readonly=True)[source]¶
Insert a new
payloaddata in the current DataStore.- Parameters:
kind (object) – The kind of the
payloaddataextras (dict) – Any key/value pairs that describe the
payloaddatapayload (object) – The data that will be stored
readonly (bool) – Is the data readonly ?