-
Notifications
You must be signed in to change notification settings - Fork 2
/
dict_tree.py
41 lines (32 loc) · 997 Bytes
/
dict_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
# -*- coding: utf-8 -*-
import json
from collections import OrderedDict
import os
import re
import pdb
from node.node import node
class dict_tree(OrderedDict):
def __init__(self):
pass
def get_json(self):
return dict(self)
def get_string(self):
return json.dumps(self, ensure_ascii=False, indent='\t')
def loads(self, rawText):
od = json.loads(rawText, object_pairs_hook=OrderedDict)
for x in od:
self[x] = od[x]
def load_from_file(self, filepath):
rawLines = ''
with open(filepath, 'r', encoding='utf-8-sig') as f:
while True:
r = f.readline()
if not r:
break
if r and r[0] == '#':
continue
rawLines += r
self.loads(rawLines)
def save_to_file(self, filepath):
with open(filepath, 'w', encoding='utf-8-sig') as f:
f.write(self.getString())