dol.mixins¶
Mixins that add or restrict store behaviours.
Main entry points:
ReadOnlyMixin: forbid writes and deletesOverWritesNotAllowedMixin: forbid writing to an existing keySimpleJsonMixin: JSON-encoded valuesIterBasedSizedContainerMixin:__len__and__contains__from__iter__>>> from dol.mixins import OverWritesNotAllowedMixin >>> class P(OverWritesNotAllowedMixin, dict): ... pass >>> p = P() >>> p['a'] = 1 >>> p['a'] = 2 Traceback (most recent call last): ... dol.errors.OverWritesNotAllowedError: key a already exists and cannot be overwritten...
Classes
Filters __iter__ and __contains__ with (the boolean filter function attribute) _key_filt. |
|
|
|
|
|
Transparent KeysWrapABC. |
|
Transparent Keys and Vals Wrap |
|
Transparent ValsWrapABC. |
|
|
|
An ABC that defines: |
|
|
|
Mixin for only allowing a write to a key if they key doesn't already exist. |
|
Put this as your first parent class to disallow write/delete operations |
|
simple json serialization. |
|
- class dol.mixins.FilteredKeysMixin[source]¶
Bases:
objectFilters __iter__ and __contains__ with (the boolean filter function attribute) _key_filt.
- class dol.mixins.IdentityKeysWrapMixin[source]¶
Bases:
objectTransparent KeysWrapABC. Often placed in the mro to satisfy the KeysWrapABC need in a neutral way. This is useful in cases where the keys the persistence functions work with are the same as those you want to work with.
- class dol.mixins.IdentityKvWrapMixin[source]¶
Bases:
IdentityKeysWrapMixin,IdentityValsWrapMixinTransparent Keys and Vals Wrap
- class dol.mixins.IdentityValsWrapMixin[source]¶
Bases:
objectTransparent ValsWrapABC. Often placed in the mro to satisfy the KeysWrapABC need in a neutral way. This is useful in cases where the values can be persisted by __setitem__ as is (or the serialization is handled somewhere in the __setitem__ method.
- class dol.mixins.IterBasedSizedContainerMixin[source]¶
Bases:
IterBasedSizedMixin,IterBasedContainerMixinAn ABC that defines:
how to iterate over a collection of elements (keys) (
__iter__)check that a key is contained in the collection (
__contains__), andhow to get the number of elements in the collection (
__len__)
This is exactly what the collections.abc.Collection (from which Keys inherits) does. The difference here, besides the “Keys” purpose-explicit name, is that Keys offers default
__len__and__contains__definitions based on what ever__iter__the concrete class defines.Keys is a collection (i.e. a Sized (has __len__), Iterable (has __iter__), Container (has __contains__). It’s purpose is to serve as a collection of object identifiers in a key->obj mapping. The Keys class doesn’t implement __iter__ (so needs to be subclassed with a concrete class), but offers mixin __len__ and __contains__ methods based on a given __iter__ method. Note that usually __len__ and __contains__ should be overridden to more, context-dependent, efficient methods.
- class dol.mixins.OverWritesNotAllowedMixin[source]¶
Bases:
objectMixin for only allowing a write to a key if they key doesn’t already exist.
Note
Should be before the persister in the MRO.
>>> class TestPersister(OverWritesNotAllowedMixin, dict): ... pass >>> p = TestPersister() >>> p['foo'] = 'bar' >>> #p['foo'] = 'bar2' # will raise error >>> p['foo'] = 'this value should not be stored' Traceback (most recent call last): ... dol.errors.OverWritesNotAllowedError: key foo already exists and cannot be overwritten. If you really want to write to that key, delete it before writing >>> p['foo'] # foo is still bar 'bar' >>> del p['foo'] >>> p['foo'] = 'this value WILL be stored' >>> p['foo'] 'this value WILL be stored'
- class dol.mixins.ReadOnlyMixin[source]¶
Bases:
objectPut this as your first parent class to disallow write/delete operations
- class dol.mixins.SimpleJsonMixin[source]¶
Bases:
objectsimple json serialization. Useful to store and retrieve
- class dol.mixins.StringKvWrap[source]¶
Bases:
IdentityKvWrapMixin