API Reference for Enthought Tool Suite 2.7.1
Functions on trees, i.e. nested sequences
Flatten a tree, i.e. 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]
Test whether a tree is a fork (instead of a leaf).
...
>>> 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
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]]
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) ()
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'])]
| Local name | Refers to |
|---|---|
| all | enthought.util.sequence.all |
| any | enthought.util.sequence.any |
| concat | enthought.util.sequence.concat |
| is_sequence | enthought.util.sequence.is_sequence |
| partial | enthought.util.functional.partial |
Copyright © 2002-2008 Enthought, Inc.