Reference Documentation¶
Core¶
- mappr.register(src_type, dst_type, strategy=Strategy.CONSTRUCTOR, mapping=None, strict=True)¶
Register new converter.
- Parameters:
Returns:
- mappr.convert(dst_type, src_obj, strict=True, strategy=None, **extra)¶
Convert an object to a given type.
- Parameters:
dst_type (
Type
[TypeVar
(T
)]) – Target type. This is the type of the return value.src_obj – An object to convert. A registered converter will be used to map between the attributes of this object and the target type.
strict (
bool
) – If set toFalse
and the converter is not found for the given type pair, it will create an ad-hoc one that maps the attributes by their name. Defaults toTrue
- Return type:
TypeVar
(T
)- Returns:
A newly created instance of
dst_type
with values initialized fromsrc_obj
.
Built-in field mappers¶
- mappr.use_default(o)¶
Indicate we want to use default field value rather than value from source object
- Parameters:
src_obj – The converted object. Not used here, but part of the mappers interface so has to be defined.
name – l Name of the converted field. Not used here, but part of the mappers interface.
- Return type:
- Returns:
A mapping function for the field. This is just a stub, as
mappr.convert
will just skip the field if it’s mapper is set tomappr.use_default
.
By default, if the attribute exists on the source object, it’s value will be used when creating the result. That happens even if the destination type defines a default value for that field. If you want to use the default provided by the target type you can achive that with
mappr.use_default
mapper. You simply set the field tomappr.use_default
and the attribute value from the source object won’t be used (the target default is used). If the target type does not define a default value an exception will be raised during the creation of the final result.Example
>>> from dataclasses import dataclass >>> import mappr >>> >>> @dataclass ... class Src: ... text: str = 'hello' ... num: int = 10 >>> >>> @dataclass ... class Dst: ... text: str ... num: int = 20 >>> >>> mappr.register(Src, Dst, mapping=dict( ... num=mappr.use_default ... )) >>> >>> src = Src() >>> dst = mappr.convert(Dst, src) >>> >>> dst.text 'hello' >>> dst.num 20
- mappr.set_const(value)¶
Always set the field to the given value during conversion.
You can use
mappr.set_const
if you want to set an attribute to a constant value during conversion, without concern about the source object. Every call tomappr.convert
will return the same value.- Parameters:
value (
Any
) – The value to set on the field. Instead of using the source object attribute or target type field default, this value will be set on the field during conversion.- Return type:
- Returns:
A mapping function.
Example
>>> from dataclasses import dataclass >>> import mappr >>> >>> @dataclass ... class Src: ... text: str = 'hello' ... num: int = 10 >>> >>> @dataclass ... class Dst: ... text: str ... num: int = 20 >>> >>> mappr.register(Src, Dst, mapping=dict( ... num=mappr.set_const(100) ... )) >>> >>> src = Src() >>> dst = mappr.convert(Dst, src) >>> >>> dst.text 'hello' >>> dst.num 100