dantro.mixins.base module

This sub-module implements the basic mixin classes that are required in the dantro.base module

exception dantro.mixins.base.UnexpectedTypeWarning[source]

Bases: UserWarning

Given when there was an unexpected type passed to a data container.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class dantro.mixins.base.AttrsMixin[source]

Bases: object

This Mixin class supplies the attrs property getter and setter and the private _attrs attribute.

Hereby, the setter function will initialize a BaseDataAttrs-derived object and store it as an attribute. This relays the checking of the correct attribute format to the actual BaseDataAttrs-derived class.

For changing the class that is used for the attributes, an overwrite of the _ATTRS_CLS class variable suffices.

_attrs = None
_ATTRS_CLS = None
property attrs

The container attributes.

class dantro.mixins.base.SizeOfMixin[source]

Bases: object

Provides the __sizeof__ magic method and attempts to take into account the size of the attributes.

__sizeof__() → int[source]

Returns the size of the data (in bytes) stored in this container’s data and its attributes.

Note that this value is approximate. It is computed by calling the sys.getsizeof function on the data, the attributes, the name and some caching attributes that each dantro data tree class contains. Importantly, this is not a recursive algorithm.

Also, derived classes might implement further attributes that are not taken into account either. To be more precise in a subclass, create a specific __sizeof__ method and invoke this parent method additionally.

For more information, see the documentation of sys.getsizeof:

class dantro.mixins.base.LockDataMixin[source]

Bases: object

This Mixin class provides a flag for marking the data of a group or container as locked.

property locked

Whether this object is locked

lock()[source]

Locks the data of this object

unlock()[source]

Unlocks the data of this object

raise_if_locked(*, prefix: str = None)[source]

Raises an exception if this object is locked; does nothing otherwise

_lock_hook()[source]

Invoked upon locking.

_unlock_hook()[source]

Invoked upon unlocking.

_LockDataMixin__locked = False
class dantro.mixins.base.CollectionMixin[source]

Bases: object

This Mixin class implements the methods needed for being a Collection.

It relays all calls forward to the data attribute.

__contains__(key) → bool[source]

Whether the given key is contained in the items.

__len__() → int[source]

The number of items.

__iter__()[source]

Iterates over the items.

class dantro.mixins.base.ItemAccessMixin[source]

Bases: object

This Mixin class implements the methods needed for getting, setting, and deleting items. It relays all calls forward to the data attribute, but if given a list (passed down from above), it extracts it

__getitem__(key)[source]

Returns an item.

__setitem__(key, val)[source]

Sets an item.

__delitem__(key)[source]

Deletes an item

_item_access_convert_list_key(key)[source]

If given something that is not a list, just return that key

class dantro.mixins.base.MappingAccessMixin[source]

Bases: dantro.mixins.base.ItemAccessMixin, dantro.mixins.base.CollectionMixin

Supplies all methods that are needed for Mapping access.

All calls are relayed to the data attribute.

keys()[source]

Returns an iterator over the attribute names.

values()[source]

Returns an iterator over the attribute values.

items()[source]

Returns an iterator over the (keys, values) tuple of the attributes.

get(key, default=None)[source]

Return the value at key, or default if key is not available.

__contains__(key) → bool

Whether the given key is contained in the items.

__delitem__(key)

Deletes an item

__getitem__(key)

Returns an item.

__iter__()

Iterates over the items.

__len__() → int

The number of items.

__setitem__(key, val)

Sets an item.

_item_access_convert_list_key(key)

If given something that is not a list, just return that key

class dantro.mixins.base.CheckDataMixin[source]

Bases: object

This mixin class extends a BaseDataContainer-derived class to check the provided data before storing it in the container.

It implements a general _check_data method, overwriting the placeholder method in the BaseDataContainer, and can be controlled via class variables.

Note

This is not suitable for checking containers that are added to an object of a BaseDataGroup-derived class!

DATA_ALLOW_PROXY

Whether to allow _all_ proxy types, i.e. classes derived from AbstractDataProxy

Type

bool

DATA_EXPECTED_TYPES

Which types to allow. If None, all types are allowed.

Type

tuple, None

DATA_UNEXPECTED_ACTION

The action to take when an unexpected type was supplied. Can be: raise, warn, ignore

Type

str

DATA_EXPECTED_TYPES = None
DATA_ALLOW_PROXY = False
DATA_UNEXPECTED_ACTION = 'warn'
_check_data(data) → None[source]

A general method to check the received data for its type

Parameters

data – The data to check

Raises
  • TypeError – If the type was unexpected and the action was ‘raise’

  • ValueError – Illegal value for DATA_UNEXPECTED_ACTION class variable

Returns

None