module documentation

Functions on trees, that is, nested sequences.

Function flatten Flatten a tree, that is, recursively concatenate nested sequences.
Function is_fork Test whether a tree is a fork (instead of a leaf).
Function tree_embeds ...
Function tree_map Map a function over the leaves of a tree.
Function tree_shape The shape of a tree expressed as nested tuples of nothing.
Function tree_zip Zip recursively.
Variable basestring Undocumented
def flatten(tree, leaves=()): (source)

Flatten a tree, that is, recursively concatenate nested sequences. >>> flatten([1, [[2, 3], 4, 5], [[[[6]]]]]) [1, 2, 3, 4, 5, 6] >>> mixed = [1, [[2], 3], (4, 5), [(6,)], 'abc'] >>> flatten(mixed) [1, 2, 3, 4, 5, 6, 'a', 'b', 'c'] >>> flatten(mixed, leaves=(str,)) [1, 2, 3, 4, 5, 6, 'abc'] >>> flatten(mixed, leaves=(str, tuple)) [1, 2, 3, (4, 5), (6,), 'abc'] >>> flatten([]) [] >>> flatten(1) [1]

def is_fork(x, leaves=()): (source)

Test whether a tree is a fork (instead of a leaf).

def tree_embeds(t, u, leaves=()): (source)

... >>> tree_embeds([1], [2]) True >>> tree_embeds([1], [1,2]) False >>> tree_embeds([1], [[1,2]]) True >>> tree_embeds([1,[2,3]], [[True, False], 'ab']) True >>> tree_embeds([1,[2,3]], [[True, False], 'ab'], leaves=(str,)) False >>> tree_embeds([], [1,2,3]) False >>> tree_embeds(1, [1,2,3]) True

def tree_map(f, tree, leaves=()): (source)

Map a function over the leaves of a tree. >>> tree_map(str, [1,[2,3]]) ['1', ['2', '3']] >>> tree_map(len, ['foo', ['x', 'asdf']]) [[1, 1, 1], [[1], [1, 1, 1, 1]]] >>> tree_map(len, ['foo', ['x', 'asdf']], leaves=(str,)) [3, [1, 4]]

def tree_shape(tree, leaves=()): (source)

The shape of a tree expressed as nested tuples of nothing. >>> tree_shape([1,[2,3]]) == tree_shape([True, 'ab']) True >>> tree_shape([1,[2,3]]) ((), ((), ())) >>> tree_shape(['ab', ['c', 'd']]) (((), ()), (((),), ((),))) >>> tree_shape(['ab', ['c', 'd']], leaves=(str,)) ((), ((), ())) >>> tree_shape(1) ()

def tree_zip(*trees, **kw): (source)

Zip recursively. Returns nested lists with tuples at the bottom. >>> tree_zip([1,[2,3]], [5,[6,7]]) [(1, 5), [(2, 6), (3, 7)]] >>> tree_zip([1,[2,3]], [5,[6,7]], ['a',['b','c']]) [(1, 5, 'a'), [(2, 6, 'b'), (3, 7, 'c')]] >>> tree_zip('foo', 'bar') [('f', 'b'), ('o', 'a'), ('o', 'r')] >>> tree_zip(['foo'], ['bar']) [[('f', 'b'), ('o', 'a'), ('o', 'r')]] >>> tree_zip(['foo'], ['bar'], leaves=(str,)) [('foo', 'bar')] >>> tree_zip('foo', ['bar']) [('f', 'bar')] >>> tree_zip('foo', ['bar'], leaves=(str,)) ('foo', ['bar']) >>> tree_zip(1, 2) (1, 2) >>> tree_zip([1,[2,3]], [5,6,7,8], [['a','b'],['c','d']]) [(1, 5, ['a', 'b']), ([2, 3], 6, ['c', 'd'])]

basestring = (source)

Undocumented