Skip to content

Commit

Permalink
getter works in empty index case
Browse files Browse the repository at this point in the history
  • Loading branch information
mrocklin committed Jul 16, 2014
1 parent 379a1ae commit e36b5ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,10 @@ def getter(index):
if len(index) == 1:
index = index[0]
return lambda x: (x[index],)
else:
elif index:
return operator.itemgetter(*index)
else:
return lambda x: ()
else:
return operator.itemgetter(index)

Expand Down
8 changes: 7 additions & 1 deletion toolz/tests/test_itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from toolz.itertoolz import (remove, groupby, merge_sorted,
concat, concatv, interleave, unique,
isiterable,
isiterable, getter,
mapcat, isdistinct, first, second,
nth, take, drop, interpose, get,
rest, last, cons, frequencies,
Expand Down Expand Up @@ -315,6 +315,12 @@ def addpair(pair):
assert result == expected


def test_getter():
assert getter(0)('Alice') == 'A'
assert getter([0])('Alice') == ('A',)
assert getter([])('Alice') == ()


def test_key_as_getter():
squares = [(i, i**2) for i in range(5)]
pows = [(i, i**2, i**3) for i in range(5)]
Expand Down

0 comments on commit e36b5ea

Please sign in to comment.