flexmeasures.data.schemas.times

Classes

class flexmeasures.data.schemas.times.AwareDateTimeField(*args, **kwargs)

Field that de-serializes to a timezone aware datetime and serializes back to a string.

_deserialize(value: str, attr, obj, **kwargs) datetime

Work-around until this PR lands: https://github.com/marshmallow-code/marshmallow/pull/1787

class flexmeasures.data.schemas.times.DurationField(*args, **kwargs)

Field that deserializes to a ISO8601 Duration and serializes back to a string.

_deserialize(value, attr, obj, **kwargs) timedelta | isodate.Duration

Use the isodate library to turn an ISO8601 string into a timedelta. For some non-obvious cases, it will become an isodate.Duration, see ground_from for more. This method throws a ValidationError if the string is not ISO norm.

_serialize(value, attr, data, **kwargs)

An implementation of _serialize. It is not guaranteed to return the same string as was input, if ground_from has been used!

static ground_from(duration: timedelta | isodate.Duration, start: datetime | None) timedelta

For some valid duration strings (such as “P1M”, a month), converting to a datetime.timedelta is not possible (no obvious number of days). In this case, _deserialize returned an isodate.Duration. We can derive the timedelta by grounding to an actual time span, for which we require a timezone-aware start datetime.

class flexmeasures.data.schemas.times.PlanningDurationField(*args, **kwargs)
classmethod load_default()

Use this with the load_default arg to __init__ if you want the default FlexMeasures planning horizon.

Exceptions

exception flexmeasures.data.schemas.times.DurationValidationError(message: str | list | dict, field_name: str = '_schema', data: Mapping[str, Any] | Iterable[Mapping[str, Any]] | None = None, valid_data: list[dict[str, Any]] | dict[str, Any] | None = None, **kwargs)