19 lines
529 B
Python
19 lines
529 B
Python
|
"""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.
|
||
|
Round the returned value to 2 significant decimals
|
||
|
If unrecognized unit, return None"""
|
||
|
try:
|
||
|
conversion = unit_conversions[unit]
|
||
|
except KeyError:
|
||
|
logging.error("unrecognized unit: %s", unit)
|
||
|
return None
|
||
|
return round(value * conversion, 2)
|