added silent flag and repr

This commit is contained in:
2023-02-23 20:03:10 +01:00
parent d9c28ec42c
commit 76b037cc3d
+19 -5
View File
@@ -1,6 +1,8 @@
from threading import Thread
from itertools import count
from slic.utils import tqdm_sleep
from threading import Thread
from time import sleep
from slic.utils import tqdm_sleep, typename
def monitor(name, sensor, record_time, grum_client, cfg=None):
@@ -18,12 +20,13 @@ def monitor(name, sensor, record_time, grum_client, cfg=None):
class Monitor:
def __init__(self, name, sensor, record_time, grum_client, cfg=None):
def __init__(self, name, sensor, record_time, grum_client, cfg=None, silent=False):
self.name = name
self.sensor = sensor
self.record_time = record_time
self.grum_client = grum_client
self.cfg = cfg or {}
self.silent = silent
self.stop()
def stop(self):
@@ -41,12 +44,17 @@ class Monitor:
sensor = self.sensor
record_time = self.record_time
grum_client = self.grum_client
silent = self.silent
dont_print = lambda *a: None
fprint = dont_print if silent else print
fsleep = sleep if silent else tqdm_sleep
grum_client.new_plot(name, self.cfg)
for x in count():
print(f"iteration #{x}")
fprint(f"iteration #{x}")
with sensor:
tqdm_sleep(record_time)
fsleep(record_time)
y = sensor.get_aggregate()
y = float(y)
grum_client.append_data(name, (x, y))
@@ -55,4 +63,10 @@ class Monitor:
break
def __repr__(self):
tn = typename(self)
state = "running" if self.running else "not running"
return f"{tn} \"{self.name}\": {state}"