1 from pysize
.core
import compute_size
2 from pysize
.core
.pysize_fs_node
import create_node
4 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/231503
6 def _breadth_first(tree
, children
=iter):
7 """Traverse the nodes of a tree in breadth-first order.
8 The first argument should be the tree root; children
9 should be a function taking as argument a tree node and
10 returning an iterator of the node's children.
14 for node
in _breadth_first(tree
, children
):
15 for child
in children(node
):
22 """The entry point to a tree of pysize_node.
23 min_size < 1.0 => ratio to total size."""
24 def __init__(self
, paths
, max_depth
=1, min_size
=0.0):
25 self
.fullpaths
= paths
26 if paths
and min_size
<= 1.0:
27 min_size
*= sum(map(compute_size
.slow
, paths
))
28 self
.root
= create_node(None, paths
, max_depth
, min_size
)
29 self
.height
= self
.root
.compute_height()
31 def get_next_sibling(self
, node
):
32 """Return the next pysize_node in node's level."""
34 for child
in _breadth_first(self
.root
, lambda c
: c
.children
):
36 if child
.compute_depth() == node
.compute_depth():
39 is_next
= child
is node
41 def get_previous_sibling(self
, node
):
42 """Return the previous pysize_node in node's level."""
44 for child
in _breadth_first(self
.root
, lambda c
: c
.children
):
46 if prev
.compute_depth() == node
.compute_depth():
51 def get_first_child(self
, node
):
52 """Return the first pysize_node in node's children."""
54 return node
.children
[0]
56 def get_parent(self
, node
):
57 """Return the saved parent of node."""
61 tree
= pysize_tree(['/usr/share/doc'], 4, 0.1)
62 print 'height', tree
.height
64 if __name__
== '__main__':