-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.py
More file actions
59 lines (44 loc) · 1.63 KB
/
Copy pathsimulator.py
File metadata and controls
59 lines (44 loc) · 1.63 KB
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
import os
import pandas as pd
import datetime as dt
class Simulator:
def __init__(self, path_stream_data, path_database):
self.path = path_database
self.path_stream = path_stream_data
self.stream = None
self.time_start = None
self.time = None
def start(self):
self.stream = pd.read_csv(self.path_stream, parse_dates=['dt'])
self.stream = self.stream.sort_values('dt')
self.time_start = self.stream['dt'].min()
self.time = self.stream['dt'].min()
self.stream.iloc[:0].to_csv(self.path, index=None)
self.step(seconds=0)
def step(self, seconds=5):
self.time = self.time + dt.timedelta(seconds=seconds)
df_stream = self.stream
out_data = df_stream.loc[df_stream['dt'] <= self.time]
if len(out_data):
with open(self.path, 'a') as f:
out_data.to_csv(f, header=False, index=None)
df_stream = df_stream.loc[df_stream['dt'] > self.time]
self.stream = df_stream
def read_db(self):
df = pd.read_csv(self.path)
return df
if __name__ == '__main__':
# Testing Simulator
path_project = os.path.abspath(
os.path.join(os.path.realpath(__file__), '..')
)
path_stream_data = os.path.join(path_project, 'database', 'raw_data.csv')
path_database = os.path.join(path_project, 'database', 'database.csv')
sim = Simulator(path_stream_data, path_database)
sim.start()
for i in range(50):
sim.step(seconds=1)
print('RAW DATA:')
print(sim.stream)
print('DATABASE:')
print(pd.read_csv(path_database))