ID-generating decorators¶
In cases where you want to generate IDs automatically, decorators are available.
These should be the outermost decorators, as they change the signature of some of the put methods slightly.
>>> from minimalkv.memory import DictStore
>>> from minimalkv.idgen import HashDecorator
>>>
>>> store = HashDecorator(DictStore())
>>>
>>> key = store.put(None, b'my_data') # note the passing of 'None' as key
>>> print(key)
ab0c15b6029fdffce16b393f2d27ca839a76249e
- class minimalkv.idgen.HashDecorator(decorated_store, hashfunc=<built-in function openssl_sha1>, template='{}')¶
Hash function decorator.
Overwrites
KeyValueStore.put()
andKeyValueStore.put_file()
.Parameters¶
- decorated_storeKeyValueStore
Store.
- hashfuncCallable, optional, default = hashlib.sha1
Function used for hashing.
- templatestr, optional, default = u”{}”
Template to format hashes.
- put(key: str | None, data: bytes, *args, **kwargs)¶
Store bytestring data at key.
Parameters¶
- keystr or None
The key under which the data is to be stored. If None, the hash of data is used.
- databytes
Data to be stored at key, must be of type
bytes
.
Returns¶
- str
The key under which data was stored.
Raises¶
- ValueError
If the key is not valid.
- IOError
If storing failed or the file could not be read.
- put_file(key: str | None, file: str | BinaryIO, *args, **kwargs)¶
Store contents of file at key.
Store data from a file into key.
file
can be a string, which will be interpreted as a filename, or an object with aread()
method.If
file
is a filename, the file might be removed while storing to avoid unnecessary copies. To prevent this, pass the opened file instead.Parameters¶
- keystr or None
Key where to store data in file. If None, the hash of data is used.
- fileBinaryIO or str
A filename or a file-like object with a read method.
Returns¶
- key: str
The key under which data was stored.
Raises¶
- ValueError
If the key is not valid.
- IOError
If there was a problem moving the file in.
- class minimalkv.idgen.UUIDDecorator(store, template='{}')¶
UUID generating decorator.
Overrides
KeyValueStore.put()
andKeyValueStore.put_file()
. If key isNone
is passed, a new UUID will be generated as the key. The attributeuuidfunc
determines which UUID-function to use. ‘uuid1’.Parameters¶
- store: KeyValueStore
Store.
- template: str, optional, default = “{}”
Template to format uuids.
- put(key: str | None, data: bytes, *args, **kwargs) str ¶
Store bytestring data at key.
Parameters¶
- keystr or None
The key under which the data is to be stored. If None, a uuid is generated.
- databytes
Data to be stored at key, must be of type
bytes
.
Returns¶
- str
The key under which data was stored.
Raises¶
- ValueError
If the key is not valid.
- IOError
If storing failed or the file could not be read.
- put_file(key: str | None, file: str | BinaryIO, *args, **kwargs)¶
Store contents of file at key.
Store data from a file into key.
file
can be a string, which will be interpreted as a filename, or an object with aread()
method.If
file
is a filename, the file might be removed while storing to avoid unnecessary copies. To prevent this, pass the opened file instead.Parameters¶
- keystr or None
The key under which the data is to be stored. If None, a uuid is generated.
- fileBinaryIO or str
A filename or a file-like object with a read method.
Returns¶
- key: str
The key under which data was stored.
Raises¶
- ValueError
If the key is not valid.
- IOError
If there was a problem moving the file in.