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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
global_max = 0
def produce_res_rec(result_pack):
"""
A beautiful recursive tree like hash producer
"""
send_list = []
global global_max
#print "The pack is counter:",counter
#print "The pack is result_pack:",result_pack
#print "The pack is global_result:",global_result
#the final step of the execution
if type(result_pack) != list and type(result_pack) != dict:
global_max = global_max + 1
return {'id':global_max,'text':str(result_pack)}
elif type(result_pack) == list :
for result_list in result_pack:
#counter = 2*counter
if type(result_list) == list:
#if there is a new list then the new parent trick
global_max = global_max +1
tmp_parent = {}
tmp_parent['id'] = global_max
tmp_parent['text'] = 'leaf_result%s%s'%(global_max,global_max)
tmp_parent['item'] = []
tmp_list_result = produce_res_rec(result_list)
if type(tmp_list_result) == list:
tmp_parent['item'].extend(tmp_list_result)
else:
tmp_parent['item'].append(tmp_list_result)
#appended to the parent
send_list.append(tmp_parent)
else:
tmp_list_result = produce_res_rec(result_list)
if type(tmp_list_result) == list:
send_list.extend(tmp_list_result)
else:
send_list.append(tmp_list_result)
elif type(result_pack) == dict :
for key_result,value_result in result_pack.iteritems():
#a new key added
global_max = global_max +1
tmp_parent = {}
#counter = 2*counter+1
tmp_parent ['id'] = global_max
tmp_parent ['text'] = str(key_result)
tmp_parent ['item'] = []
tmp_dict_res = produce_res_rec(value_result)
if type(tmp_dict_res) == list :
tmp_parent['item'].extend(tmp_dict_res)
else:
tmp_parent['item'].append(tmp_dict_res)
send_list.append(tmp_parent)
else: #shouldnt come here !
return {}
return send_list
if __name__ == "__main__":
"""
main_pack = {
'minion':[["one","two"],["three","four"]]
}
main_pack = {
'minion':{
'result1':True,
'result2':False
},
'minion2':{
'result3':True,
'result4':False
},
'minion3':{
'result5':True,
'result6':False
}
}
"""
main_pack = {
'minion1':[
{
'res1':[['hey','hhhey'],['mey','mmmey']]
},
{
'res2':['wey','dey']
}
],
'minion2':[
{
'res3':['nums','mums']
},
{
'res4':['wums','dums']
}
]
}
final = produce_res_rec(main_pack)
print "The final pack is like that : "
print final
|