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: object

An object that can store any pickable data. It acts like a small key/value database.

  • Keys are of _DataStoreEntryKey class. 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 kind and extras exists 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_payload is not None, a new entry is added to the DataStore using the default_payload and readonly arguments.

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 kind and extras.

Note:

When matching extras, supernumerary attributes are ignored (e.g. extras=dict(a=1) will match dict(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 kind and extras and 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 match dict(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 payload data in the current DataStore.

Parameters:
  • kind (object) – The kind of the payload data

  • extras (dict) – Any key/value pairs that describe the payload data

  • payload (object) – The data that will be stored

  • readonly (bool) – Is the data readonly ?

keys()[source]

Return the list of available keys in this DataStore.

pickle_dump(dumpfile=None)[source]

Pickle the content of the current DataStore and write it to disk.

Parameters:

dumpfile (str) – Path to the dump file (if None, the default provided at the object creation time is used).

pickle_load(dumpfile=None)[source]

Read a pickle dump file from disk and refill the current DataStore.

Parameters:

dumpfile (str) – Path to the dump file (if None, the default provided at the object creation time is used).