Skip to content

brainhops.datamodel.base

The base class shared by every data model, and its converter.

Classes

IdentityComparison

Mixin that makes a class, and every subclass, compare by identity.

== and != are those of object (a == b is a is b) and never raise, and __hash__ is that of object, so instances are hashable and can be put in a set or used as dictionary keys.

A subclass that also derives from another struct takes its options from whichever base comes first, and may be given a generated __eq__ (and __hash__) that compares its fields. Identity is put back on every subclass, so none compares by value.

DataModelBase

Bases: Magic

Base class for all data models.

A polymorphic data model class is built from the arguments its on= constraint matches, and refuses the ones it does not (pin_discriminant="pin+narrow"): OrientedAxis(orientation=None) and AnatomicalAxis(orientation=<not anatomical>) raise rather than build an axis that contradicts its own class. A field a class writes out itself keeps its own type (bagof leaves it as written), which is why the discriminants that subclasses declare are typed narrowly (Literal["space"], Narrow[str]) where they are declared.

Methods:

from_dict classmethod
from_dict(other: Mapping, *args, **kwargs) -> Self

Create an instance of the class from a dictionary-like object.

Only keys in the dictionary that match keyword-like fields of this class, or the keywords its constructor takes without storing them (its InitVars, such as the matrix= of an Affine), will be used. Other keys are ignored, but see from_other, which refuses them.

Additional positional and/or keyword arguments can be provided, and will take precedence over the values in the dictionary.

A key naming a field that this class fixes (a field that cannot be passed to its constructor) is checked instead of used: a dictionary that sets it to anything other than None or the value of this class is refused with a ValueError.

from_instance classmethod
from_instance(other: Self, *args, **kwargs) -> Self

Create an instance of the class from an instance of a similar class.

Only attributes of the other instance that match keyword-like fields of this class will be used. An attribute that is None is unset, and leaves the default of this class in place.

Additional positional and/or keyword arguments can be provided, and will take precedence over the attributes in the instance.

Unless the other instance is already an instance of this class, an attribute naming a field that this class fixes (a field that cannot be passed to its constructor) is checked instead of used: an instance that sets it to anything other than None or the value of this class is refused with a ValueError. A generic Axis whose orientation is right-to-left, for example, cannot be read as a LeftToRightAxis.

from_other classmethod
from_other(other: Any, *args, **kwargs) -> Self

Create an instance of the class from any object that can be interpreted as a dictionary, or an instance of a similar class, or an arguments to be passed to the constructor.

A similar class is this class or one of its parents within the data model, or another member of a polymorphic family this class belongs to: calling a polymorphic class such as Axis builds the subclass its arguments select, so a "generic" axis is usually an instance of a sibling (a RightToLeftAxis, a TimeAxis) rather than of a parent. Any other object, including an instance of a parent that is not a data model (such as a plain object), is passed to the constructor.

Unlike from_dict, a dictionary with a key that matches no field of this class is refused with a TypeError naming the keys, so that a misspelt key is not silently dropped.

DataModelConverter

Bases: Converter[DataModelBase, Any]

Converts a value to a DataModelBase instance.

This converter is registered for DataModelBase, and is used automatically wherever a field is typed with DataModelBase or one of its subclasses and conversion is enabled. A value that is already an instance of the target type is returned unchanged. Any other value is converted through DataModelBase.from_other of the target type, so that a mapping or an instance of a parent class is read field by field instead of being passed to the constructor as its first argument.

As with the default converter, a TypeError or ValueError raised while converting surfaces as a [ConversionError][bagof.converters.ConversionError] (a ValueConversionError), with the original as its cause. Unlike the default converter, it keeps the original message.

Methods:

__call__
__call__(value: Any) -> DataModelBase

Convert value to an instance of the target type.