Return to index

Module rstdoc.misclibs.functional


Module functional

Iterator and functional programming tools. Most of the iterator methods were copied from the Python itertools documentation with added docstrings to show example usage.

Imports:
itertools, operator, unittest
Classes:
Forever(object): Iterator that loops forever over a sequence.
Methods:
all(seq, pred=bool): Returns True if pred(x) is True for every element in the iterable.
any(seq, pred=bool): Returns True if pred(x) is True for at least one element in the iterable:
attrdeleter(attr): Returns a function f such that calling f(ob) deletes ob.attr:
attrgetter(attr): Returns a function f such that calling f(ob) returns ob.attr:
attrsetter(attr): Returns a function f such that calling f(ob,value) sets ob.attr = value:
dotproduct(vec1, vec2): Calculate the dot product of two vectors:
exhaust(seq): Iterate thorugh all items in seq discarding return values.
first(seq): Return the first item in seq and discard the rest:
flatten(*seqs): Create a single list from seqs:
forever(iter): Generator that loops forever over a sequence.
grouper(n, iterable, padvalue=None): Groups iterable into tuples of n elements:
ienumerate(iterable): Returns a iterator that yields (0, item1), (1, item2), ...:
itemdeleter(item): Returns a function f such that calling f(ob) deletes f[item]:
itemgetter(item): Returns a function f such that calling f(ob) returns ob[item]:
itemsetter(item): Returns a function f such that calling f(ob,key) sets ob[attr] = value:
iteritems(mapping): Returns a iterator yielding (key, value) pairs from mapping:
ncycles(seq, n): Returns the sequence elements n times:
no(seq, pred=bool): Returns True if pred(x) is False for every element in the iterable.
nth(iterable, n): Returns the nth item of iterable.
pad(seq, padvalue=None): Returns the sequence elements and then returns None indefinitely. Useful for emulating the behavior of the built-in map() function.
pairwise(iterable): Group iterable into pairs:
quantify(seq, pred=bool): Count how many times the predicate is True in the sequence.
repeatfunc(func, times=None, *args): Repeat calls to func with specified arguments:
tabulate(function): Return function(0), function(1), ...:
take(n, seq): Return the first n elements of seq:
testSuite(): Returns doctest testsuite for module.

Class functional.Forever

Back to functional

Bases:
object

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]
Methods:
__init__(self, items):
__iter__(self):
next(self):

Method functional.all

Back to functional

Arguments:
seq, pred=bool

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

Method functional.any

Back to functional

Arguments:
seq, pred=bool

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

Method functional.attrdeleter

Back to functional

Arguments:
attr

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'

Method functional.attrgetter

Back to functional

Arguments:
attr

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'

Method functional.attrsetter

Back to functional

Arguments:
attr

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'

Method functional.dotproduct

Back to functional

Arguments:
vec1, vec2

Calculate the dot product of two vectors:

>>> dotproduct([1, 2, 3], [4, 5, 6])
32

Method functional.exhaust

Back to functional

Arguments:
seq

Iterate thorugh all items in seq discarding return values.

>>> test = xrange(10)
>>> print exhaust(test)
None

Method functional.first

Back to functional

Arguments:
seq

Return the first item in seq and discard the rest:

>>> first([1, 2, 3])
1
>>> print first([])
Traceback (most recent call last):
    ...
StopIteration

Method functional.flatten

Back to functional

Arguments:
*seqs

Create a single list from seqs:

>>> flatten([1, 2, 3], (4, 5, 6))
[1, 2, 3, 4, 5, 6]

Method functional.forever

Back to functional

Arguments:
iter

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]

Method functional.grouper

Back to functional

Arguments:
n, iterable, padvalue=None

Groups iterable into tuples of n elements:

>>> list(grouper(3, 'abcdefg', 'x'))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]

Method functional.ienumerate

Back to functional

Arguments:
iterable

Returns a iterator that yields (0, item1), (1, item2), ...:

>>> list(ienumerate([1, 2, 3]))
[(0, 1), (1, 2), (2, 3)]
>>> list(ienumerate([]))
[]

Method functional.itemdeleter

Back to functional

Arguments:
item

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'

Method functional.itemgetter

Back to functional

Arguments:
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'

Method functional.itemsetter

Back to functional

Arguments:
item

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'

Method functional.iteritems

Back to functional

Arguments:
mapping

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)]

Method functional.ncycles

Back to functional

Arguments:
seq, n

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))
[]

Method functional.no

Back to functional

Arguments:
seq, pred=bool

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

Method functional.nth

Back to functional

Arguments:
iterable, n

Returns the nth item of iterable.

>>> nth([1, 2, 3], 1)
:changeset:`2 [2]`

Method functional.pad

Back to functional

Arguments:
seq, padvalue=None

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']

Method functional.pairwise

Back to functional

Arguments:
iterable

Group iterable into pairs:

>>> list(pairwise([1, 2, 3, 4]))
[(1, 2), (2, 3), (3, 4)]

Method functional.quantify

Back to functional

Arguments:
seq, pred=bool

Count how many times the predicate is True in the sequence.

>>> quantify([True, False, True])
2

Method functional.repeatfunc

Back to functional

Arguments:
func, times=None, *args

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]

Method functional.tabulate

Back to functional

Arguments:
function

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]

Method functional.take

Back to functional

Arguments:
n, seq

Return the first n elements of seq:

>>> take(3, [1, 2, 3, 4])
[1, 2, 3]
>>> take(3, [])
[]

Method functional.testSuite

Back to functional

Arguments:

Returns doctest testsuite for module.

Imports:
doctest