-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
172 lines (124 loc) · 3.5 KB
/
helpers.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import numpy as np
import pandas as pd
import re
import sqlite3
from datetime import datetime, timezone
YEAR = datetime.today().year
DB = "tipster.db"
CSV_FILE = "tipster_data.csv"
CSV_URL = "https://raw.githubusercontent.com/nickneos/tipster/main/tipster_data.csv"
# Colours
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def db_import_csv(db, csv):
try:
# create db if doesnt exist
conn = sqlite3.connect(db)
c = conn.cursor()
# load into df
df = pd.read_csv(csv)
df = clean_df(df)
# write to sql
df.to_sql('tbl_fixture', conn, if_exists='replace', index = False)
conn.commit()
c.close()
return db
except Exception as e:
print(f"{FAIL}Couldn't import {csv} into {db}\n{e}{ENDC}")
return None
def db_qry(db, sql, param=None):
try:
conn = sqlite3.connect(db)
c = conn.cursor()
if param is None:
c.execute(sql)
else:
c.execute(sql, param)
result = c.fetchall()
conn.commit()
c.close()
if len(result) == 1:
if len(result[0]) == 1:
return result[0][0]
else:
return result[0]
elif len(result) > 1:
if len(result[0]) == 1:
tmp = []
for x in result:
tmp.append(x[0])
return tmp
else:
return result
else:
return result
except Exception as e:
print(f"{FAIL}{e}{ENDC}")
return None
def db_qry_many(db, sql, param):
try:
conn = sqlite3.connect(db)
c = conn.cursor()
c.executemany(sql, param)
result = c.fetchall()
conn.commit()
c.close()
return result
except Exception as e:
print(f"{FAIL}{e}{ENDC}")
return None
def db_print(data, columns):
if type(columns) is not list:
columns = re.split(", ", columns)
if type(data) is tuple:
ldata = []
ldata.append(data)
data = ldata
df = pd.DataFrame(data, columns=columns)
df = clean_df(df)
# print df
print(f"\n{df.to_string(index=False)}\n")
def sql_to_csv(sql_qry, csv=CSV_FILE, db=DB):
""" Create csv from sqlite3 query """
try:
conn = sqlite3.connect(db)
df = pd.read_sql_query(sql_qry, conn)
df = clean_df(df)
df.to_csv(csv, index=False)
conn.close
except Exception as e:
print(f"{FAIL}Couldn't update {csv}\n{e}{ENDC}")
def cleaner(list):
"""" Cleans the lists extracted from beautiful soup """
content = []
for li in list:
content.append(li.getText().replace("\n", " ").replace("\t""", " "))
# print(content)
return content
def fix_team_name(teamname):
""" Standardise team names """
if teamname == "Greater Western Sydney":
teamname = "GWS"
return teamname
def utc_to_local(utc_dt):
""" Converts utc datetime to local datetime"""
if type(utc_dt) == str:
try:
utc_dt = datetime.fromisoformat(utc_dt)
except:
utc_dt = None
return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
def clean_df(df):
""" Clean data in tipster dataframe """
try:
df['TipOutcome'] = df['TipOutcome'].astype('Int64')
except:
pass
return df