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

Added itertoolz getter documentation #420

Open
wants to merge 1 commit into
base: master
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
26 changes: 26 additions & 0 deletions toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,32 @@ def pluck(ind, seqs, default=no_default):


def getter(index):
"""
Return a callable object that fetch item from its operand using the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: as per PEP 257, I would prefer that the docstring begin with a short description that fits on a single line followed by a blank line. Perhaps something short and friendly, such as:
""" Return callable that gets elements in a sequence or dict

operand's __getitem__() method.
If list of items are specified, returns a tuple of lookup values.

Let r be an object then the following are equivalent:
1. getter(i)(r) == r[i]
2. getter([i])(r) == (r[i], )
3. getter([i, j]) == (r[i], r[j])
4. getter([])(r) == ()

e.g:

>>> getter(0)('Alice')
'A'

>>> getter([0])('Alice')
('A',)

>>> getter('a')(dict(a='A', l='l', i='i', c='c', e='e'))
'A'

>>> getter(['a', 'l'])(dict(a='A', l='l', i='i', c='c', e='e'))
('A', 'l')

"""
if isinstance(index, list):
if len(index) == 1:
index = index[0]
Expand Down