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.

class flexmeasures.data.schemas.times.StartEndTimeSchema(*, only: types.StrSequenceOrSet | None = None, exclude: types.StrSequenceOrSet = (), many: bool = False, context: dict | None = None, load_only: types.StrSequenceOrSet = (), dump_only: types.StrSequenceOrSet = (), partial: bool | types.StrSequenceOrSet | None = None, unknown: str | None = None)
validate(data, **kwargs)

Validate data against the schema, returning a dictionary of validation errors.

Parameters:
  • data – The data to validate.

  • many – Whether to validate data as a collection. If None, the value for self.many is used.

  • partial – Whether to ignore missing fields and not require any fields declared. Propagates down to Nested fields as well. If its value is an iterable, only missing fields listed in that iterable will be ignored. Use dot delimiters to specify nested fields.

Returns:

A dictionary of validation errors.

New in version 1.1.0.

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

Field that de-serializes to a TimeInverval defined with start and duration.

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

Deserialize value. Concrete Field classes 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.

  • kwargs – Field-specific keyword arguments.

Raises:

ValidationError – In case of formatting or validation failure.

Returns:

The deserialized value.

Changed in version 2.0.0: Added attr and data parameters.

Changed in version 3.0.0: Added **kwargs to signature.

class flexmeasures.data.schemas.times.TimeIntervalSchema(*, only: types.StrSequenceOrSet | None = None, exclude: types.StrSequenceOrSet = (), many: bool = False, context: dict | None = None, load_only: types.StrSequenceOrSet = (), dump_only: types.StrSequenceOrSet = (), partial: bool | types.StrSequenceOrSet | None = None, unknown: str | None = None)

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)