-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: add helpers to merge extra data into a cycler instance #47
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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_supplemental(source, indx_key, supplemental_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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name here doesn't match the name in the call signature. |
||
|
||
Parameters | ||
---------- | ||
source : Cycler | ||
The cycler to augment. | ||
|
||
indx_key : Any | ||
Must be one of the keys in ``source`` | ||
|
||
supplemental_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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This docstring would greatly benefit from an example. As it stands right now, I have to think really hard to understand the use-cases for this function (and I am the one who originally thought of this idea!). |
||
|
||
Returns | ||
------- | ||
ret : Cycler | ||
|
||
""" | ||
return (source + | ||
from_iter_of_dicts(supplemental_data[v[indx_key]] | ||
for v in source)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be pedantic, wouldn't the first
Any
beHashable
? I haven't gotten fully into the annotation habit yet, so I don't know ifMapping
already impliesHashable
for the keys.