-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutils.py
83 lines (72 loc) · 2.09 KB
/
utils.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
import json
import pandas as pd
import os
def read_csv(fp):
return pd.read_csv(fp)
def to_csv(contest):
# all_votes = [votes for c in contests for votes in c.votes_to_list()]
all_votes = contest.votes_to_list()
df = pd.DataFrame(
all_votes,
columns=[
"year",
"round",
"from_country_id",
"to_country_id",
"from_country",
"to_country",
"total_points",
"tele_points",
"jury_points",
],
)
if not os.path.exists("votes.csv"):
df.to_csv("votes.csv", index=False)
else:
df.to_csv("votes.csv", mode="a", header=False, index=False)
all_contestants = contest.contestants_to_list()
df = pd.DataFrame(
all_contestants,
columns=[
"year",
"to_country_id",
"to_country",
"performer",
"song",
"place_contest",
"sf_num",
"running_final",
"running_sf",
"place_final",
"points_final",
"place_sf",
"points_sf",
"points_tele_final",
"points_jury_final",
"points_tele_sf",
"points_jury_sf",
"composers",
"lyricists",
"lyrics",
"youtube_url",
],
)
if not os.path.exists("contestants.csv"):
df.to_csv("contestants.csv", index=False)
else:
df.to_csv("contestants.csv", mode="a", header=False, index=False)
def cast_int(i):
if i is not None and i.isdigit():
return int(i)
return None
def prepend_key_in_dict(d: dict, prepend_key: str):
return {f"{prepend_key}_{k}": v for k, v in d.items()}
def to_dict(obj, prepend_key: str = ""):
o = json.loads(json.dumps(obj, default=lambda o: o.__dict__))
if prepend_key != "":
if type(o) == list:
for idx in range(len(o)):
o[idx] = prepend_key_in_dict(o[idx], prepend_key)
elif type(o) == dict:
o = prepend_key_in_dict(o, prepend_key)
return o