Coverage for slic/utils/tqdm_mod.py: 36%
28 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-07-07 16:57 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-07-07 16:57 +0000
1from time import sleep
2import tqdm
5def tqdm_sleep(seconds, ndiv=100):
6 delta = seconds / float(ndiv)
7 for _ in tqdm.trange(ndiv):
8 sleep(delta)
12class tqdm_mod(tqdm.tqdm):
14 def __init__(self, *args, **kwargs):
15 kwargs.setdefault("unit", "@") # use "@/s" to signal Hz
16 kwargs.setdefault("unit_scale", True) # this enables use of format_sizeof
17 super().__init__(*args, **kwargs)
19 def format_meter(self, *args, **kwargs):
20 res = super().format_meter(*args, **kwargs)
21 # these have to have the same length otherwise the combined line gets messed up
22 res = res.replace("@/s", " Hz")
23 return res
25 def set(self, elapsed):
26 """
27 update with elapsed n, i.e., the delta between start and current n
28 """
29 elapsed = clamp(elapsed, 0, self.total)
30 increment = elapsed - self.n
31 self.update(increment)
34def clamp(val, vmin, vmax):
35 val = max(val, vmin)
36 val = min(val, vmax)
37 return val
40def format_sizeof(num, *args, **kwargs):
41 # format floats such that they accommodate up to 100.x without jumping around
42 if isinstance(num, float):
43 return f"{num:5.1f}" # len("100") + len(".1") == 3+2 == 5
44 # for everything else, use the default string representation
45 return str(num)
49# format_meter is a staticmethod, thus has no self and uses the tqdm class instead
50# hence need to overwrite at the source to convince it to use the custom format_sizeof
51tqdm.tqdm.format_sizeof = format_sizeof