iterator.next()
-def itergroup(iterator, count, to_container=tuple):
+def itergroup(iterator, count, padValue = None):
"""
Iterate in groups of 'count' values. If there
aren't enough values, the last result is padded with
None.
>>> for val in itergroup([1, 2, 3, 4, 5, 6], 3):
- ... print val
+ ... print tuple(val)
(1, 2, 3)
(4, 5, 6)
- >>> for val in itergroup([1, 2, 3, 4, 5, 6], 3, list):
- ... print val
+ >>> for val in itergroup([1, 2, 3, 4, 5, 6], 3):
+ ... print list(val)
[1, 2, 3]
[4, 5, 6]
>>> for val in itergroup([1, 2, 3, 4, 5, 6, 7], 3):
- ... print val
+ ... print tuple(val)
(1, 2, 3)
(4, 5, 6)
(7, None, None)
>>> for val in itergroup("123456", 3):
- ... print val
+ ... print tuple(val)
('1', '2', '3')
('4', '5', '6')
- >>> for val in itergroup("123456", 3, lambda i: "".join(s for s in i if s is not None)):
- ... print repr(val)
+ >>> for val in itergroup("123456", 3):
+ ... print repr("".join(val))
'123'
'456'
"""
-
- iterator = iter(iterator)
- values_left = [True]
-
- def values():
- values_left[0] = False
- for x in range(count):
- try:
- yield iterator.next()
- values_left[0] = True
- except StopIteration:
- yield None
- while True:
- value = to_container(values())
- if not values_left[0]:
- raise StopIteration
- yield value
+ paddedIterator = itertools.chain(iterator, itertools.repeat(padValue, count-1))
+ nIterators = (paddedIterator, ) * count
+ return itertools.izip(*nIterators)
def xzip(*iterators):
>>> class Test(object):
... pass
...
- >>> dir(Test)
- ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
+ >>> try:
+ ... dir(Test).index("_Test__me")
+ ... print dir(Test)
+ ... except:
+ ... print "Not Found"
+ Not Found
>>> setattr(Test, privatize(Test.__name__, "me"), "Hello World")
- >>> dir(Test)
- ['_Test__me', '__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
+ >>> try:
+ ... dir(Test).index("_Test__me")
+ ... print "Found"
+ ... except:
+ ... print dir(Test)
+ 0
+ Found
>>> print getattr(Test, obfuscate(Test.__name__, "__me"))
Hello World
>>>
>>> class Test(object):
... __me = "Hello World"
...
- >>> dir(Test)
- ['_Test__me', '__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
+ >>> try:
+ ... dir(Test).index("_Test__me")
+ ... print "Found"
+ ... except:
+ ... print dir(Test)
+ 0
+ Found
>>> print getattr(Test, obfuscate(Test.__name__, "__me"))
Hello World
>>> is_private(obfuscate(Test.__name__, "__me"))
>>> with lexical_scope(1) as (a):
... print a
...
+ 1
>>> with lexical_scope(1,2,3) as (a,b,c):
... print a,b,c
...
+ 1 2 3
>>> with lexical_scope():
... d = 10
... def foo():
... pass
...
- >>> print dir() # check those temporary variables are deleted.
>>> print b
+ 2
"""
frame = inspect.currentframe().f_back.f_back