Flatten a tree into a list. Args: tree: iterable or not. If iterable, its elements (child) can also be iterable or not. leaves: list to which the tree leaves are appended (None by default). Returns: A list of all the leaves in the tree.
(tree, leaves=None)
| 108 | |
| 109 | |
| 110 | def flatten_tree(tree, leaves=None): |
| 111 | """Flatten a tree into a list. |
| 112 | |
| 113 | Args: |
| 114 | tree: iterable or not. If iterable, its elements (child) can also be |
| 115 | iterable or not. |
| 116 | leaves: list to which the tree leaves are appended (None by default). |
| 117 | Returns: |
| 118 | A list of all the leaves in the tree. |
| 119 | """ |
| 120 | if leaves is None: |
| 121 | leaves = [] |
| 122 | if isinstance(tree, dict): |
| 123 | for _, child in iteritems(tree): |
| 124 | flatten_tree(child, leaves) |
| 125 | elif is_iterable(tree): |
| 126 | for child in tree: |
| 127 | flatten_tree(child, leaves) |
| 128 | else: |
| 129 | leaves.append(tree) |
| 130 | return leaves |
| 131 | |
| 132 | |
| 133 | def transform_tree(tree, fn, iterable_type=tuple): |
nothing calls this directly
no test coverage detected