From c20b7007199b0db9f3c8641df4a1911c17efa9aa Mon Sep 17 00:00:00 2001 From: matan Date: Fri, 28 Sep 2018 17:46:55 +0300 Subject: [PATCH] Added itertoolz getter documentation --- toolz/itertoolz.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index 941b9a9f..ffe68fcb 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -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 + 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]