forked from DanielMartinezMiravete/Axion_Limits_Memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBaseClass.py
205 lines (175 loc) · 7.13 KB
/
DataBaseClass.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import sqlite3 as sql
"""
# Example of usage:
import DataBaseClass as db
database = db.DataBaseGag("Axions.db")
AxionsGag = [
['qcdband', 'band', PATH_DATA + 'QCD_band.dat', "facecolor='yellow'", 1, 1, 1, 1, 1, 1, 0],
['old_haloscopes', 'band', PATH_DATA + 'MicrowaveCavities.txt', "facecolor='limegreen', edgecolor='darkgreen', linewidth=0.2", 1, 1, 1, 1, 1, 0, 0],
['ABRA3', 'line', PATH_DATA + 'ABRAres_3.dat', "color='green', linewidth=0.1, linestyle='-'", 1, 1, 1, 1, 1, 1, 0],
['ADMX2018', 'band', PATH_DATA + 'ADMX2018.txt', "facecolor='limegreen', edgecolor='darkgreen', linewidth=0.2", 0, 0, 0, 0, 0, 0, 0],
['BabyIAXO', 'band', PATH_DATA + 'miniIAXO.dat', "facecolor='deepskyblue', linewidth=0.5, alpha=0.1, linestyle='-'", 1, 1, 1, 0, 1, 0, 1],
['IAXO', 'band', PATH_DATA + 'IAXO_nominal.txt', "facecolor='deepskyblue', linewidth=0.5, alpha=0.1, linestyle='-'", 1, 1, 1, 0, 1, 0, 1],
['CAST', 'band', PATH_DATA + 'cast_env_2016.dat', "facecolor='deepskyblue', edgecolor='blue', linewidth=0.5", 1, 1, 1, 0, 1, 0, 1],
]
database.insert_rows(AxionsGag)
data = database.read_rows()
print(data)
"""
"""
# Example of usage:
import DataBaseClass as db
database = db.DataBaseGae("Axions.db")
AxionsGae= [
["DFSZ1_starhint", "region", path1 + "DFSZ1_ABC_dominant_No_SN_2sigma_hint_rootgaegag_vs_ma.dat", "facecolor='springgreen', edgecolor='darkgreen', alpha=0.2", 1, 0],
["AJ83_starhint", "region", path1 + "AJ83_ABC_dominant_No_SN_2sigma_hint_rootgaegag_vs_ma.dat", "facecolor='red', edgecolor='red', alpha=0.2", 1, 0],
["QCDband", "band", path2 + "DFSZband_gaegag.dat", "facecolor='lemonchiffon', edgecolor='none', linewidth=1", 1, 0],
["CAST_gae", "band", path2 + "CAST_gae_gagg.dat", "facecolor='steelblue', edgecolor='darkblue', linewidth=0.5", 1, 0],
["IAXO_gae", "band", path2 + "sqrtgaagae_sc2.dat", "facecolor='skyblue', edgecolor='black', linewidth=0.5, alpha=0.3", 0, 1],
["IAXOplus_gae", "band", path2 + "sqrtgaagae_sc3.dat", "facecolor='skyblue', edgecolor='black', linewidth=0.5, alpha=0.3", 0, 1],
]
database.insert_rows(AxionsGae)
data = database.read_rows()
print(data)
"""
"""
# Example of usage:
import DataBaseClass as db
database = db.DataBaseLabels("Axions.db")
labels = [
["CAST", 1.0e-6, 1.0e-6, "color='blue', fontsize=10", 1, 0],
["IAXO", 1.0e-6, 1.0e-6, "color='blue', fontsize=10", 1, 1],
]
database.insert_rows(labels)
data = database.read_rows()
print(data)
"""
# DataBase class is not intended to be used directly, but to be inherited by DataBaseGag and DataBaseGae
class DataBase:
def __init__(self, file_database="Axions.db", name: str = "Axions"):
self.FILE_DATABASE = file_database
try:
self.conn = sql.connect(self.FILE_DATABASE)
except Error as e:
print(e)
exit()
self.cursor = self.conn.cursor()
self.name = name
def get_rows(self, selection: str = ""):
instruction = f"SELECT * FROM {self.name}"
if selection != "":
instruction += f" WHERE {selection}"
self.cursor.execute(instruction)
rows = self.cursor.fetchall()
return rows
def update_row(self, name, field, value):
instruction = (
f"UPDATE {self.name} SET {field} = '{value}' WHERE name = '{name}'"
)
self.cursor.execute(instruction)
self.conn.commit()
def delete_rows(self, selection: str = "", confirm: bool = False):
instruction = f"DELETE FROM {self.name}"
if selection != "":
instruction += f" WHERE {selection}"
if not confirm:
print(
"WARNING: You are trying to delete all rows "
+ ("WHERE '" + selection + "' " if selection != "" else "")
+ "from the table"
)
ans = input("Are you sure? (y/n)\n")
if ans not in ["y", "yes", "Y", "YES"]:
print("Aborting")
return
self.cursor.execute(instruction)
self.conn.commit()
def __del__(self):
self.conn.commit()
self.conn.close()
class DataBaseGag(DataBase):
def __init__(self, file_database="Axions.db", name: str = "AxionsGag"):
DataBase.__init__(self, file_database, name)
self.cursor.execute(
f"""CREATE TABLE IF NOT EXISTS {self.name} (
name TEXT,
type TEXT,
path TEXT,
drawOptions TEXT,
wildType INTEGER,
large_panorama INTEGER,
panorama INTEGER,
helioscopes INTEGER,
haloscopes INTEGER,
lswexps INTEGER,
projection INTEGER
)"""
)
self.conn.commit()
def insert_row(
self,
name,
type,
path,
drawOptions="",
noPT=1,
LP=0,
P=0,
Helios=0,
Halos=0,
LSW=0,
projection=0,
):
instruction = f"INSERT INTO {self.name} VALUES ('{name}', '{type}','{path}','{drawOptions}','{noPT}','{LP}','{P}','{Helios}','{Halos}',{LSW},{projection})"
self.cursor.execute(instruction)
self.conn.commit()
def insert_rows(self, rows):
instruction = f"INSERT INTO {self.name} VALUES (?,?,?,?,?,?,?,?,?,?,?)"
self.cursor.executemany(instruction, rows)
self.conn.commit()
class DataBaseGae(DataBase):
def __init__(self, file_database="Axions.db", name: str = "AxionsGae"):
DataBase.__init__(self, file_database, name)
self.cursor.execute(
f"""CREATE TABLE IF NOT EXISTS {self.name} (
name TEXT,
type TEXT,
path TEXT,
drawOptions TEXT,
wildType INTEGER,
projection INTEGER
)"""
)
self.conn.commit()
def insert_row(self, name, type, path, drawOptions="", noPT=1, projection=0):
instruction = f"INSERT INTO {self.name} VALUES ('{name}', '{type}','{path}','{drawOptions}',{noPT},{projection})"
self.cursor.execute(instruction)
self.conn.commit()
def insert_rows(self, rows):
instruction = f"INSERT INTO {self.name} VALUES (?, ?, ?, ?, ?, ?)"
self.cursor.executemany(instruction, rows)
self.conn.commit()
class DataBaseLabels(DataBase):
def __init__(self, file_database="Axions.db", name: str = "Labels"):
DataBase.__init__(self, file_database, name)
self.cursor.execute(
f"""CREATE TABLE IF NOT EXISTS {self.name} (
label TEXT,
x_position REAL,
y_position REAL,
drawOptions TEXT,
onoff INTEGER,
projection INTEGER
)"""
)
self.conn.commit()
def insert_row(
self, label, x_position, y_position, drawOptions="", on=1, projection=0
):
instruction = f"INSERT INTO {self.name} VALUES ('{label}', '{x_position}', '{y_position}', '{drawOptions}', {on}, {projection})"
self.cursor.execute(instruction)
self.conn.commit()
def insert_rows(self, rows):
instruction = f"INSERT INTO {self.name} VALUES (?, ?, ?, ?, ?, ?)"
self.cursor.executemany(instruction, rows)
self.conn.commit()