Skip to content

Commit 4c0be03

Browse files
committed
-
1 parent cf06047 commit 4c0be03

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

source_py3/python_toolbox/cute_iter_tools.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def call_until_exception(function, exception, lazy_tuple=False):
346346
'''
347347
iterator = _call_until_exception(function, exception)
348348
if lazy_tuple:
349-
from python_toolbox import nifty_collections # Avoiding circular import.
349+
from python_toolbox import nifty_collections # Avoiding circular import
350350
return nifty_collections.LazyTuple(iterator)
351351
else:
352352
return iterator
@@ -469,3 +469,18 @@ def push_back(self):
469469
self.just_pushed_back = True
470470

471471

472+
473+
def iterate_pop(poppable, lazy_tuple=False):
474+
return call_until_exception(poppable.pop, IndexError,
475+
lazy_tuple=lazy_tuple)
476+
477+
def iterate_popleft(left_poppable, lazy_tuple=False):
478+
return call_until_exception(left_poppable.popleft, IndexError,
479+
lazy_tuple=lazy_tuple)
480+
481+
def iterate_popitem(item_poppable, lazy_tuple=False):
482+
return call_until_exception(item_poppable.popitem, KeyError,
483+
lazy_tuple=lazy_tuple)
484+
485+
486+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2009-2014 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
import collections
5+
from python_toolbox import nifty_collections
6+
7+
from python_toolbox.cute_iter_tools import (iterate_pop, iterate_popitem,
8+
iterate_popleft)
9+
10+
11+
def test():
12+
13+
deque = collections.deque(range(10))
14+
assert tuple(iterate_pop(deque)) == tuple(range(9, -1, -1))
15+
assert not deque
16+
17+
deque = collections.deque(range(10))
18+
assert tuple(iterate_popleft(deque)) == tuple(range(10))
19+
assert not deque
20+
21+
dict_ = {1: 2, 3: 4, 5: 6,}
22+
assert dict(iterate_popitem(dict_)) == {1: 2, 3: 4, 5: 6,}
23+
assert not dict_
24+
25+
lazy_tuple = iterate_pop(list(range(5)))
26+
assert isinstance(lazy_tuple, nifty_collections.LazyTuple)
27+

0 commit comments

Comments
 (0)