-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_parser.py
123 lines (89 loc) · 3.66 KB
/
data_parser.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
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
109
110
111
112
113
114
115
116
117
118
119
from collections import namedtuple
import pandas as pd
import json
from dataclasses import dataclass
Race = namedtuple('Race',['name','drivers','teams'])
@dataclass
class Racer:
"""Class for keeping track of an item in inventory."""
name: str
points: int
cost: float
def parse_races():
races = []
const_cost = pd.read_csv('data/constructor_value.csv')
driver_cost = pd.read_csv('data/driver_value.csv')
race_names = const_cost.columns[1:]
teams = ['Mercedes','Red Bull','McLaren', 'Ferrari', 'Aston Martin',
'Alpine', 'AlphaTauri', 'Alfa Romeo', 'Williams','Haas']
drivers = ['Lewis Hamilton', 'Max Verstappen', 'Valtteri Bottas', 'Sergio Perez',
'Daniel Ricciardo', 'Charles Leclerc', 'Sebastian Vettel', 'Fernando Alonso',
'Carlos Sainz','Lance Stroll','Lando Norris','Piere Gasly','Esteban Ocon',
'Kimi Raikkonen','Yuki Tsunoda','Antonio Giovinazzi','Nicholas Latifi',
'George Russell','Mick Schumacher','Nikita Mazepin']
for i,r in enumerate(race_names[:-1]):
race_str = r.replace('GP','').replace(' ','').strip().lower()
# plz write clean data...
if race_str == 'french':
race_str = 'france'
elif race_str == 'styrian':
race_str = 'styria'
elif race_str == 'austrian':
race_str = 'austria'
elif race_str == 'british':
race_str = 'britain'
elif race_str == 'dutch':
race_str = 'netherlands'
elif race_str == 'italian':
race_str = 'italy'
elif race_str == 'russian':
race_str = 'russia'
elif race_str == 'turkish':
race_str = 'turkey'
elif race_str == 'hungarian':
race_str = 'hungary'
elif race_str == 'belgian':
race_str = 'belgium'
driver_perf = pd.read_csv(f'data/{race_str}_driver_performance.csv')
constr_perf = pd.read_csv(f'data/{race_str}_constructor_performance.csv')
drivers_tmp = []
for d in drivers:
season_cost_driver = driver_cost[driver_cost['Driver Name'] == d]
cost = season_cost_driver[r].iloc[0]
all_points = driver_perf[driver_perf['Driver Name'] == d]
points = all_points['Fantasy Points'].iloc[0]
drivers_tmp.append(Racer(d,points,cost))
constructors_tmp = []
for t in teams:
season_cost_team = const_cost[const_cost['Constructor Name'] == t]
cost = season_cost_team[r].iloc[0]
all_points = constr_perf[constr_perf['Constructor Name']==t]
points = all_points['Fantasy Points'].iloc[0]
constructors_tmp.append(Racer(t,points,cost))
race = Race(r, drivers_tmp,constructors_tmp)
races.append(race)
return races
def save_races_as_json(races,name):
main_obj = []
for race in races:
race_dict = {}
race_dict['name'] = race.name
drivers_list = []
race_dict['drivers'] = drivers_list
for d in race.drivers:
racer_dict = {}
racer_dict['name'] = d.name
racer_dict['cost'] = d.cost
racer_dict['points'] = int(d.points)
drivers_list.append(racer_dict)
team_list = []
race_dict['teams'] = team_list
for t in race.teams:
team_dict = {}
team_dict['name'] = t.name
team_dict['points'] = int(t.points)
team_dict['cost'] = t.cost
team_list.append(team_dict)
main_obj.append(race_dict)
with open(name,'w') as f:
f.write(json.dumps(main_obj,indent=3))