flexmeasures.data.schemas.generic_assets
Classes
- class flexmeasures.data.schemas.generic_assets.GenericAssetIdField(status_if_not_found: HTTPStatus | None = None, *args, **kwargs)
Field that deserializes to a GenericAsset and serializes back to an integer.
- _deserialize(value: int | str, attr, obj, **kwargs) GenericAsset
Turn a generic asset id into a GenericAsset.
- _serialize(asset: GenericAsset, attr, data, **kwargs) int
Turn a GenericAsset into a generic asset id.
- class flexmeasures.data.schemas.generic_assets.GenericAssetSchema(*args, **kwargs)
GenericAsset schema, with validations.
- class Meta
- model
alias of
GenericAsset
- opts: LoadInstanceMixin.Opts = <flask_marshmallow.sqla.SQLAlchemySchemaOpts object>
- class flexmeasures.data.schemas.generic_assets.GenericAssetTypeSchema(*args, **kwargs)
GenericAssetType schema, with validations.
- class Meta
- model
alias of
GenericAssetType
- opts: LoadInstanceMixin.Opts = <flask_marshmallow.sqla.SQLAlchemySchemaOpts object>
- class flexmeasures.data.schemas.generic_assets.JSON(*, 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: types.Validator | typing.Iterable[types.Validator] | 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)
- _deserialize(value, attr, data, **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 <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
**kwargs
to signature.
- _serialize(value, attr, data, **kwargs) str
Serializes
value
to a basic Python datatype. Noop by default. ConcreteField
classes 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
- class flexmeasures.data.schemas.generic_assets.SensorKPIFieldSchema(*args, **kwargs)
- opts: LoadInstanceMixin.Opts = <flask_marshmallow.sqla.SQLAlchemySchemaOpts object>
- class flexmeasures.data.schemas.generic_assets.SensorsToShowAsKPIsSchema(*args, **kwargs)
- opts: LoadInstanceMixin.Opts = <flask_marshmallow.sqla.SQLAlchemySchemaOpts object>
- class flexmeasures.data.schemas.generic_assets.SensorsToShowSchema(*, 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: types.Validator | typing.Iterable[types.Validator] | 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)
Schema for validating and deserializing the sensors_to_show attribute of a GenericAsset.
The sensors_to_show attribute defines which sensors should be displayed for a particular asset. It supports various input formats, which are standardized into a list of dictionaries, each containing a title (optional) and a sensors list. The valid input formats include:
A single sensor ID (int): 42 -> {“title”: None, “sensors”: [42]}
A list of sensor IDs (list of ints): [42, 43] -> {“title”: None, “sensors”: [42, 43]}
A dictionary with a title and sensor: {“title”: “Temperature”, “sensor”: 42} -> {“title”: “Temperature”, “sensors”: [42]}
A dictionary with a title and sensors: {“title”: “Pressure”, “sensors”: [42, 43]}
Validation ensures that: - The input is either a list, integer, or dictionary. - If the input is a dictionary, it must contain either sensor (int) or sensors (list of ints). - All sensor IDs must be valid integers.
Example Input: - [{“title”: “Test”, “sensors”: [1, 2]}, {“title”: None, “sensors”: [3, 4]}, 5]
Example Output (Standardized): - [{“title”: “Test”, “sensors”: [1, 2]}, {“title”: None, “sensors”: [3, 4]}, {“title”: None, “sensors”: [5]}]
- _standardize_item(item) dict
Standardize different input formats to a consistent dictionary format.
- classmethod flatten(nested_list) list[int]
Flatten a nested list of sensors or sensor dictionaries into a unique list of sensor IDs.
This method processes the following formats, for each of the entries of the nested list: - A list of sensor IDs: [1, 2, 3] - A list of dictionaries where each dictionary contains a sensors list or a sensor key: [{“title”: “Temperature”, “sensors”: [1, 2]}, {“title”: “Pressure”, “sensor”: 3}] - Mixed formats: [{“title”: “Temperature”, “sensors”: [1, 2]}, {“title”: “Pressure”, “sensor”: 3}, 4, 5, 1]
It extracts all sensor IDs, removes duplicates, and returns a flattened list of unique sensor IDs.
- Args:
nested_list (list): A list containing sensor IDs, or dictionaries with sensors or sensor keys.
- Returns:
list: A unique list of sensor IDs.