Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Copy link
Member

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 be Hashable? I haven't gotten fully into the annotation habit yet, so I don't know if Mapping already implies Hashable for the keys.

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.
Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The 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))