Skip to content

Commit

Permalink
Update conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
atuonufure committed Jan 3, 2024
1 parent aae2beb commit a7e13a9
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions fhirpathpy/engine/nodes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timedelta, timezone
from dateutil.relativedelta import relativedelta
from dateutil import parser
from decimal import ROUND_UP, Decimal
from decimal import ROUND_HALF_UP, ROUND_UP, Decimal
import math
import json
import re
Expand Down Expand Up @@ -148,7 +148,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}
_lbs_kg_conversion_factor = {"'kg'": 1.0, "lbs": 0.453592, "'[lb_av]'": 0.453592}
_g_mg_conversion_factor = {"'g'": 1.0, "'mg'": 0.001}

datetime_multipliers = {
Expand Down Expand Up @@ -209,6 +209,11 @@ def deep_equal(self, other):
if self.unit in self._years_and_months and other.unit in self._years_and_months:
return self._compare_years_and_months(other, year_units=["'a'", "year", "years"])
else:
if self.unit != other.unit:
converted = FP_Quantity.conv_unit_to(self.unit, self.value, other.unit)
reverse_converted = FP_Quantity.conv_unit_to(converted.unit, converted.value, self.unit)
if converted is not None:
return self.value == reverse_converted.value and self.unit == reverse_converted.unit
return self.__eq__(other)
else:
return super().__eq__(other)
Expand Down Expand Up @@ -251,10 +256,9 @@ def conv_unit_to(fromUnit, 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)

value = Decimal(value) * Decimal(FP_Quantity._g_mg_conversion_factor[fromUnit])
result = (value / Decimal(FP_Quantity._g_mg_conversion_factor[toUnit])).quantize(Decimal('1.'), rounding=ROUND_HALF_UP)
return FP_Quantity(result, toUnit)
return None

def _compare_years_and_months(self, other, year_units=["year", "years"]):
Expand Down Expand Up @@ -838,7 +842,10 @@ def create_by_value_in_namespace(namespace, value):
name = "boolean"

if namespace == TypeInfo.System:
name = name.capitalize()
if name == "dateTime":
name = "DateTime"
else:
name = name.capitalize()

return TypeInfo(name, namespace)

Expand Down

0 comments on commit a7e13a9

Please sign in to comment.