Flattens nested clusters to a list, down to the given depth. Cluster((1, Cluster((2, Cluster((3, 4)))))).flatten(1) => [1, 2, Cluster(3, 4)].
(self, depth=1000)
| 1574 | return max([0] + [1 + n.depth for n in self if isinstance(n, Cluster)]) |
| 1575 | |
| 1576 | def flatten(self, depth=1000): |
| 1577 | """ Flattens nested clusters to a list, down to the given depth. |
| 1578 | Cluster((1, Cluster((2, Cluster((3, 4)))))).flatten(1) => [1, 2, Cluster(3, 4)]. |
| 1579 | """ |
| 1580 | a = [] |
| 1581 | for item in self: |
| 1582 | if isinstance(item, Cluster) and depth > 0: |
| 1583 | a.extend(item.flatten(depth-1)) |
| 1584 | else: |
| 1585 | a.append(item) |
| 1586 | return a |
| 1587 | |
| 1588 | def traverse(self, visit=lambda cluster: None): |
| 1589 | """ Calls the given visit() function on this cluster and each nested cluster, breadth-first. |
no test coverage detected