dol.mixins

Mixins that add or restrict store behaviours.

Main entry points:

  • ReadOnlyMixin: forbid writes and deletes

  • OverWritesNotAllowedMixin: forbid writing to an existing key

  • SimpleJsonMixin: JSON-encoded values

  • IterBasedSizedContainerMixin: __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

FilteredKeysMixin()

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

GetBasedContainerMixin()

HashableMixin()

IdentityKeysWrapMixin()

Transparent KeysWrapABC.

IdentityKvWrapMixin()

Transparent Keys and Vals Wrap

IdentityValsWrapMixin()

Transparent ValsWrapABC.

IterBasedContainerMixin()

IterBasedSizedContainerMixin()

An ABC that defines:

IterBasedSizedMixin()

OverWritesNotAllowedMixin()

Mixin for only allowing a write to a key if they key doesn't already exist.

ReadOnlyMixin()

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

SimpleJsonMixin()

simple json serialization.

StringKvWrap()

class dol.mixins.FilteredKeysMixin[source]

Bases: object

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

class dol.mixins.IdentityKeysWrapMixin[source]

Bases: object

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]

Bases: IdentityKeysWrapMixin, IdentityValsWrapMixin

Transparent Keys and Vals Wrap

class dol.mixins.IdentityValsWrapMixin[source]

Bases: object

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]

Bases: IterBasedSizedMixin, IterBasedContainerMixin

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 (__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: object

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]

Bases: object

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

class dol.mixins.SimpleJsonMixin[source]

Bases: object

simple json serialization. Useful to store and retrieve

class dol.mixins.StringKvWrap[source]

Bases: IdentityKvWrapMixin