General purpose store for key/value pairs, time series and event sequences with optional encryption.
Project description
General purpose store for key/value pairs, time series and event sequences with optional encryption.
Values are encrypted as soon as the key argument is specified with Store(). Alternatively, the key can be fixed for the store by setting Store._key. The same store can be used in encrypted and unencrypted mode. Note: Keys must be of type ``str`` and are never encrypted.
Different backends are available for persistence. The backend is selected by the dsn argument of Store():
PostgreSQL database: dsn='postgresql://localhost'
Redis server: dsn='redis://localhost'
SQLite data file: dsn='sqlite://datafile.db'
Different namespaces can be used within a single store. The namespace is selected with the namespace argument of Store() or by setting Store._namespace. If no namespace is selected it defaults to default.
Values are stored using the pickle module (before encryption). This behavior can be changed by subclassing Store and overwriting _dump and _load. For example:
class JsonStore(Store):
def _dump(self, value):
return json.dumps(value)
def _load(self, data):
return json.loads(data)
Key/value
Functions for the key/value store include: set, get, delete, has, and keys. The k/v store can also be used in a dict-like fashion with s[k], del s[k], k in s, and len(s).
with Store('file.name', frozen=False) as kv:
kv.set('some_key', value)
value = kv.get('some_key')
del kv['some_key']
Time series
Time series are key/value pairs where the value changes over time. Each value change (or data point) is recorded with a timestamp. The timestamp is an arbitrary int value and its meaning must be determined by the application. An even frequency between the timestamps is optional.
Functions for time series include: range, first, last, missing, iscomplete, extend, remove, and timeseries.
with Store('file.name', frozen=False) as ts:
ts.extend('some_series', (timestamp1, value1), ...)
timestamp, value = ts.last('some_series')
ts.remove('some_series')
Event sequences
Event sequences are key/dict pairs where each change to the dict is recorded as an event. Events are stored as tuple (timestamp, item, op, value) where:
timestamp is the POSIX timestamp in milliseconds when the event was recorded (type int)
item is the name of the dict member (type str)
op is the operator to apply to the dict member (operator.setitem, operator.delitem, operator.add, …)
value is the value to apply to the dict member (or None for unary operators such as operator.delitem)
Functions for event sequences include: current, past, apply, prune, events, and eventsources.
with Store('file.name', frozen=False) as es:
es.apply('some_dict', {'item': 1}) # op defaults to operator.setitem
es.apply('some_dict', {'item': (operator.add, 1)})
es.events('some_dict') == [(timestamp, 'item', operator.setitem, 1), (timestamp, 'item', operator.add, 1)]
es.current('some_dict') == {'item': 2}
es.prune('some_dict', remove=True)
Installation
$ pip install rlib-store
Getting Started
from rstore import Store
Check the doc strings and unit tests for examples.
License
“MIT”. See LICENSE for details. Copyright t5w5h5@gmail.com, 2018.
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
File details
Details for the file rlib-store-0.1.tar.gz
.
File metadata
- Download URL: rlib-store-0.1.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64e35aa3fa33418b24e483463e4b5f8c0d13906a26ef4bd440f9728a65ab99c3 |
|
MD5 | 0e99754a0f028372c395870b46f4a7d8 |
|
BLAKE2b-256 | fc0f0a645d82630560a77bec64672091bec23f3039beb7f4eb69571284f7a80e |