-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.py
36 lines (25 loc) · 847 Bytes
/
logger.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
import csv
from rich.console import Console
console = Console()
class Logger:
def __init__(self, logfile):
self.tracks = {}
self.file = open(logfile, "w")
self.writer = csv.writer(self.file)
def close(self):
self.file.close()
def log(self, name, value):
if name not in self.tracks:
self.tracks[name] = []
self.tracks[name].append(value)
def print(self, title=None):
if title is not None:
console.rule(title)
for name, track in self.tracks.items():
console.print(f"{name}: {track[-1]}")
def write(self):
if self.file.tell() == 0:
self.writer.writerow(list(self.tracks.keys()))
row = [track[-1] for track in self.tracks.values()]
self.writer.writerow(row)
self.file.flush()