-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatistics.py
63 lines (52 loc) · 1.92 KB
/
statistics.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import json
import argparse
def load_month_json(year):
path = 'data/' + str(year)
files = os.listdir(path)
try:
files.remove('average.json')
except ValueError:
pass
return sorted(files)
def average_month(year, month):
path = 'data/' + str(year) + '/' + str(year) + str(month).zfill(2) + '.json'
data = json.loads(open(path).read())
orange_line_people, red_line_people, total_people = 0, 0, 0
for v in data:
orange_line_people = orange_line_people + int(v['orange_line_people'])
red_line_people = red_line_people + int(v['red_line_people'])
total_people = total_people + int(v['total_people'])
return ((orange_line_people, orange_line_people / len(data)),
(red_line_people, red_line_people / len(data)),
(total_people, total_people / len(data)))
def save_average_by_year(year):
data = []
month = 1
while month <= 12:
try:
v = average_month(year, month)
data.append({'year': year, 'month': month,
't_orange_line_people': v[0][0],
'a_orange_line_people': v[0][1],
't_red_line_people': v[1][0],
'a_red_line_people': v[1][1],
't_total_people': v[2][0],
'a_total_people': v[2][1]
})
except:
print 'Error on month ' + str(month)
month += 1
return data
def run():
parser = argparse.ArgumentParser(description='Print argument')
parser.add_argument("-y", "--year", help="set year")
args = parser.parse_args()
data = save_average_by_year(args.year)
with open('data/' + args.year + '/average.json', 'w') as json_file:
json.dump(data, json_file, indent=4, ensure_ascii=False)
json_file.close()
if __name__ == '__main__':
run()