-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty_print_tree.py
50 lines (40 loc) · 961 Bytes
/
pretty_print_tree.py
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
41
42
43
44
45
46
47
48
49
50
#! /usr/bin/python
import sys, json, pprint
"""
Pretty print a tree from json.
"""
'''Extending pprint'''
class Node:
"""
Dummy class for python's pretty printer.
"""
def __init__(self, name): self.name = name
def __repr__(self): return self.name
def format_tree(tree):
"""
Convert a tree with strings, to one with nodes.
"""
tree[0] = Node(tree[0])
if len(tree) == 2:
tree[1] = Node(tree[1])
elif len(tree) == 3:
format_tree(tree[1])
format_tree(tree[2])
def pretty_print_tree(tree):
"""
Print out a tree with nice formatting.
"""
format_tree(tree)
print pprint.pformat(tree)
def main(parse_file):
for l in open(parse_file):
pretty_print_tree(json.loads(l))
def usage():
sys.stderr.write("""
Usage: python pretty_print_tree.py [tree_file]
Pretty print a file of trees.\n""")
if __name__ == "__main__":
if len(sys.argv) != 2:
usage()
sys.exit(1)
main(sys.argv[1])