PySDM_examples.utils.dummy_controller

 1from contextlib import AbstractContextManager
 2
 3from PySDM.products import CPUTime, WallTime
 4
 5
 6class DummyController(AbstractContextManager):
 7    def __init__(self):
 8        self.panic = False
 9        self.t_last = self.__times()
10
11    @staticmethod
12    def __times():
13        return WallTime.clock(), CPUTime.clock()
14
15    def set_percent(self, value):
16        t_curr = self.__times()
17        wall_time = t_curr[0] - self.t_last[0]
18        cpu_time = t_curr[1] - self.t_last[1]
19        print(
20            f"{100 * value:.1f}%"
21            f" (times since last print: cpu={cpu_time:.1f}s wall={wall_time:.1f}s)"
22        )
23        self.t_last = self.__times()
24
25    def __enter__(self, *_):
26        pass
27
28    def __exit__(self, *_):
29        pass
class DummyController(contextlib.AbstractContextManager):
 7class DummyController(AbstractContextManager):
 8    def __init__(self):
 9        self.panic = False
10        self.t_last = self.__times()
11
12    @staticmethod
13    def __times():
14        return WallTime.clock(), CPUTime.clock()
15
16    def set_percent(self, value):
17        t_curr = self.__times()
18        wall_time = t_curr[0] - self.t_last[0]
19        cpu_time = t_curr[1] - self.t_last[1]
20        print(
21            f"{100 * value:.1f}%"
22            f" (times since last print: cpu={cpu_time:.1f}s wall={wall_time:.1f}s)"
23        )
24        self.t_last = self.__times()
25
26    def __enter__(self, *_):
27        pass
28
29    def __exit__(self, *_):
30        pass

An abstract base class for context managers.

panic
t_last
def set_percent(self, value):
16    def set_percent(self, value):
17        t_curr = self.__times()
18        wall_time = t_curr[0] - self.t_last[0]
19        cpu_time = t_curr[1] - self.t_last[1]
20        print(
21            f"{100 * value:.1f}%"
22            f" (times since last print: cpu={cpu_time:.1f}s wall={wall_time:.1f}s)"
23        )
24        self.t_last = self.__times()