Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_unit_from_dimensions(dimensions, text, lang="en_US"):
"""
Reconcile a unit based on its dimensionality.
"""
key = load.get_key_from_dimensions(dimensions)
try:
unit = load.units(lang).derived[key]
except KeyError:
_LOGGER.debug(u"\tCould not find unit for: %s", key)
unit = cls.Unit(
name=build_unit_name(dimensions, lang),
dimensions=dimensions,
entity=get_entity_from_dimensions(dimensions, text, lang),
)
# Carry on original composition
unit.original_dimensions = dimensions
return unit
def get_entity_from_dimensions(dimensions, text, lang="en_US"):
"""
Infer the underlying entity of a unit (e.g. "volume" for "m^3") based on
its dimensionality.
"""
new_derived = [
{"base": load.units(lang).names[i["base"]].entity.name, "power": i["power"]}
for i in dimensions
]
final_derived = sorted(new_derived, key=lambda x: x["base"])
key = load.get_key_from_dimensions(final_derived)
ent = dis.disambiguate_entity(key, text, lang)
if ent is None:
_LOGGER.debug("\tCould not find entity for: %s", key)
ent = cls.Entity(name="unknown", dimensions=new_derived)
return ent