Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # grenka 2018
- # by : 9kin
- # ldld - list of dictionary and list to dict
- # example : [{"$s1":["s11"]}, ["s2"]} -> {key1 : "s1" : ["s11"], key2 : ["s2"]}
- import json
- import dpath.util
- from graphviz import Graph
- class BeautifulJson:
- def __init__(self):
- pass
- def jwirite(self, data, filepath):
- with open(filepath, 'w', encoding='utf-8') as fh:
- fh.write(json.dumps(data, ensure_ascii=False))
- def jread(self, filepatch):
- with open(filepatch, 'r', encoding='utf-8') as fh:
- return json.load(fh)
- class Gr:
- def __init__(self, data):
- self.tree = {**data}
- def get_list_of_keys(self, data):
- return list(iter(data.keys()))
- def merge_dicts(self, list):
- res = {}
- for i in range(len(list)):
- res = {**res, **list[i]}
- return res
- def ldld(self, key, nkey):
- res = {}
- s = dpath.util.get(self.tree, key)
- for i in range(len(s)):
- res = {**res, nkey[i]: s[i]}
- return res
- def is_leaf(self, key):
- try:
- return not (type(dpath.util.get(self.tree, key)) == dict)
- except:
- return True
- def ldld_all_tree(self, where, datalist):
- for i in range(len(datalist)):
- ldld_c, ldld_f, ldld_s, ldld_r = datalist[i][0], datalist[i][1][0], datalist[i][1][1], datalist[i][2]
- try:
- res = self.ldld(where + "/" + ldld_c, [ldld_f, ldld_s])
- dpath.util.delete(self.tree, where + "/" + ldld_c)
- dpath.util.new(self.tree, where + "/" + ldld_r, res)
- except:
- pass
- def build_tree(self, where, pr):
- l = [["pl", ("func", "line"), "parallel"], ["do", ("func", "line"), "do"], ["plan", ("func", "line"), "plan"]]
- if self.is_leaf(where) == False:
- print(where, pr)
- self.ldld_all_tree(where, l)
- cur = dpath.util.get(self.tree, where)
- if cur.get("plan") != None:
- dpath.util.new(self.tree, where + "SYS", 1)
- self.build_tree(where + "/plan/func", where)
- if cur.get("parallel") != None:
- dpath.util.new(self.tree, where + "SYS", 2)
- self.build_tree(where + "/parallel/func", where)
- if cur.get("do") != None:
- dpath.util.new(self.tree, where + "SYS", 3)
- self.build_tree(where + "/do/func", where)
- def call(self, where, x):
- dpath.util.new(self.tree, where + "/CALL", [x])
- j = BeautifulJson()
- data = j.jread("__init__.grenka")
- gr = Gr(data)
- #gr.ldld_all_tree("$main", [["pl", ("func", "line"), "parallel"], ["do", ("func", "line"), "do"]])
- #gr.ldld_all_tree("$main/parallel/func", [["pl", ("func", "line"), "parallel"]])
- #gr.build_tree("$main")
- #gr.build_tree("$git")
- for i in gr.get_list_of_keys(gr.tree):
- gr.build_tree(i, "test")
- #gr.call(i)
- print(json.dumps(gr.tree, indent=4))
- '''
- {
- "$main": {
- "pl" : [{"do" : [{}, ["1"]], "plan" : [{}, []]}, []]
- }
- }
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement