-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelk.py
executable file
·82 lines (52 loc) · 1.49 KB
/
elk.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python3
import pprint
pp = pprint.PrettyPrinter(indent=4)
def main():
structs = [
{'a': '11', 'b': ['77', '88']},
{'a': '22', 'b': ['99', '00']},
{'a': '33', 'b': [{'x': '1'}, {'y': '2'}]},
]
pp.pprint(structs)
docs = structs_to_docs(structs)
print()
pp.pprint(docs)
def structs_to_docs(structs):
while structs_contain_lists(structs):
new_structs = list()
for struct in structs:
new_structs = new_structs + expand_struct_by_list(struct)
structs = new_structs
docs = structs
return docs
def structs_contain_lists(structs):
for struct in structs:
for k in struct.keys():
if type(struct[k]) == list:
return True
return False
def expand_struct_by_list(struct):
k_list = find_first_list(struct)
base_struct = dict()
for k in struct.keys():
if k != k_list:
base_struct.update({k: struct[k]})
new_structs = list()
for elem in struct[k_list]:
new_struct = dict()
new_struct.update(base_struct)
new_struct.update({k_list: elem})
new_structs.append(new_struct)
return new_structs
def struct_contains_lists(struct):
for k in struct.keys():
if type(struct[k]) == list:
return True
return False
def find_first_list(doc):
for k in doc.keys():
if type(doc[k]) == list:
return k
return None
if __name__ == '__main__':
main()