brainhops.datamodel.base
Base class of the data models and its converter.
Classes
DataModelBase
Bases: SharedOptions
Base class of all data models.
All such classes are Magic classes, and they
implicitly convert values to the declared type of their attributes,
at construction or during assignment.
All instances of these classes can be constructed from
- an instance of a subclass,
- an instance of a superclass,
- an instance of "related" class (which shares a superclass), or
- a dictionary,
using the from_any, from_instance or from_dict factories,
as long as their attributes or keys are compatible.
Some classes are polymorphic, meaning that they can return instances of their subclasses based on the value of their arguments.
Methods:
from_dict
classmethod
Build an instance from a mapping.
Only keys that can be passed to the constructor are used.
Other keys are ignored, unlike in from_any
A key that corresponds to a fixed attribute (that is, an attribute that cannot be set by the constructor) is checked against its value.
Extra arguments (*args and **kwargs) are passed to the
constructor and take precedence over the mapping.
Raises:
| Type | Description |
|---|---|
ValueError
|
If a fixed attribute is given a value other than |
from_instance
classmethod
Build an instance from an instance of a related data model.
Two classes are related when:
- one of them is a parent of the other, or
- when both belong to the same polymorphic family, or
- when they represent the same "data model".
In all these cases, attributes that have the same name also have the same meaning, so they can be copied by name.
Note
- Only attributes that name keyword arguments are used.
- An attribute that is
Noneleaves the default of the class in place. - Extra arguments (
*argsand**kwargs) are passed to the constructor and take precedence. - Unless
otheris already an instance of this class, an attribute of the instance that that corresponds to a fixed attribute in the target class is checked against its value. - In genemost casesral,
cls.from_instance(other)is equivalent toother.to(cls, copy=True).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
DataModelBase
|
The instance to copy. |
required |
*args
|
Any
|
Constructor arguments, which take precedence over the fields
of |
()
|
**kwargs
|
Any
|
Constructor arguments, which take precedence over the fields
of |
{}
|
Returns:
| Type | Description |
|---|---|
Self
|
The new instance. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ConversionError
|
If the class of |
ValueError
|
If a fixed field of |
from_any
classmethod
Build an instance from a mapping, a data model or an argument.
This constructors dispatches to from_dict, from_instance
or the constructor, depending on the type of other.
Raises:
| Type | Description |
|---|---|
TypeError
|
If a mapping has a key that names no field. Unlike
|
to
Convert the object to another class of its data model.
Without a class, the object keeps its own class, and its attributes may be replaced by the keyword arguments.
Otherwise, the object is converted by cls.from_instance, which
copies the attributes of a related class by name. Some subclasses
override this method so that it triggers a more specific conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to convert to. By default, the class of the object. |
None
|
copy
|
bool
|
Whether to return a new object even when nothing changes. |
False
|
**kwargs
|
Any
|
Fields of the result, which take precedence over those of the object. |
{}
|
Returns:
| Type | Description |
|---|---|
DataModelBase
|
The converted object, an instance of |
Raises:
| Type | Description |
|---|---|
ConversionError
|
If |
DataModelConverter
Bases: Converter[DataModelBase, Any]
Converter to a DataModelBase, applied to fields of that type.
An instance of the target class is returned unchanged. Any other value
goes through DataModelBase.from_any, so a mapping or a parent
instance is read field by field rather than passed to the constructor.
A TypeError or ValueError raised during conversion becomes a
[ConversionError][bagof.converters.ConversionError] that keeps the
original message. A refused loss
(LossyConversionError),
such as a loss of metadata, is a TypeError too, but it passes
through unchanged, so that the error policy of the caller can act on
it as on any other loss. Any other conversion error of brainhops is
wrapped, so that a caller that tries several converters, such as the
converter of a union, still moves on to the next one.