From bd2eef0cf7c0bca064d0556951c6ac72438942c2 Mon Sep 17 00:00:00 2001 From: Amber Date: Fri, 26 Jan 2024 12:36:01 +0100 Subject: [PATCH] informations about subtree --- src/__pycache__/pretty_print.cpython-39.pyc | Bin 0 -> 330 bytes src/__pycache__/tree.cpython-39.pyc | Bin 3583 -> 4237 bytes src/pretty_print.py | 5 +++ src/tree.py | 35 ++++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 src/__pycache__/pretty_print.cpython-39.pyc create mode 100644 src/pretty_print.py diff --git a/src/__pycache__/pretty_print.cpython-39.pyc b/src/__pycache__/pretty_print.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8d1da4a0d0708d3bdadee153b159dc980cc7f0f7 GIT binary patch literal 330 zcmYjLyK2Kg5WGD}j$&}i6#0Uv$^AeG2KR2#sS%3yz!rKOd#A!+S8oxg0&^IMi{B)qJd{+y`UjSvS~zkE>JNR=K_B%y0M~L!vL%TY5EuIa?GTD2rmY``nBmkrA2UaL zam!rXYJ6)+`YB`iRCL4R=h5uO-kNS|YBN~EDv-6tQ)3+25DwMc@#GFsJ11!`?g)^f=H=~N{{p~8K$rjk literal 0 HcmV?d00001 diff --git a/src/__pycache__/tree.cpython-39.pyc b/src/__pycache__/tree.cpython-39.pyc index 2aa560adc5ab0aa1056b9fe21e62492f58cabbdb..12946417c80691c06441d0b1cbeb495c70545254 100644 GIT binary patch delta 839 zcmaJ-J8u&~5Z>8)_;L?BaeycxNSgqW4h2NPLm-p_XpoQ_D3B$vF5dNF$ob6P842qw z3q(s19j^kU=7$R>c%8f4dkRe}Bpu*YUc-2b(=O>yKFwz^v$=DF__I5Q;A3UnXjA1< zE(6aB?kl*HF9;m)qi^VlX3(!^}N?D$h6xFSx3eZQ#TZr67W%d zI&n+gxTVY{UYWKEA9)N#a#;aWa0a0v&#Lgaf)xZ`msJ%nB212yleK~{lp+jL(UPJ) zkV*HU*myKC2Yrs5Z^)Wb>uQ>2rMPk|gj_(fS=*nWw35@xy?_}NokJMnPSjYWLV@m@ zhE40#g(|q33FfY8F2Vvm0CmlV;YveDeiL8v)PHB&`C+|+db8G!H)7f9#4-xfRLExj z*!~U|^IJ~GSi}3?=AWG3u$I4fPs7*zr@LzS(D0pn+Pmzi$)pm=lleDqE{{EXNzHA6 bUa4A^Yt`Iq_EC3LT1fhioW-M=zxKkvc(u^e delta 171 zcmeBG{4dRy$ji&c00dJTHl?NuPUKt17&39Mr9wJG8e?&*v|xA>FubMlMg)AEaQ6H7``i*8M3T8>^{GmJReXE2O|qR9|s2y2S1M|k4_N> RP)L)zNEk@nnye;}4gl+CE5`r; diff --git a/src/pretty_print.py b/src/pretty_print.py new file mode 100644 index 0000000..778708d --- /dev/null +++ b/src/pretty_print.py @@ -0,0 +1,5 @@ +import json + +def pp(json_obj, indent=2): + dumps = json.dumps(json_obj, indent=2) + print(dumps) diff --git a/src/tree.py b/src/tree.py index 33f49a0..51bde54 100644 --- a/src/tree.py +++ b/src/tree.py @@ -119,6 +119,41 @@ class NerdTree(): 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=[]): ''' recursively produce a string for representing the tree