dayrize-usecase/pipeline/beam_etl/helpers/misc.py

18 lines
464 B
Python
Raw Normal View History

"""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.
If unrecognized unit, return None"""
try:
conversion = unit_conversions[unit]
except KeyError:
logging.error("unrecognized unit: %s", unit)
return None
return value * conversion