dol.mixins

Mixins

class dol.mixins.FilteredKeysMixin[source]

Filters __iter__ and __contains__ with (the boolean filter function attribute) _key_filt.

class dol.mixins.IdentityKeysWrapMixin[source]

Transparent 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]

Transparent Keys and Vals Wrap

class dol.mixins.IdentityValsWrapMixin[source]

Transparent 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]
An ABC that defines
  1. how to iterate over a collection of elements (keys) (__iter__)

  2. check that a key is contained in the collection (__contains__), and

  3. how to get the number of elements in the collection

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]

Mixin 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]

Put this as your first parent class to disallow write/delete operations

class dol.mixins.SimpleJsonMixin[source]

simple json serialization. Useful to store and retrieve

class dol.mixins.StringKvWrap[source]