Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. # grenka 2018
  2. # by : 9kin
  3. # ldld - list of dictionary and list to dict
  4. # example : [{"$s1":["s11"]}, ["s2"]} -> {key1 : "s1" : ["s11"], key2 : ["s2"]}
  5. import json
  6. import dpath.util
  7. from graphviz import Graph
  8.  
  9.  
  10. class BeautifulJson:
  11. def __init__(self):
  12. pass
  13.  
  14. def jwirite(self, data, filepath):
  15. with open(filepath, 'w', encoding='utf-8') as fh:
  16. fh.write(json.dumps(data, ensure_ascii=False))
  17.  
  18. def jread(self, filepatch):
  19. with open(filepatch, 'r', encoding='utf-8') as fh:
  20. return json.load(fh)
  21.  
  22.  
  23. class Gr:
  24. def __init__(self, data):
  25. self.tree = {**data}
  26.  
  27. def get_list_of_keys(self, data):
  28. return list(iter(data.keys()))
  29.  
  30. def merge_dicts(self, list):
  31. res = {}
  32. for i in range(len(list)):
  33. res = {**res, **list[i]}
  34. return res
  35.  
  36. def ldld(self, key, nkey):
  37. res = {}
  38. s = dpath.util.get(self.tree, key)
  39. for i in range(len(s)):
  40. res = {**res, nkey[i]: s[i]}
  41. return res
  42.  
  43. def is_leaf(self, key):
  44. try:
  45. return not (type(dpath.util.get(self.tree, key)) == dict)
  46. except:
  47. return True
  48.  
  49. def ldld_all_tree(self, where, datalist):
  50. for i in range(len(datalist)):
  51. ldld_c, ldld_f, ldld_s, ldld_r = datalist[i][0], datalist[i][1][0], datalist[i][1][1], datalist[i][2]
  52. try:
  53. res = self.ldld(where + "/" + ldld_c, [ldld_f, ldld_s])
  54. dpath.util.delete(self.tree, where + "/" + ldld_c)
  55. dpath.util.new(self.tree, where + "/" + ldld_r, res)
  56. except:
  57. pass
  58.  
  59. def build_tree(self, where, pr):
  60. l = [["pl", ("func", "line"), "parallel"], ["do", ("func", "line"), "do"], ["plan", ("func", "line"), "plan"]]
  61. if self.is_leaf(where) == False:
  62. print(where, pr)
  63. self.ldld_all_tree(where, l)
  64. cur = dpath.util.get(self.tree, where)
  65. if cur.get("plan") != None:
  66. dpath.util.new(self.tree, where + "SYS", 1)
  67. self.build_tree(where + "/plan/func", where)
  68. if cur.get("parallel") != None:
  69. dpath.util.new(self.tree, where + "SYS", 2)
  70. self.build_tree(where + "/parallel/func", where)
  71. if cur.get("do") != None:
  72. dpath.util.new(self.tree, where + "SYS", 3)
  73. self.build_tree(where + "/do/func", where)
  74.  
  75. def call(self, where, x):
  76. dpath.util.new(self.tree, where + "/CALL", [x])
  77.  
  78.  
  79.  
  80.  
  81. j = BeautifulJson()
  82. data = j.jread("__init__.grenka")
  83. gr = Gr(data)
  84.  
  85. #gr.ldld_all_tree("$main", [["pl", ("func", "line"), "parallel"], ["do", ("func", "line"), "do"]])
  86. #gr.ldld_all_tree("$main/parallel/func", [["pl", ("func", "line"), "parallel"]])
  87. #gr.build_tree("$main")
  88. #gr.build_tree("$git")
  89. for i in gr.get_list_of_keys(gr.tree):
  90. gr.build_tree(i, "test")
  91. #gr.call(i)
  92. print(json.dumps(gr.tree, indent=4))
  93.  
  94. '''
  95. {
  96. "$main": {
  97. "pl" : [{"do" : [{}, ["1"]], "plan" : [{}, []]}, []]
  98. }
  99. }
  100. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement