Skip to content

Commit d122570

Browse files
committed
make coordinates hashable
1 parent ad95740 commit d122570

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

funlib/geometry/coordinate.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import numpy as np
55

6+
67
class Coordinate(tuple):
78
"""A ``tuple`` of numbers or None.
89
@@ -37,6 +38,9 @@ def __new__(cls, *array_like):
3738
def __eq__(self, other) -> bool:
3839
return np.array_equal(self, other)
3940

41+
def __hash__(self) -> int:
42+
return hash(tuple(x for x in self))
43+
4044
@property
4145
def dims(self) -> int:
4246
return len(self)

tests/test_coordinate.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def test_none():
9696
assert abs(a) == (None, 1, 2)
9797
assert abs(-a) == (None, 1, 2)
9898

99+
99100
def test_json():
100101
a = Coordinate((1, 2, 3))
101102
b = Coordinate((None, 2, 3))
@@ -104,4 +105,10 @@ def test_json():
104105
b_json = json.dumps(b)
105106

106107
assert a == json.loads(a_json)
107-
assert b == json.loads(b_json)
108+
assert b == json.loads(b_json)
109+
110+
111+
def test_hash():
112+
assert {Coordinate(1, 2, 3): "A", Coordinate(4.5, 5.5, 6.5): "B"}[
113+
Coordinate(1, 2, 3)
114+
] == "A"

0 commit comments

Comments
 (0)