-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path03_build_data.py
66 lines (51 loc) · 1.85 KB
/
03_build_data.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
# coding=utf8
import json
from collections import namedtuple
geocodes = json.load(open('data/geocodes.json'))
data = json.load(open('data/data.json'))
CongressMan = namedtuple('CongressMan', ['name', 'party'])
Restaurant = namedtuple('Restaurant', ['name', 'kind', 'address', 'location'])
Eat = namedtuple('Eat', ['congress_man', 'restaurant', 'price'])
men = []
restaurants = []
eats = []
for man_name, party, restaurant_name, address, kind, price, memo in data:
if address in geocodes:
location = {
'lat': geocodes[address]['lat'],
'lng': geocodes[address]['lng'],
}
else:
location = None
man = CongressMan(man_name, party)
restaurant = Restaurant(restaurant_name, kind, address, location)
if man not in men:
men.append(man)
if restaurant not in restaurants:
restaurants.append(restaurant)
eat = Eat(men.index(man), restaurants.index(restaurant), price)
print(man)
print(restaurant)
eats.append(eat)
print(eats)
# by restaurant
search_by_restaurant = {}
for eat in eats:
restaurant = eat.restaurant
if restaurant not in search_by_restaurant:
search_by_restaurant[restaurant] = []
search_by_restaurant[restaurant].append(eat)
restaurant_loves_by_congressman = [
{
'name': restaurants[x].name,
'kind': restaurants[x].kind,
'address': restaurants[x].address,
'location': restaurants[x].location,
'totalPrice': sum(x.price for x in search_by_restaurant[x]),
'times': len(search_by_restaurant[x]),
}
for x in reversed(sorted(search_by_restaurant, key=lambda k: len(search_by_restaurant[k])))
if restaurants[x].location and 'lat' in restaurants[x].location
]
print(restaurant_loves_by_congressman)
json.dump({'data': restaurant_loves_by_congressman}, open('static/build/data/restaurant.json', 'w+'))