-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeviewer.py
More file actions
40 lines (35 loc) · 1.08 KB
/
treeviewer.py
File metadata and controls
40 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from contextlib import redirect_stdout
def startTree(c) -> None:
if isinstance(c, dict):
treeViewDict(c)
elif isinstance(c, list):
treeViewList(c)
else:
print(c)
def treeViewDict(d: dict, prefix='') -> None:
prefix += '-'
for k, v in d.items():
suffix = f"[\"{k}\"]:"
printValues(v, prefix, suffix)
def treeViewList(l: list, prefix='') -> None:
prefix += '>'
for i, v in enumerate(l):
suffix = f"[{i}]:"
printValues(v, prefix, suffix)
def printValues(c, prefix: str, id: str) -> None:
spaces = (len(prefix)-1) * ' '
if isinstance(c, dict):
print(''.join([spaces, prefix[1:], id]))
treeViewDict(c, prefix)
elif isinstance(c, list):
print(''.join([spaces, prefix[1:], id]))
treeViewList(c, prefix)
else:
print(''.join([spaces, prefix[1:], id]), type(c).__name__)
def treeView(obj, output=None) -> None:
if not output:
startTree(obj)
else:
with open(output, 'w') as file:
with redirect_stdout(file):
startTree(obj)