Return to index
Iterator and functional programming tools. Most of the iterator methods were copied from the Python itertools documentation with added docstrings to show example usage.
Iterator that loops forever over a sequence.
>>> queue = Forever([1, 2, 3]) >>> [queue.next() for i in range(10)] [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
Returns True if pred(x) is True for every element in the iterable.
>>> all([True, True, True]) True>>> all([True, True, False]) False>>> all([]) True
Returns True if pred(x) is True for at least one element in the iterable:
>>> any([False, True, False]) True>>> any([False, False, False]) False>>> any([]) False
Returns a function f such that calling f(ob) deletes ob.attr:
>>> class Test: pass >>> test = Test() >>> test.aProp = "Test Value" >>> test.aProp 'Test Value'>>> fn = attrdeleter('aProp') >>> fn(test) >>> print test.aProp Traceback (most recent call last): ... AttributeError: Test instance has no attribute 'test'
Returns a function f such that calling f(ob) returns ob.attr:
>>> class Test: pass >>> test = Test() >>> test.aProp = "Test Value">>> fn = attrgetter('aProp') >>> fn(test) 'Test Value'
If ob.attr is not found the normal attribute error is raised:
>>> fn = attrgetter('oops') >>> print fn(test) Traceback (most recent call last): ... AttributeError: Test instance has no attribute 'oops'
Returns a function f such that calling f(ob,value) sets ob.attr = value:
>>> class Test: pass >>> test = Test() >>> fn = attrsetter('aProp') >>> fn(test,"Test Value") >>> test.aProp 'Test Value'>>> fn(test,"Changed Value") >>> test.aProp 'Changed Value'
Calculate the dot product of two vectors:
>>> dotproduct([1, 2, 3], [4, 5, 6]) 32
Iterate thorugh all items in seq discarding return values.
>>> test = xrange(10) >>> print exhaust(test) None
Return the first item in seq and discard the rest:
>>> first([1, 2, 3]) 1>>> print first([]) Traceback (most recent call last): ... StopIteration
Create a single list from seqs:
>>> flatten([1, 2, 3], (4, 5, 6)) [1, 2, 3, 4, 5, 6]
Generator that loops forever over a sequence.
>>> test = forever([1,2,3]) >>> [test.next() for i in range(10)] [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
Groups iterable into tuples of n elements:
>>> list(grouper(3, 'abcdefg', 'x')) [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
Returns a iterator that yields (0, item1), (1, item2), ...:
>>> list(ienumerate([1, 2, 3])) [(0, 1), (1, 2), (2, 3)]>>> list(ienumerate([])) []
Returns a function f such that calling f(ob) deletes f[item]:
>>> test = {'item' : 'Test Value'} >>> test['item'] 'Test Value'>>> fn = itemdeleter('item') >>> fn(test)>>> test['item'] Traceback (most recent call last): ... KeyError: 'item'
Returns a function f such that calling f(ob) returns ob[item]:
>>> test = { 'item' : 'Test Value' } >>> test['item'] 'Test Value'>>> fn = itemgetter('item') >>> fn(test) 'Test Value'
If ob[item] is not found the normal key error is raised:
>>> fn = itemgetter('oops') >>> print fn(test) Traceback (most recent call last): ... KeyError: 'oops'
Returns a function f such that calling f(ob,key) sets ob[attr] = value:
>>> test = {} >>> fn = itemsetter('item')>>> fn(test, 'Test Value') >>> test['item'] 'Test Value'>>> fn(test, 'Changed Value') >>> test['item'] 'Changed Value'
Returns a iterator yielding (key, value) pairs from mapping:
>>> i = iteritems({ 'a' : 0, 'b' : 1, 'c' : 2}) >>> i = list(i) >>> i.sort() >>> i [('a', 0), ('b', 1), ('c', 2)]
Returns the sequence elements n times:
>>> list(ncycles([1, 2, 3], 3)) [1, 2, 3, 1, 2, 3, 1, 2, 3]>>> list(ncycles([], 3)) []>>> list(ncycles([1, 2, 3], 0)) []
Returns True if pred(x) is False for every element in the iterable.
>>> no([False, False, False]) True>>> no([True, False, False]) False>>> no([]) True
Returns the nth item of iterable.
>>> nth([1, 2, 3], 1) :changeset:`2 [2]`
Returns the sequence elements and then returns None indefinitely. Useful for emulating the behavior of the built-in map() function.
>>> i = pad([1, 2, 3]).next >>> [i() for n in xrange(5)] [1, 2, 3, None, None] >>> i = pad([1, 2, 3], "Pad").next >>> [i() for n in xrange(5)] [1, 2, 3, 'Pad', 'Pad']
Group iterable into pairs:
>>> list(pairwise([1, 2, 3, 4])) [(1, 2), (2, 3), (3, 4)]
Count how many times the predicate is True in the sequence.
>>> quantify([True, False, True]) 2
Repeat calls to func with specified arguments:
>>> i = repeatfunc(lambda: 1) >>> [i.next() for n in xrange(10)] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]>>> list(repeatfunc(lambda: 2, 10)) [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
Return function(0), function(1), ...:
>>> iter = tabulate(lambda n: n*2) >>> [iter.next() for n in xrange(10)] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Return the first n elements of seq:
>>> take(3, [1, 2, 3, 4]) [1, 2, 3]>>> take(3, []) []