flexmeasures.data.schemas.utils
Functions
- flexmeasures.data.schemas.utils.convert_to_quantity(value: str, to_unit: str) Quantity
Convert value to quantity in the given unit.
- Parameters:
value – Value to convert.
to_unit – Unit to convert to. If the unit starts with a ‘/’, the value can have any unit, and the unit is used as the denominator.
- Returns:
Quantity in the desired unit.
- flexmeasures.data.schemas.utils.load_data_from_path_and_files(request: Request, schema)
Custom webargs location loader to extract data from both path parameters and uploaded files.
This loader combines variables from the request URL path (request.view_args) and uploaded file data (request.files) into a single MultiDict, which is then passed to webargs for validation and deserialization.
- Note:
If any keys are present in both request.view_args and request.files, the file data will overwrite the path data for those keys.
- Parameters:
request (Request): The incoming Flask request object. schema: The webargs schema used to validate and deserialize the extracted data.
- Returns:
- MultiDictProxy: A proxy object wrapping the merged data from path parameters
and uploaded files.
- flexmeasures.data.schemas.utils.with_appcontext_if_needed()
Execute within the script’s application context, in case there is one.
An exception is flexmeasures run, which has a click context at the time the decorator is called, but no longer has a click context at the time the decorated function is called, which, typically, is a request to the running FlexMeasures server.
Classes
- class flexmeasures.data.schemas.utils.MarshmallowClickMixin(*args, **kwargs)
- __init__(*args, **kwargs)
- convert(value, param, ctx, **kwargs)
Convert the value to the correct type. This is not called if the value is
None
(the missing value).This must accept string values from the command line, as well as values that are already the correct type. It may also convert other compatible types.
The
param
andctx
arguments may beNone
in certain situations, such as when converting prompt input.If the value cannot be converted, call
fail()
with a descriptive message.- Parameters:
value – The value to convert.
param – The parameter that is using this type to convert its value. May be
None
.ctx – The current context that arrived at this value. May be
None
.
- get_metavar(param, **kwargs)
Returns the metavar default for this param if it provides one.
Exceptions
- exception flexmeasures.data.schemas.utils.FMValidationError(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)
Custom validation error class. It differs from the classic validation error by having two attributes, according to the USEF 2015 reference implementation. Subclasses of this error might adjust the status attribute accordingly.