randonneur.utils

Attributes

ALL_VERBS

EXCLUDED_ATTRS

SAFE_VERBS

Classes

FlexibleLookupDict

A Mapping is a generic container for associating key/value

Functions

apply_mapping(→ dict)

Apply the label changes in mapping to the transformations in migrations.

rescale_edge(→ dict)

Rescale edges, including formulas and uncertainty values, by a constant factor

right_case(→ Any)

Convert strings to lower case, and lists to tuples.

Module Contents

class randonneur.utils.FlexibleLookupDict(input_data: collections.abc.Iterable[dict], fields_filter: List[str] | None = None, case_sensitive: bool = False)[source]

Bases: collections.abc.Mapping

A Mapping is a generic container for associating key/value pairs.

This class provides concrete generic implementations of all methods except for __getitem__, __iter__, and __len__.

A dictionary that allow for more flexible matching of dictionaries against other dicts.

input_data is a dictionary like {“foo”: {“first”: True, “bar”: 42}}. We want to match this input against {‘first’: True} and get back foo. Here is an examples:

```python fld = FlexibleLookupDict(

input_data=[

{“source”: {“foo”: “a”, “bar”: “b”}}, {“source”: {“foo”: “b”}},

]

) fld[{“foo”: “b”}] == {“source”: {“foo”: “b”}} >>> True ```

For real data we would have input data with both source and target (or targets for disaggregation) keys. This class makes the strong assumption that input_data has source and target/targets keys.

We need to match a dictionary against another dictionary, but the other dictionary doesn’t have a fixed set of keys - they can vary across all the possibilities. We therefore allow matching based on each unique combination of keys present.

If fields_filter is given, then only consider keys present in that list.

```python fld = FlexibleLookupDict(

input_data=[

{“source”: {“foo”: “a”, “bar”: “b”}}, {“source”: {“foo”: “b”}},

], fields_filter=[“foo”]

) fld[{“foo”: “b”, “other”: “whatever”}] == {“source”: {“foo”: “b”}} >>> True ```

If case_sensitive, then do case-sensitive matching on values (not keys) when comparing strings. Here is an example of a case-insensitve match:

```python fld = FlexibleLookupDict(

input_data=[

{“source”: {“foo”: “a”, “bar”: “b”}}, {“source”: {“foo”: “b”}},

], case_sensitive=False

) fld[{“foo”: “B”}] == {“source”: {“foo”: “b”}} >>> True ```

_case_sensitive = False[source]
_dict[source]
_field_combinations[source]
randonneur.utils.apply_mapping(migrations: dict, mapping: dict, verbs: List[str]) dict[source]

Apply the label changes in mapping to the transformations in migrations.

randonneur.utils.rescale_edge(edge: dict, factor: numbers.Number) dict[source]

Rescale edges, including formulas and uncertainty values, by a constant factor

randonneur.utils.right_case(value: Any, case_sensitive: bool) Any[source]

Convert strings to lower case, and lists to tuples.

randonneur.utils.ALL_VERBS = ['create', 'delete', 'replace', 'update', 'disaggregate'][source]
randonneur.utils.EXCLUDED_ATTRS = ('target', 'targets', 'source', 'conversion_factor')[source]
randonneur.utils.SAFE_VERBS = ['update', 'replace', 'disaggregate'][source]