From d8e9c80854b9355c2eb39fdac198bab49386e37c Mon Sep 17 00:00:00 2001
From: Benedikt Seeger <benedikt.seeger@ptb.de>
Date: Tue, 25 Mar 2025 10:09:34 +0100
Subject: [PATCH] added __hash__ function

---
 .vscode/launch.json      | 17 -----------------
 dsiUnits-js/package.json |  2 +-
 pyproject.toml           |  2 +-
 setup.cfg                | 14 --------------
 src/dsiUnits.py          |  4 ++++
 tests/test_dsiUnits.py   |  8 ++++++++
 6 files changed, 14 insertions(+), 33 deletions(-)
 delete mode 100644 .vscode/launch.json
 delete mode 100644 setup.cfg

diff --git a/.vscode/launch.json b/.vscode/launch.json
deleted file mode 100644
index ae99854..0000000
--- a/.vscode/launch.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
-    // Use IntelliSense to learn about possible attributes.
-    // Hover to view descriptions of existing attributes.
-    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
-    "version": "0.2.0",
-    "configurations": [
-        {
-            "name": "Pytest: Debugger",
-            "type": "debugpy",
-            "request": "launch",
-            "program": "${workspaceFolder}/venv/bin/pytest",
-            "console": "integratedTerminal",
-            "justMyCode": false,
-            "args": ["--no-cov"],
-        }
-    ]
-}
\ No newline at end of file
diff --git a/dsiUnits-js/package.json b/dsiUnits-js/package.json
index 7dfad02..30a1123 100644
--- a/dsiUnits-js/package.json
+++ b/dsiUnits-js/package.json
@@ -1,6 +1,6 @@
 {
   "name": "dsiunits-js",
-  "version": "0.9.0",
+  "version": "0.9.1",
   "type": "module",
   "main": "dist/index.cjs.js",
   "module": "dist/index.esm.js",
diff --git a/pyproject.toml b/pyproject.toml
index 26bff3b..4fd46ce 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
 
 [project]
 name = "dsiunits"  # Ensure this is correctly specified
-version = "2.4.0"
+version = "2.4.1"
 description = "This is a Python module for handling the SI units as objects in Python, parsing them from strings and converting them to Latex and Unicode, as well as performing math operations and calculating scale factors."
 authors = [
     { name="Benedikt Seeger", email="benedikt.seeger@ptb.de" },
diff --git a/setup.cfg b/setup.cfg
deleted file mode 100644
index ed5564f..0000000
--- a/setup.cfg
+++ /dev/null
@@ -1,14 +0,0 @@
-[metadata]
-name = dsiUnits
-version = 2.4.0
-description = This is a Python module for handling the SI units as objects in Python, parsing them from strings and converting them to Latex and Unicode, as well as performing math operations and calculating scale factors.
-long_description = file: README.md
-
-[options]
-python_requires = >=3.7
-
-[options.extras_require]
-testing =
-    pytest>=7.4.1
-    pytest-cov>=4.1.0
-    # Any other testing dependencies
diff --git a/src/dsiUnits.py b/src/dsiUnits.py
index 7da858b..8af756b 100644
--- a/src/dsiUnits.py
+++ b/src/dsiUnits.py
@@ -588,6 +588,10 @@ class dsiUnit:
                 result += str(node)
         return result
 
+    def __hash__(self):
+        # Use the hash of an immutable attribute (here, self.value)
+        return hash(str(self))
+
     def __repr__(self):
         contentStr = self.toUTF8()
         if not self.valid:
diff --git a/tests/test_dsiUnits.py b/tests/test_dsiUnits.py
index 28f0e65..e1c1a5c 100644
--- a/tests/test_dsiUnits.py
+++ b/tests/test_dsiUnits.py
@@ -618,3 +618,11 @@ def test_exponentMath():
     assert m2.getBaseUnit(km2) == m2
     assert m2.getScaleFactor(km2) == 1.0e6
 
+
+def test_hash():
+    Hmm2=hash(dsiUnit(r'\milli\metre\tothe{2}'))
+    Hkm2=hash(dsiUnit(r'\kilo\metre\tothe{2}'))
+    Hm2=hash(dsiUnit(r'\metre\tothe{2}'))
+    Hm2_2=hash(dsiUnit(r'\metre\tothe{2}'))
+    assert hash(Hmm2) != hash(Hkm2) != hash(Hm2)
+    assert hash(Hm2) == hash(Hm2_2)
-- 
GitLab