2023-06-23 10:30:07 +02:00
|
|
|
"""Misc helper functions"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from typing import Dict, Optional
|
|
|
|
|
|
|
|
|
|
|
|
def convert_units(
|
|
|
|
value: float, unit: str, unit_conversions: Dict[str, float]
|
|
|
|
) -> Optional[float]:
|
|
|
|
"""Convert a given value and unit into a different unit.
|
2023-06-23 11:11:32 +02:00
|
|
|
Round the returned value to 2 significant decimals
|
2023-06-23 10:30:07 +02:00
|
|
|
If unrecognized unit, return None"""
|
|
|
|
try:
|
|
|
|
conversion = unit_conversions[unit]
|
|
|
|
except KeyError:
|
|
|
|
logging.error("unrecognized unit: %s", unit)
|
|
|
|
return None
|
2023-06-23 11:11:32 +02:00
|
|
|
return round(value * conversion, 2)
|