From 697fa094536e68e366fa516042ba30ad0c6e8448 Mon Sep 17 00:00:00 2001
From: Mark Driver <mdd31@alumni.cam.ac.uk>
Date: Sun, 2 Feb 2020 20:23:44 +0000
Subject: [PATCH] overload temperature hash method.

---
 phasecalculator/classes/temperature.py | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/phasecalculator/classes/temperature.py b/phasecalculator/classes/temperature.py
index bd03633..78f1c89 100755
--- a/phasecalculator/classes/temperature.py
+++ b/phasecalculator/classes/temperature.py
@@ -70,6 +70,23 @@ class Temperature(object):
             return (abs(self.temperature - other.temperature)< self.TOLERANCE and self.temperature_unit == other.temperature_unit)
         else:
             return False
+    
+    def __hash__(self):
+        """Overload hash operator of Temperature class.
+        
+        Convert Temperature float to kelvin, and write to formatted string to
+        2dp. This matches tolerance of numerical values.
+
+        Returns
+        -------
+        int
+            hash of temperature object.
+
+        """
+        temperature = self.temperature if self.temperature_unit == "KELVIN" else self.temperature + 273.15
+        temp_str = "{:.2f}".format(abs(temperature))
+        return hash(temp_str) + hash(self.temperature_unit)
+    
     @classmethod
     def parse_xml(cls, temperature_xml):
         """Parse XML representation to new instance.
-- 
GitLab