From f0823d45091df98ec6745a8ea4279c4929e58f2d Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 23 Apr 2018 15:32:05 -0400 Subject: [PATCH 1/2] ENH: add helpers to merge extra data into a cycler instance From private discussions with @weathergod --- cycler.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/cycler.py b/cycler.py index f6245c1..f767060 100644 --- a/cycler.py +++ b/cycler.py @@ -563,3 +563,59 @@ def _cycler(label, itr): itr = (v[lab] for v in itr) return Cycler._from_iter(label, itr) + + +def from_iter_of_dicts(inp): + """Construct a summation-only cycler from a list of dicts + + Given an iterable of dictionaries (such as you would get from + iterating over a `Cycler`) and constructs a new `Cycler`. + + The following are equivalent :: + + from_iter_of_dicts(list(c)) == c.simplify() + + Parameters + ---------- + inp : Iterable[Mapping[Any, Any]] + An iterable of dictionaries. All must have the same keys. + + Returns + ------- + ret : Cycler + """ + # TODO better validation that all keys match, not just using + # the keys from the first entry + # TODO deal with empty list correctly + inp = list(inp) + return reduce(add, (cycler(k, [_[k] for _ in inp]) for k in inp[0])) + + +def merge_suplemental(source, indx_key, sumplemental_data): + """Update a cycler with some supplemental data + + Given a cycler, add extra keys to each entry based + on the value of ``index_key`` in that entry. + + Parameters + ---------- + source : Cycler + The cycler to augment. + + indx_key : Any + Must be one of the keys in ``source`` + + sumplemental_data : Mapping[Any, Any] + A mapping between the values of ``index_key`` in ``source`` + and mappings of additional keys and values. + + Each mapping must have the same set of keys. + + Returns + ------- + ret : Cycler + + """ + return (source + + from_iter_of_dicts(sumplemental_data[v[indx_key]] + for v in source)) From 5f00bec178dfa911f5752b0234166ad70cc81b2b Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 13 Sep 2019 19:18:09 -0400 Subject: [PATCH 2/2] MNT: fix spelling --- cycler.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cycler.py b/cycler.py index f767060..7552e51 100644 --- a/cycler.py +++ b/cycler.py @@ -591,7 +591,7 @@ def from_iter_of_dicts(inp): return reduce(add, (cycler(k, [_[k] for _ in inp]) for k in inp[0])) -def merge_suplemental(source, indx_key, sumplemental_data): +def merge_supplemental(source, indx_key, supplemental_data): """Update a cycler with some supplemental data Given a cycler, add extra keys to each entry based @@ -605,7 +605,7 @@ def merge_suplemental(source, indx_key, sumplemental_data): indx_key : Any Must be one of the keys in ``source`` - sumplemental_data : Mapping[Any, Any] + supplemental_data : Mapping[Any, Any] A mapping between the values of ``index_key`` in ``source`` and mappings of additional keys and values. @@ -617,5 +617,5 @@ def merge_suplemental(source, indx_key, sumplemental_data): """ return (source + - from_iter_of_dicts(sumplemental_data[v[indx_key]] + from_iter_of_dicts(supplemental_data[v[indx_key]] for v in source))