From: epage Date: Wed, 20 May 2009 01:00:30 +0000 (+0000) Subject: Bringing doctests upto snuff with Python26 X-Git-Url: http://vcs.maemo.org/git/?p=ejpi;a=commitdiff_plain;h=b7ad5feed84c40384764d9aee24671ab20c6c761 Bringing doctests upto snuff with Python26 git-svn-id: file:///svnroot/ejpi/trunk@37 df6cc7de-23d0-4ae0-bb86-c17aa67b2a9d --- diff --git a/src/libraries/recipes/algorithms.py b/src/libraries/recipes/algorithms.py index a2dc356..5da8b80 100644 --- a/src/libraries/recipes/algorithms.py +++ b/src/libraries/recipes/algorithms.py @@ -87,51 +87,37 @@ def iterstep(iterator, n): 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): diff --git a/src/libraries/recipes/concurrent.py b/src/libraries/recipes/concurrent.py index 3961723..36548f0 100644 --- a/src/libraries/recipes/concurrent.py +++ b/src/libraries/recipes/concurrent.py @@ -95,6 +95,7 @@ def qlock(queue, gblock = True, gtimeout = None, pblock = True, ptimeout = None) """ Locking with a queue, good for when you want to lock an item passed around + >>> import Queue >>> item = 5 >>> lock = Queue.Queue() >>> lock.put(item) diff --git a/src/libraries/recipes/io.py b/src/libraries/recipes/io.py index 6470cb6..aece2dd 100644 --- a/src/libraries/recipes/io.py +++ b/src/libraries/recipes/io.py @@ -55,6 +55,7 @@ def redirect(object_, attr, value): ... print "hello" ... >>> print "we're back" + we're back """ orig = getattr(object_, attr) setattr(object_, attr, value) diff --git a/src/libraries/recipes/misc.py b/src/libraries/recipes/misc.py index 2104c84..b381ea3 100644 --- a/src/libraries/recipes/misc.py +++ b/src/libraries/recipes/misc.py @@ -48,11 +48,20 @@ def privatize(clsName, attributeName): >>> 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 >>> @@ -72,8 +81,13 @@ def obfuscate(clsName, attributeName): >>> 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")) @@ -565,16 +579,18 @@ def lexical_scope(*args): >>> 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