-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_funcs.py
124 lines (103 loc) · 2.97 KB
/
db_funcs.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
import sqlite3
db = (
"../rebalancing_data_db.sqlite"
if __name__ != "__main__"
else "rebalancing_data_db.sqlite"
)
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file, isolation_level=None)
except Exception as e:
print(e)
return conn
def create_table(statement):
conn = create_connection(db)
try:
connection = conn.cursor()
connection.execute(statement)
print("Table created successfully")
except Exception as e:
print(e)
def add_column(column, table_name):
conn = create_connection(db).cursor()
statement = f"ALTER TABLE {table_name} ADD {column} DOUBLE PRECISION"
conn.execute(statement)
def drop_column(column, table_name):
conn = create_connection(db).cursor()
statement = f"ALTER TABLE {table_name} DROP COLUMN {column} "
conn.execute(statement)
def does_column_exist(column, table_name):
conn = create_connection(db).cursor()
statement = f"SELECT length({column}) FROM {table_name}"
try:
conn.execute(statement)
return True
except:
add_column(column, table_name)
return True
def insert_values(row, columns, values, table_name):
for column in columns:
does_column_exist(column, table_name)
conn = create_connection(db).cursor()
columns = ",".join(columns)
values = str(values).strip("[]").replace(" ", "")
statement = f"""
REPLACE INTO {table_name}(date,{columns})
VALUES("{row}",{values})"""
conn.execute(statement)
# Helper functions
def convert_to_sql_strings(string_list):
mapping = {
"0": "zero",
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five",
"6": "six",
"7": "seven",
"8": "eight",
"9": "nine",
}
converted_strings = []
for i in string_list:
if "-" in i:
i = i.replace("-", "_")
for number in list(mapping.keys()):
if number in i:
i = i.replace(number, mapping[number])
converted_strings.append(i)
return converted_strings
def convert_from_sql_strings(string_list):
mapping = {
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
}
converted_strings = []
for i in string_list:
if "_" in i:
i = i.replace("_", "-")
for number in list(mapping.keys()):
if number in i:
i = i.replace(number,mapping[number])
converted_strings.append(i)
return converted_strings
def create_liquidity_table(name):
sql_string = f"""CREATE TABLE IF NOT EXISTS {name} (
date text PRIMARY KEY
);"""
create_table(sql_string)
def create_benchmark_table(name):
sql_string = f"""CREATE TABLE IF NOT EXISTS {name} (
date text PRIMARY KEY
);"""
create_table(sql_string)