Skip to content

Commit

Permalink
Update __hash__ in FP_Quantity for different time units
Browse files Browse the repository at this point in the history
  • Loading branch information
atuonufure committed Oct 2, 2023
1 parent 77448ba commit 3a79381
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions fhirpathpy/engine/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ class FP_Quantity(FP_Type):

_year_month_conversion_factor = {"'a'": 12, "'mo'": 1}

datetime_multipliers = {
**{key: Decimal("604800") for key in ["'wk'", "week", "weeks"]},
**{key: Decimal("86400") for key in ["'d'", "day", "days"]},
**{key: Decimal("3600") for key in ["'h'", "hour", "hours"]},
**{key: Decimal("60") for key in ["'min'", "minute", "minutes"]},
**{key: Decimal("1") for key in ["'s'", "second", "seconds"]},
**{key: Decimal("0.001") for key in ["'ms'", "millisecond", "milliseconds"]},
}

def __init__(self, value, unit):
super().__init__()
self.asStr = f"{value} {unit}"
Expand All @@ -132,7 +141,16 @@ def __repr__(self):
return f"{type(self)}<{self.asStr}>"

def __hash__(self):
return hash(self.value) ^ hash(self.unit)
if self.unit in self._years_and_months:
value_in_months = self.value
if self.unit in ["'a'", "year", "years"]:
value_in_months *= 12
return hash(("months", value_in_months))
elif self.unit in self._weeks_days_and_time:
value_in_seconds = self.value * self.datetime_multipliers[self.unit]
return hash(("seconds", value_in_seconds))
else:
return hash((self.value, self.unit))

def __eq__(self, other):
if isinstance(other, FP_Quantity):
Expand All @@ -145,25 +163,8 @@ def __eq__(self, other):
other_value_in_months *= 12
return self_value_in_months == other_value_in_months
elif self.unit in self._weeks_days_and_time and other.unit in self._weeks_days_and_time:
weeks_multipliers = {key: Decimal("604800") for key in ["'wk'", "week", "weeks"]}
days_multipliers = {key: Decimal("86400") for key in ["'d'", "day", "days"]}
hours_multipliers = {key: Decimal("3600") for key in ["'h'", "hour", "hours"]}
minutes_multipliers = {key: Decimal("60") for key in ["'min'", "minute", "minutes"]}
seconds_multipliers = {key: Decimal("1") for key in ["'s'", "second", "seconds"]}
milliseconds_multipliers = {
key: Decimal("0.001") for key in ["'ms'", "millisecond", "milliseconds"]
}

datetime_multipliers = {
**weeks_multipliers,
**days_multipliers,
**hours_multipliers,
**minutes_multipliers,
**seconds_multipliers,
**milliseconds_multipliers,
}
self_value_in_seconds = self.value * datetime_multipliers[self.unit]
other_value_in_seconds = other.value * datetime_multipliers[other.unit]
self_value_in_seconds = self.value * self.datetime_multipliers[self.unit]
other_value_in_seconds = other.value * self.datetime_multipliers[other.unit]
return self_value_in_seconds == other_value_in_seconds
else:
return self.value == other.value and self.unit == other.unit
Expand Down

0 comments on commit 3a79381

Please sign in to comment.