From fda4941ba06d8100cc88d2435d1de4798e98c9ce Mon Sep 17 00:00:00 2001 From: atuonufure Date: Fri, 22 Dec 2023 13:57:38 +0100 Subject: [PATCH] Add g mg conversion --- fhirpathpy/engine/invocations/misc.py | 2 +- fhirpathpy/engine/nodes.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/fhirpathpy/engine/invocations/misc.py b/fhirpathpy/engine/invocations/misc.py index 696b3bd..4a1cdf4 100644 --- a/fhirpathpy/engine/invocations/misc.py +++ b/fhirpathpy/engine/invocations/misc.py @@ -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)) \ No newline at end of file + raise Exception("Not supported type {}".format(type)) diff --git a/fhirpathpy/engine/nodes.py b/fhirpathpy/engine/nodes.py index b82b865..20ddf3d 100644 --- a/fhirpathpy/engine/nodes.py +++ b/fhirpathpy/engine/nodes.py @@ -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"]}, @@ -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) @@ -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"]):