flexmeasures.utils.coding_utils

Various coding utils (e.g. around function decoration)

Functions

flexmeasures.utils.coding_utils.delete_key_recursive(value, key)

Delete key in a multilevel dictionary

flexmeasures.utils.coding_utils.deprecated(alternative, version: str | None = None)

Decorator for printing a warning error. alternative: importable object to use as an alternative to the function/method decorated version: version in which the function will be sunset

flexmeasures.utils.coding_utils.find_classes_module(module, superclass)
flexmeasures.utils.coding_utils.find_classes_modules(module, superclass, skiptest=True)
flexmeasures.utils.coding_utils.flatten_unique(nested_list_of_objects: list) list

Get unique sensor IDs from a list of sensors_to_show.

Handles: - Lists of sensor IDs - Dictionaries with a sensors key - Nested lists (one level) - Dictionaries with plots key containing lists of sensors or asset’s flex-config reference

Example:

Input: [1, [2, 20, 6], 10, [6, 2], {“title”:None,”sensors”: [10, 15]}, 15, {“plots”: [{“sensor”: 1}, {“sensors”: [20, 6]}]}]

Output: [1, 2, 20, 6, 10, 15]

flexmeasures.utils.coding_utils.get_classes_module(module, superclass, skiptest=True) dict
flexmeasures.utils.coding_utils.merge_or_append(item: dict[str, Any], items: list[dict[str, Any]], match_key: str, match_value: str | None = None) None

Merge item into the first dictionary in items with the same value for key, preserving its position in the sequence.

Values from item take precedence when keys overlap. If no matching dictionary is found, item is appended to the end of items.

Parameters:
  • item – The dictionary to merge or append.

  • items – A mutable sequence of dictionaries to update.

  • match_key – The dictionary key used to determine whether two items match.

  • match_value – The value used to determine whether two items match.

Returns:

None. The items sequence is modified in place.

flexmeasures.utils.coding_utils.optional_arg_decorator(fn)

A decorator which _optionally_ accepts arguments.

So a decorator like this:

@optional_arg_decorator def register_something(fn, optional_arg = ‘Default Value’):

… return fn

will work in both of these usage scenarios:

@register_something(‘Custom Name’) def custom_name():

pass

@register_something def default_name():

pass

Thanks to https://stackoverflow.com/questions/3888158/making-decorators-with-optional-arguments#comment65959042_24617244

flexmeasures.utils.coding_utils.sort_dict(unsorted_dict: dict) dict
flexmeasures.utils.coding_utils.timeit(func)

Decorator for printing the time it took to execute the decorated function.

Classes

class flexmeasures.utils.coding_utils.OrderByIdMixin

Mixin class that adds rich comparison and hashing methods based on an id attribute.

Classes inheriting from this mixin must define an id property or attribute that is an int or otherwise supports comparison and hashing.