dantro.mixins.base module

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

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


Locks the data of this object


Unlocks the data of this object

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

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


Invoked upon locking.


Invoked upon unlocking.

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

Bases: object

Provides a (very basic) __eq__ method to compare equality.

__eq__(other) → bool[source]

Evaluates equality by making the following comparisons: identity, strict type equality, and finally: equality of the _data and _attrs attributes, i.e. the private attribute. This ensures that comparison does not trigger any downstream effects like resolution of proxies.

If types do not match exactly, NotImplemented is returned, thus referring the comparison to the other side of the ==.

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.


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


Returns an item.

__setitem__(key, val)[source]

Sets an item.


Deletes an item


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.


Returns an iterator over the data’s keys.


Returns an iterator over the data’s values.


Returns an iterator over data’s (key, value) tuples

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.


Deletes an item


Returns an item.


Iterates over the items.

__len__() → int

The number of items.

__setitem__(key, val)

Sets an item.


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.


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


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




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


tuple, None


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



_check_data(data) → None[source]

A general method to check the received data for its type


data – The data to check

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

  • ValueError – Illegal value for DATA_UNEXPECTED_ACTION class variable



class dantro.mixins.base.DirectInsertionModeMixin[source]

Bases: object

A mixin class that provides a context manager, within which insertion into the mixed-in class (think: group or container) can happen more directly. This is useful in cases where more assumptions can be made about the to-be-inserted data, thus allowing to make fewer checks during insertion (think: duplicates, key order, etc.).


This direct insertion mode is not (yet) part of the public interface, as it has to be evaluated how robust and error-prone it is.

property with_direct_insertion

Whether the class this mixin is mixed into is currently in direct insertion mode.

_direct_insertion_mode(*, enabled: bool = True)[source]

A context manager that brings the class this mixin is used in into direct insertion mode. While in that mode, the with_direct_insertion() property will return true.

This context manager additionally invokes two callback functions, which can be specialized to perform certain operations when entering or exiting direct insertion mode: Before entering, _enter_direct_insertion_mode() is called. After exiting, _exit_direct_insertion_mode() is called.


enabled (bool, optional) – whether to actually use direct insertion mode. If False, will yield directly without setting the toggle. This is equivalent to a null-context.


Called after entering direct insertion mode; can be overwritten to attach additional behaviour.

_DirectInsertionModeMixin__in_direct_insertion_mode = False

Called before exiting direct insertion mode; can be overwritten to attach additional behaviour.