A lightweight library to store an object's history into a SQL database
Project description
TSIH - A dict with a HISTory
tsih.Dict
is a type of UserDict
that allows versioning, backed up by a sqlite3
database.
- Transparent operation
- Only changes (deltas) are stored.
- Forward-filling of values. A value is reused in future versions, unless it changes.
- Auto-versioning option (off by default), to produce a new version every time a value change happens.
- Ability to store related entries as separate dictionaries. Each
tsih.Dict
has adict_name
that is used in the database to identify the dictionary. - Tuple-based indexing. Get and set values by
dict_name
,version
andkey
.
Usage and examples
tsih.Dict
objects can be used just like regular dictionaries:
>>> from tsih import Dict
>>> a = Dict()
>>> a['test'] = True
>>> a
{'test': True}
>>> a.get('missing', 5)
5
>>> a['missing']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'missing'
But at any point, new versions can be produced:
>>> a.version
0
>>> a['start'] = 'now'
>>> a
{'test': True, 'start': 'now'}
>>> a.version = 1
>>> a['start'] = 'one version ago'
>>> a
{'test': True, 'start': 'one version ago'}
Previous values can be accessed using tuple keys, i.e., (version, key):
>>> a[(0, 'start')]
'now'
>>> a[(1, 'start')]
'one version ago'
Each version only "records" changes, but later versions (even if they don't exist yet) inherit unchanged values from the previous ones:
>>> a[(5, 'start')]
'one version ago'
>>> a.version = 5
>>> # Until the value is changed
>>> a['start'] = '4 versions ago'
>>> a[(5, 'start')]
'4 versions ago'
You can access every state of the Dict using None
in place of the version and/or the key.
In that case, we will get an iterator, which we can turn into a list explicitly or with the .value
method.
For example, here we get all the changes to the start
key:
>>> a[(None, 'start')].value() #
[(0.0, 'now'), (1.0, 'one version ago'), (5.0, '4 versions ago')]
Similarly, to get the keys and values at a specific version:
>>> list(a[(0, None)])
[('start', 'now'), ('test', True)]
Or, we can combine both to get the keys and values at every version:
>>> a[(None, None)].value()
[(0.0, 'start', 'now'), (1.0, 'start', 'one version ago'), (5.0, 'start', '4 versions ago'), (0.0, 'test', True), (1.0, 'test', True), (5.0, 'test', True)]
Use cases
Tsih was originally part of the Soil Agent-Based Social Simulation framework, where both the environment and the agents need to keep track of state (i.e., attribute) changes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tsih-0.1.9.tar.gz
.
File metadata
- Download URL: tsih-0.1.9.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/5.1.0 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b40481b22d7983957d3c8859a245ab7f64236080dcd9bd8ef255147780657e8b |
|
MD5 | a54b8f9d77a0d2d9bcd12cdd3e7d50b1 |
|
BLAKE2b-256 | 07f08f16d1432f6a43e1a3609e778d99620cea0da90ea13d800c59a59c71737d |
File details
Details for the file tsih-0.1.9-py3-none-any.whl
.
File metadata
- Download URL: tsih-0.1.9-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/5.1.0 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1c4a743c9247493fe0c5d201d078e0a4b1cdce029a691a28accf4877571f9a4 |
|
MD5 | 2ac33a1ad25b7faa1ca1cc7b2deaa6ec |
|
BLAKE2b-256 | 5a061f601e360c3eb05e02ebbb01d292556512f05aa86ef6cbb3dff66db5db98 |