flexmeasures.data.schemas.units
Classes
- class flexmeasures.data.schemas.units.QuantityField(to_unit: str, *args, default_src_unit: str | None = None, return_magnitude: bool = False, **kwargs)
Marshmallow/Click field for validating quantities against a unit registry.
The FlexMeasures unit registry is based on the pint library.
- For example:
>>> percentage_field = QuantityField("%", validate=validate.Range(min=0, max=1)) >>> percentage_field.deserialize("2.5%") <Quantity(2.5, 'percent')> >>> percentage_field.deserialize(0.025) <Quantity(2.5, 'percent')> >>> power_field = QuantityField("kW", validate=validate.Range(max=ur.Quantity("1 kW"))) >>> power_field.deserialize("120 W") <Quantity(0.12, 'kilowatt')>
- __init__(to_unit: str, *args, default_src_unit: str | None = None, return_magnitude: bool = False, **kwargs)
- _deserialize(value, attr, data, return_magnitude: bool | None = None, **kwargs) Quantity
Turn a quantity describing string into a Quantity.
- _serialize(value, attr, obj, **kwargs)
Turn a Quantity into a string in scientific format.
- class flexmeasures.data.schemas.units.QuantityValidator(*, error: str | None = None)
Validator which succeeds if the value passed to it is a valid quantity.
- class flexmeasures.data.schemas.units.UnitField(*, load_default: ~typing.Any = <marshmallow.missing>, missing: ~typing.Any = <marshmallow.missing>, dump_default: ~typing.Any = <marshmallow.missing>, default: ~typing.Any = <marshmallow.missing>, data_key: str | None = None, attribute: str | None = None, validate: ~typing.Callable[[~typing.Any], ~typing.Any] | ~typing.Iterable[~typing.Callable[[~typing.Any], ~typing.Any]] | None = None, required: bool = False, allow_none: bool | None = None, load_only: bool = False, dump_only: bool = False, error_messages: dict[str, str] | None = None, metadata: ~typing.Mapping[str, ~typing.Any] | None = None, **additional_metadata)
Field that represents a unit.
- _deserialize(value, attr, data, **kwargs) str
Deserialize value. Concrete
Fieldclasses should implement this method.- Parameters:
value – The value to be deserialized.
attr – The attribute/key in data to be deserialized.
data – The raw input data passed to the Schema.load <marshmallow.Schema.load>.
kwargs – Field-specific keyword arguments.
- Raises:
ValidationError – In case of formatting or validation failure.
- Returns:
The deserialized value.
Changed in version 3.0.0: Added
**kwargsto signature.
- _serialize(value: str, attr, obj, **kwargs) str
Serializes
valueto a basic Python datatype. Noop by default. ConcreteFieldclasses should implement this method.Example:
class TitleCase(Field): def _serialize(self, value, attr, obj, **kwargs): if not value: return "" return str(value).title()
- Parameters:
value – The value to be serialized.
attr – The attribute or key on the object to be serialized.
obj – The object the value was pulled from.
kwargs – Field-specific keyword arguments.
- Returns:
The serialized value