-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_stuff.py
185 lines (169 loc) · 6.8 KB
/
json_stuff.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from common import *
def test_inheritance(data: dict, root_key='gadget.gadgetId') -> dict:
data_flat = [flatten_json(d) for d in data]
by_root_key = {}
for d in data_flat:
key = d.get(root_key, 0)
l = by_root_key.get(key, [])
l.append(d)
by_root_key[key] = l
len(by_root_key)
output = {}
for id, gs in by_root_key.items():
template = {k:v for k,v in gs[0].items() if k != root_key}
children = []
for child in gs[1:]:
c = {}
for k,v in child.items():
if k == root_key:
continue
if k not in template or template[k] != v:
c[k] = v
children.append(c)
if len(children):
template['zz_children'] = children
output[id] = template
return output
def get_best_parents(data_flat):
def remaining_props(d, parent) -> int:
r = len(d)
for k,v in d.items():
if k in parent and parent[k] == v:
r -= 1
return r
best_parents = {}
total = len(data_flat)
for i in range(total-1):
print(f'Finding best parent {i+1}/{total-1}')
d = data_flat[-(i+1)]
uid = d['_UID']
min_r = len(d)
best_parent_uid = -1
for j in range(total-i-1):
parent = data_flat[j]
if parent['_UID'] == uid:
continue
r = remaining_props(d, parent)
if r < min_r:
min_r = r
best_parent_uid = parent['_UID']
best_parents[uid] = best_parent_uid
return best_parents
def test_bruteforce_inheritance(data_flat, best_parents) -> list:
def inherit(d, parent, parent_full) -> dict:
child = {}
for k,v in d.items():
if k == '_UID':
continue
if not (k in parent_full and parent_full[k] == v):
child[k] = v
child = unflatten_json(child)
children = parent.get('zz_children', [])
children.append(child)
parent['zz_children'] = children
return child
output = []
lookups = {}
full_parents = {d['_UID']:d for d in data_flat}
for d in data_flat:
uid = d['_UID']
bp = best_parents.get(uid, -1)
if bp > -1 and bp in lookups:
lookups[uid] = inherit(d, lookups[bp], full_parents[bp])
else:
d2 = {k:v for k,v in d.items() if k!='_UID'}
d2 = unflatten_json(d2)
output.append(d2)
lookups[uid] = d2
return output
# def turn_progenitors_into_groups(data):
# if isinstance(data, list):
# for item in list:
# turn_progenitors_into_groups(item)
# elif isinstance(data, dict):
# if 'zz_children' not in data:
# return
data = load_json('3.json')
data_flat_unsorted = [flatten_json(d, flatten_keys=['gadget', 'motionInfo'], flatten_xyz=True, stripped_keys=['entityId']) for d in data]
data_flat = sorted(data_flat_unsorted, key=lambda x: (len(x), x.get('gadget.gadgetId', 0)))
# for i, d in enumerate(data_flat):
# d['_UID'] = i
# best_parents = get_best_parents(data_flat)
# df4 = test_bruteforce_inheritance(data_flat, best_parents)
#save_json(df4, '3.flatdense.json')
# s = json.dumps(df4, ensure_ascii=False)
# s2 = s.replace('},', '},\n')
# s3 = s2.replace(' ', '').replace('"zz_children"', '\n"zz"').replace('configId', 'c').replace('groupId', 'g')
# with open_write('3.flatdense.json', True) as f:
# f.write(s3)
# o = turn_progenitors_into_groups(load_json('3.df4.json'))
def format_dump(input) -> str:
MAX_LINE = 130
def format_d(data, indent=0) -> list:
prefix = '\t' * indent
if isinstance(data, dict):
s = json.dumps(data) # str(data)
if len(s) < MAX_LINE: # Inline
return [prefix+s]
else: # Expand all elements
output = [prefix+'{']
prefix2 = prefix+'\t'
for k,v in data.items():
if k == 'zz_children':
output.append(f'{prefix2}"children": [')
for value in v:
output += format_d(value, indent+2)
output[-1] = output[-1] + ','
output.append(f'{prefix2}],')
else:
s = json.dumps(v) # str(v)
if len(s) < MAX_LINE:
output.append(f'{prefix2}"{k}": {s},')
else:
output.append(f'{prefix2}"{k}":')
output += format_d(v, indent+1)
output.append(prefix+'},')
return output
elif isinstance(data, list):
s = json.dumps(data) # str(data)
if len(s) < MAX_LINE: # Inline
return [prefix+s]
else: # Expand all elements
output = [prefix+'[']
for value in data:
output += format_d(value, indent+1)
output.append(prefix+'],')
return output
else:
return [prefix + str(data)]
return '\n'.join(format_d(input))
# with open_write('3.flatdense2.json', True) as f:
# f.write(format_dump(df4))
data = load_json('3.json')
data_flat = sorted([flatten_json(
d, flatten_keys=['gadget', 'motionInfo', 'gatherGadget'], flatten_xyz=True, flatten_lists=False) for d in data],
key=lambda x: (len(x), x.get('gadgetId', 0)))
df = pd.DataFrame(data_flat)
df1 = df.loc[:,df.nunique(dropna=False)!=1].sort_values(['itemId', 'groupId', 'entityId', 'configId', 'gadgetId'])
# df1[df1.nunique().sort_values().index].to_excel('3.xlsx')
# gatherable_counts = df['gatherGadget.itemId'].value_counts()
# {handbook_items.get(int(x), int(x)):y for x,y in gatherable_counts.sort_values().iteritems()}
def make_gatherable_templates(df: pd.DataFrame):
def make_template(df, name):
common_values = df.loc[:, df.nunique(dropna=False) == 1]
varying_values = df.loc[:, df.nunique(dropna=False) > 1]
output = {'TemplateName': name}
for k,v in common_values.iloc[0].iteritems():
output[k] = v
output['zChildren'] = varying_values.to_dict('records')
return output
gatherable_counts = df['itemId'].value_counts()
one_ofs = gatherable_counts[gatherable_counts == 1].index
multiple_ofs = gatherable_counts[gatherable_counts > 1].index
outputs = []
for gatherable_id in multiple_ofs:
name = handbook_items.get(int(gatherable_id), gatherable_id)
outputs.append(make_template(df[df['itemId'] == gatherable_id], name))
outputs.append(make_template(df[df['itemId'].isin(one_ofs)], 'one_ofs'))
outputs.append(make_template(df[df['itemId'].isnull()], 'NaNs'))
return outputs