-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproducts_refactor.py
More file actions
108 lines (68 loc) · 2.22 KB
/
products_refactor.py
File metadata and controls
108 lines (68 loc) · 2.22 KB
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
import json
import uuid
# FIRST FIX Products
file_name = './stub_json/Products.json'
file = open(file_name, 'r')
data = json.loads(file.read())
file.close()
products = data['products']
new_dict = {'products': {}}
product_id_to_hash_map = {}
counter = 0
for key in products:
product = products[key]
uniq_id = key
new_dict['products'][str(uniq_id)] = product
product['uniq_id'] = str(uniq_id)
name = product['product_id']
if name not in product_id_to_hash_map:
product_id_to_hash_map[name] = str(uniq_id)
else:
counter += 1
print('There are {} products in the products table with duplicate product_ids'.format(counter))
with open(file_name, 'w') as outfile:
json.dump(new_dict, outfile)
# NOW FIX Shipments W/NEW Product IDS
file_name = './stub_json/Shipments.json'
file = open(file_name, 'r')
data = json.loads(file.read())
file.close()
shipments = data['shipments']
new_dict = {'shipments': {}}
counter = 0
for key in shipments:
items = shipments[key]['ship_items']
for item in items:
name = item['product']
try:
uniq_id = product_id_to_hash_map[name]
except:
counter += 1
uniq_id = 'INVALID PRODUCT ID'
item['product'] = uniq_id
new_dict['shipments'][key] = shipments[key]
print('{} shipments have product_ids listed that do not exist in the products table'.format(counter))
with open(file_name, 'w') as outfile:
json.dump(new_dict, outfile)
# NOW FIX Receipts W/NEW Product IDS
file_name = './stub_json/Contributions.json'
file = open(file_name, 'r')
data = json.loads(file.read())
file.close()
receipts = data['contributions']
new_dict = {'contributions': {}}
counter = 0
for key in receipts:
items = receipts[key]['receive_items']
for item in items:
name = item['product']
try:
uniq_id = product_id_to_hash_map[name]
except:
counter += 1
uniq_id = 'INVALID PRODUCT ID'
item['product'] = uniq_id
new_dict['contributions'][key] = receipts[key]
print('{} receipts have product_ids listed that do not exist in the products table'.format(counter))
with open(file_name, 'w') as outfile:
json.dump(new_dict, outfile)