informations about subtree
This commit is contained in:
parent
aec7377780
commit
bd2eef0cf7
BIN
src/__pycache__/pretty_print.cpython-39.pyc
Normal file
BIN
src/__pycache__/pretty_print.cpython-39.pyc
Normal file
Binary file not shown.
Binary file not shown.
5
src/pretty_print.py
Normal file
5
src/pretty_print.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
def pp(json_obj, indent=2):
|
||||||
|
dumps = json.dumps(json_obj, indent=2)
|
||||||
|
print(dumps)
|
35
src/tree.py
35
src/tree.py
@ -119,6 +119,41 @@ class NerdTree():
|
|||||||
|
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
def compute_aggregate_recursively(self, node=None):
|
||||||
|
'''
|
||||||
|
node is the rootnode of the subtree
|
||||||
|
at the moment compute size and number of items of subtree starting from node
|
||||||
|
'''
|
||||||
|
|
||||||
|
subtree = {
|
||||||
|
'subtree_size' : 0,
|
||||||
|
'subtree_items' : 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
node = node or self.json_tree
|
||||||
|
|
||||||
|
for item in node.get('children') or []:
|
||||||
|
|
||||||
|
if item['type'] == 'd':
|
||||||
|
if item.get('subtree_size') is None:
|
||||||
|
subtree_compute = self.compute_aggregate_recursively(item)
|
||||||
|
else:
|
||||||
|
subtree_compute = {
|
||||||
|
'subtree_size' : item['subtree_size'],
|
||||||
|
'subtree_items' : item['subtree_items'],
|
||||||
|
}
|
||||||
|
## if you want count the current directory as item you must sum 1
|
||||||
|
## if you want count the current directory size you must include item['size']
|
||||||
|
subtree['subtree_size'] += subtree_compute['subtree_size'] + item['size']
|
||||||
|
subtree['subtree_items'] += subtree_compute['subtree_items'] + 1
|
||||||
|
else:
|
||||||
|
subtree['subtree_size'] += item['size']
|
||||||
|
subtree['subtree_items'] += 1
|
||||||
|
|
||||||
|
# return subtree
|
||||||
|
node.update(subtree)
|
||||||
|
return subtree
|
||||||
|
|
||||||
def tree_from_struct(self, rootnode, prec_seps=[]):
|
def tree_from_struct(self, rootnode, prec_seps=[]):
|
||||||
'''
|
'''
|
||||||
recursively produce a string for representing the tree
|
recursively produce a string for representing the tree
|
||||||
|
Loading…
x
Reference in New Issue
Block a user