-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulations.py
75 lines (63 loc) · 2.26 KB
/
simulations.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
from create_tables import create_connection
from datetime import datetime
from dataclasses import dataclass
from random import uniform
from time import sleep
get_sql = """select * from simulations where id=?"""
@dataclass
class Simulation:
id: int
directory: str
distance: float
start_time:str
end_time: str
completion_status: int
fk_blocks_id:int
update_sql = """update simulations set directory=?, distance=?, start_time=?, end_time=?, completion_status=?, fk_blocks_id=? where id=?"""
@staticmethod
def get_simulation(id):
retries=0
while retries<5:
try:
with create_connection() as conn:
c=conn.cursor()
c.execute("begin")
print(get_sql)
c.execute(get_sql, (id,))
first_row = c.fetchone()
print(first_row)
if first_row is not None:
return Simulation(*first_row)
except Exception as e :
print(e)
sleep_time = uniform(0.1,4)
print(f"Sleeping for {sleep_time}")
sleep(sleep_time)
retries+=1
return false
def get_update_tuple(self):
return (self.directory, self.distance, self.start_time, self.end_time, self.completion_status, self.fk_blocks_id, self.id)
def update(self):
retries=0
while retries<5:
try:
with create_connection() as conn:
print("updating simulation")
cur = conn.cursor()
cur.execute("begin")
cur.execute(self.update_sql, self.get_update_tuple())
return
except Exception as e:
print(f"__set_time has failed with {sql_command}")
print(e)
sleep_time = uniform(0.1,4)
print(f"Sleeping for {sleep_time}")
sleep(sleep_time)
retries+=1
raise Exception(f"__set_time failed after {retries}")
if __name__=="__main__":
sim = Simulation.get_simulation(0)
sim.start_time= datetime.now()
sim.end_time= datetime.now()
print(sim)
sim.update()