Skip to content

Commit

Permalink
Add g mg conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
atuonufure committed Dec 22, 2023
1 parent c99db9c commit fda4941
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fhirpathpy/engine/invocations/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,4 @@ def singleton(coll, type):
if (val is not None):
return val
raise Exception("Expected {type}, but got: {coll}".format(type=type.lower(), coll=coll))
raise Exception("Not supported type {}".format(type))
raise Exception("Not supported type {}".format(type))
13 changes: 13 additions & 0 deletions fhirpathpy/engine/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class FP_Quantity(FP_Type):
_year_month_conversion_factor = {"'a'": 12, "'mo'": 1}
_m_cm_mm_conversion_factor = {"'m'": 1.0, "'cm'": 0.01, "'mm'": 0.001}
_lbs_kg_conversion_factor = {"'kg'": 1.0, "lbs": 0.453592}
_g_mg_conversion_factor = {"'g'": 1.0, "'mg'": 0.001}

datetime_multipliers = {
**{key: Decimal("604800") for key in ["'wk'", "week", "weeks"]},
Expand Down Expand Up @@ -194,6 +195,11 @@ def __eq__(self, other):
other_value_in_seconds = other.value * self.datetime_multipliers[other.unit]
return self_value_in_seconds == other_value_in_seconds
else:
if self.unit != other.unit:
converted = FP_Quantity.conv_unit_to(self.unit, self.value, other.unit)
if converted is not None:
return other.value == converted.value and other.unit == converted.unit

return self.value == other.value and self.unit == other.unit
else:
return super().__eq__(other)
Expand Down Expand Up @@ -242,6 +248,13 @@ def conv_unit_to(fromUnit, value, toUnit):
rounded_value = converted_value.quantize(Decimal("1."), rounding=ROUND_UP)
return FP_Quantity(rounded_value, toUnit)

from_g_mg_magnitude = FP_Quantity._g_mg_conversion_factor.get(fromUnit)
to_g_mg_magnitude = FP_Quantity._g_mg_conversion_factor.get(toUnit)
if from_g_mg_magnitude and to_g_mg_magnitude:
converted_value = (Decimal(from_g_mg_magnitude) * value) / Decimal(to_g_mg_magnitude)
rounded_value = converted_value.quantize(Decimal("1."), rounding=ROUND_UP)
return FP_Quantity(rounded_value, toUnit)

return None

def _compare_years_and_months(self, other, year_units=["year", "years"]):
Expand Down

0 comments on commit fda4941

Please sign in to comment.