8000 Merge pull request #111 from dahlia/map-list-equality · nirum-lang/nirum-python@1da9d1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1da9d1f

Browse files
authored
Merge pull request #111 from dahlia/map-list-equality
Equality functions for Map and List
2 parents 112606c + 1d0d625 commit 1da9d1f

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Version 0.4.3
66

77
To be released.
88

9+
- Added missing equality functions (i.e., ``==``, ``!=`` operators, & ``hash()``
10+
function) to ``nirum.datastructures.Map`` and ``nirum.datastructures.List``.
11+
[`#110`_]
12+
13+
.. _#110: https://github.com/spoqa/nirum-python/issues/110
14+
915

1016
Version 0.4.2
1117
-------------

nirum/datastructures.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ def __init__(self, *args, **kwargs):
2121
# TODO: type check on elements
2222
self.value = dict(*args, **kwargs)
2323

24+
def __eq__(self, other):
25+
if not (isinstance(other, collections.Mapping) and
26+
len(self.value) == len(other)):
27+
return False
28+
for k, v in self.items():
29+
if k in other and other[k] == v:
30+
continue
31+
return False
32+
return True
33+
34+
def __ne__(self, other):
35+
return not (self == other)
36+
2437
def __iter__(self):
2538
return iter(self.value)
2639

@@ -41,6 +54,9 @@ def __bool__(self):
4154

4255
__nonzero__ = __bool__
4356

57+
def __hash__(self):
58+
return hash(tuple(self.items()))
59+
4460
def __repr__(self):
4561
if self:
4662
items = sorted(self.value.items())
@@ -54,7 +70,20 @@ def __repr__(self):
5470
class List(collections.Sequence):
5571

5672
def __init__(self, l):
57-
self.l = l
73+
self.l = l # noqa: E741
74+
75+
def __eq__(self, other):
76+
if not (isinstance(other, collections.Sequence) and
77+
len(self) == len(other)):
78+
return False
79+
for a, b in zip(self, other):
80+
if a == b:
81+
continue
82+
return False
83+
return True
84+
85+
def __ne__(self, other):
86+
return not (self == other)
5887

5988
def __getitem__(self, index):
6089
return self.l[index]
@@ -68,6 +97,9 @@ def __contains__(self, item):
6897
def __iter__(self):
6998
return iter(self.l)
7099

100+
def __hash__(self):
101+
return hash(tuple(self))
102+
71103
def index(self, item):
72104
return self.l.index(item)
73105

tests/datastructures_test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ def test_map_init():
2020
assert not isinstance(Map(), collections.MutableMapping)
2121

2222

23+
def test_map_equality(fx_record_type, fx_unboxed_type):
24+
Point = fx_record_type
25+
Offset = fx_unboxed_type
26+
a = Map([
27+
(Offset(1.), Point(left=Offset(1.), top=Offset(2.0))),
28+
(Offset(3.), Point(left=Offset(3.), top=Offset(4.0))),
29+
])
30+
b = Map([
31+
(Offset(1.), Point(left=Offset(1.), top=Offset(2.0))),
32+
(Offset(3.), Point(left=Offset(3.), top=Offset(4.0))),
33+
])
34+
c = Map([
35+
(Offset(1.), Point(left=Offset(1.), top=Offset(2.0))),
36+
(Offset(3.), Point(left=Offset(3.), top=Offset(5.0))),
37+
])
38+
assert a == b
39+
assert not (a != b)
40+
assert hash(a) == hash(b)
41+
assert b != c
42+
assert not (b == c)
43+
assert hash(b) != hash(c)
44+
assert a != c
45+
assert not (a == c)
46+
47+
2348
def test_map_iter():
2449
assert list(Map()) == []
2550
assert list(Map(a=1)) == ['a']
@@ -88,3 +113,28 @@ def test_list():
88113
assert immutable_list.count(1) == 1
89114
assert immutable_list.count(2) == 1
90115
assert immutable_list.count(3) == 0
116+
117+
118+
def test_list_equality(fx_record_type, fx_unboxed_type):
119+
Point = fx_record_type
120+
Offset = fx_unboxed_type
121+
a = List([
122+
Point(left=Offset(1.), top=Offset(2.)),
123+
Point(left=Offset(3.), top=Offset(4.)),
124+
])
125+
b = List([
126+
Point(left=Offset(1.), top=Offset(2.)),
127+
Point(left=Offset(3.), top=Offset(4.)),
128+
])
129+
c = List([
130+
Point(left=Offset(1.), top=Offset(2.)),
131+
Point(left=Offset(3.), top=Offset(5.)),
132+
])
133+
assert a == b
134+
assert not (a != b)
135+
assert hash(a) == hash(b)
136+
assert b != c
137+
assert not (b == c)
138+
assert hash(b) != hash(c)
139+
assert a != c
140+
assert not (a == c)

0 commit comments

Comments
 (0)
0