/
recordsfilter.py
61 lines (52 loc) · 1.9 KB
/
recordsfilter.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
import json
import numericalunits as nu
import numpy as np
import os
import pandas as pd
import sys
COLUMNS = ['start_date_local', 'distance', 'type', 'elapsed_time', 'total_elevation_gain']
def read_dir_json(dirname):
records = list()
for filename in os.listdir(dirname):
with open(os.path.join(dirname, filename)) as f:
records.append(json.loads(f.read()))
df = pd.DataFrame(records, columns=COLUMNS)
df = df.set_index(pd.DatetimeIndex(df['start_date_local']))
df = df.rename(columns={'elapsed_time': 'duration (hours)',
'total_elevation_gain': 'ascent (m)',
'distance': 'distance (km)'})
df['distance (km)'] = df['distance (km)'] * nu.m / nu.km
df['duration (hours)'] = df['duration (hours)'] * nu.s / nu.hour
return df
def apply_filters(filters, data):
if 'month' in filters:
if not 'year' in filters:
print "A month filter must have an associated year"
sys.exit(1)
else:
data = data[filters['year'] + '-' + filters['month']]
elif 'year' in filters:
data = data[filters['year']]
if 'type' in filters:
data = data[data['type'].str.lower() == filters['type'].lower()]
if 'groupby' in filters:
if filters['groupby'] == 'week':
return data.groupby(pd.Grouper(freq='1W'))
if filters['groupby'] == 'month':
return data.groupby(pd.Grouper(freq='1M'))
if filters['groupby'] == 'year':
return data.groupby([data.index.year])
return data.groupby[filters['groupby']]
return data
def main(args):
filters = dict()
for arg in args[1:]:
if '=' in arg:
(k, v) = arg.split('=')
filters[k] = v
else:
data = read_dir_json(arg)
grouped = apply_filters(filters, data)
print grouped.aggregate(np.sum)
if __name__ == '__main__':
sys.exit(main(sys.argv))