srne-mqtt/srnemqtt/util.py

91 lines
2.1 KiB
Python
Raw Normal View History

2023-01-07 21:18:30 +00:00
# -*- coding: utf-8 -*-
2023-12-18 13:26:56 +00:00
import json
2023-01-07 21:18:30 +00:00
import time
2023-12-18 13:26:56 +00:00
from logging import Filter as LoggingFilter
2023-12-16 22:36:53 +00:00
from logging import getLogger
2023-12-18 13:26:56 +00:00
from typing import Dict, Optional
__all__ = ["humanize_number", "Periodical", "LazyJSON", "LoggingDictFilter"]
2023-01-07 21:18:30 +00:00
# Only factor of 1000
SI_PREFIXES_LARGE = "kMGTPEZY"
SI_PREFIXES_SMALL = "mµnpfazy"
2023-12-16 22:36:53 +00:00
logger = getLogger(__name__)
2023-01-07 21:18:30 +00:00
def humanize_number(data, unit: str = ""):
counter = 0
while data >= 1000:
data /= 1000
counter += 1
if counter >= len(SI_PREFIXES_LARGE):
break
while data < 1:
data *= 1000
counter -= 1
if abs(counter) >= len(SI_PREFIXES_SMALL):
break
if not counter:
prefix = ""
elif counter > 0:
prefix = SI_PREFIXES_LARGE[counter - 1]
elif counter < 0:
prefix = SI_PREFIXES_SMALL[abs(counter) - 1]
return f"{data:.3g} {prefix}{unit}"
2023-12-18 13:26:56 +00:00
class LazyJSON:
def __init__(self, data):
self.data = data
def __str__(self) -> str:
return json.dumps(self.data)
def __repr__(self) -> str:
return repr(self.data)
class LoggingDictFilter(LoggingFilter):
data: Dict[str, str]
def __init__(self):
self.data = {}
def filter(self, record):
print(self.data)
for key, value in self.data.items():
print(key, value)
assert not hasattr(record, key)
setattr(record, key, value)
return True
2023-01-07 21:18:30 +00:00
class Periodical:
prev: float
interval: float
def __init__(self, interval: float, start: Optional[float] = None):
self.prev = time.time() - interval if start is None else start
self.interval = interval
def __call__(self, now: Optional[float] = None) -> bool:
if now is None:
now = time.time()
if (now - self.prev) >= self.interval:
skipped, overshoot = divmod(now - self.prev, self.interval)
skipped -= 1
if skipped:
2023-12-16 22:36:53 +00:00
logger.debug(
"Skipped:", skipped, overshoot, now - self.prev, self.interval
)
2023-01-07 21:18:30 +00:00
self.prev = now - overshoot
return True
return False