Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,23 @@ def partition_all(n, seq):
yield prev
prev = item
if prev[-1] is no_pad:
yield prev[:prev.index(no_pad)]
try:
# If seq defines __len__, then
# we can quickly calculate where no_pad starts
yield prev[:len(seq) % n]
except TypeError:
# Get first index of no_pad without using .index()
# https://github.com/pytoolz/toolz/issues/387
# Binary search from CPython's bisect module,
# modified for identity testing.
lo, hi = 0, n
while lo < hi:
mid = (lo + hi) // 2
if prev[mid] is no_pad:
hi = mid
else:
lo = mid + 1
yield prev[:lo]
else:
yield prev

Expand Down
11 changes: 11 additions & 0 deletions toolz/tests/test_itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ def test_partition_all():
assert list(partition_all(3, range(5))) == [(0, 1, 2), (3, 4)]
assert list(partition_all(2, [])) == []

# Regression test: https://github.com/pytoolz/toolz/issues/387
class NoCompare(object):
def __eq__(self, other):
if self.__class__ == other.__class__:
return True
raise ValueError()
obj = NoCompare()
result = [(obj, obj, obj, obj), (obj, obj, obj)]
assert list(partition_all(4, [obj]*7)) == result
assert list(partition_all(4, iter([obj]*7))) == result


def test_count():
assert count((1, 2, 3)) == 3
Expand Down