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

Add utils.consume from cytoolz #539

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion toolz/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from toolz.utils import raises
from toolz.utils import raises, consume


def test_raises():
assert raises(ZeroDivisionError, lambda: 1 / 0)
assert not raises(ZeroDivisionError, lambda: 1)


def test_consume():
l = [1, 2, 3]
assert consume(l) is None
il = iter(l)
assert consume(il) is None
assert raises(StopIteration, lambda: next(il))
assert raises(TypeError, lambda: consume(1))
8 changes: 8 additions & 0 deletions toolz/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
from collections import deque


def consume(seq):
"""Efficently consume an iterable"""
deque(seq, maxlen=0)


def raises(err, lamda):
try:
lamda()
Expand Down