-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_formater.py
47 lines (36 loc) · 1.53 KB
/
data_formater.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
import json
import logging
from const import MAX_L_FILENAMES_DATA_FORMATER as MAX
# Init a logger for file errors
logger = logging.getLogger(__name__)
# Entry point to data formatter
# entry point accepts as filenames as an argument
def entry_point(l_filenames):
# Init data structures
record = dict()
zipSet = set()
try:
# test to see l_filenames is correctly sized or not
# raise error if it is not correctly sized
if len(l_filenames) > MAX or len(l_filenames) < MAX:
raise ValueError('This function must pass in a list with 2 values. It failed to do so', f'Size of l_filenames: {len(l_filenames)}' )
# Decompose filename list into zipset file, json file and records file
jsonFile, zipcodeFile, recordsJsonFile = l_filenames
with open(jsonFile) as json_:
data = json.load(json_)
# print(data)
for item in data:
if item['zip_code'] in zipSet:
record[item['zip_code']].append(item['price'])
else:
zipSet.add(item['zip_code'])
record[item['zip_code']] = list()
record[item['zip_code']].append(item['price'])
zresults = {'zipcodes':zipSet}
with open(zipcodeFile, 'w+') as f:
for z in zipSet:
f.write("%s\n" % z)
with open(recordsJsonFile, 'w+') as f:
json.dump(record, f)
except ValueError as error:
logger.error(error)