randonneur.utils
Attributes
Classes
A Mapping is a generic container for associating key/value |
Functions
|
Apply the label changes in mapping to the transformations in migrations. |
|
Rescale edges, including formulas and uncertainty values, by a constant factor |
|
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.MappingA 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 ```
- 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