8000 Merge tag '0.6.3' · nirum-lang/nirum-python@a474673 · GitHub
[go: up one dir, main page]

Skip to content

Commit a474673

Browse files
committed
Merge tag '0.6.3'
nirum-python 0.6.3
2 parents 241cc9c + c6d4b8e commit a474673

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

CHANGES.rst

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ To be released.
1212
``parameter_annotations``) of ``nirum.transport.Transport.call()`` method
1313
now can take multiple arguments.
1414
- Since Nirum compiler became to give ``__nirum_type__`` attribute to
15-
every generated clasess (see also the `pull request`__),
15+
every generated classes (see also the `pull request`__),
1616
``nirum.deserialize.deserialize_meta()`` function also became to leverage
1717
it if present.
1818
- Since Nirum compiler became to give ``__nirum_tag_classes__`` mapping to
@@ -24,6 +24,16 @@ __ https://github.com/spoqa/nirum/pull/192
2424
__ https://github.com/spoqa/nirum/pull/192
2525

2626

27+
Version 0.6.3
28+
-------------
29+
30+
Released on April 5, 2018.
31+
32+
- Added missing equality functions (i.e., ``==``, ``!=`` operators, & ``hash()``
33+
function) to ``nirum.datastructures.Map`` and ``nirum.datastructures.List``.
34+
[`#110`_]
35+
36+
2737
Version 0.6.2
2838
-------------
2939

@@ -111,12 +121,33 @@ Released on July 11, 2017.
111121
.. _nirum-python-wsgi: https://github.com/spoqa/nirum-python-wsgi
112122

113123

124+
Version 0.5.6
125+
-------------
126+
127+
Released on April 5, 2018.
128+
129+
- Fixed a bug that ``hash()`` on ``nirum.datastructures.List`` had raised
130+
``TypeError``.
131+
132+
133+
Version 0.5.5
134+
-------------
135+
136+
Released on April 5, 2018.
137+
138+
- Added missing equality functions (i.e., ``==``, ``!=`` operators, & ``hash()``
139+
function) to ``nirum.datastructures.Map`` and ``nirum.datastructures.List``.
140+
[`#110`_]
141+
142+
.. _#110: https://github.com/spoqa/nirum-python/issues/110
143+
144+
114145
Version 0.5.4
115146
-------------
116147

117148
Released on December 9, 2017.
118149

119-
- Made `nirum.datastructures.List` to copy the given value so that
150+
- Made ``nirum.datastructures.List`` to copy the given value so that
120151
it doesn't refer given value's state and is immutable.
121152

122153

@@ -188,6 +219,16 @@ Released on June 1, 2017.
188219
.. _#71: https://github.com/spoqa/nirum-python/issues/71
189220

190221

222+
Version 0.4.3
223+
-------------
224+
225+
Released on April 5, 2018.
226+
227+
- Added missing equality functions (i.e., ``==``, ``!=`` operators, & ``hash()``
228+
function) to ``nirum.datastructures.Map`` and ``nirum.datastructures.List``.
229+
[`#110`_]
230+
231+
191232
Version 0.4.2
192233
-------------
193234

@@ -224,6 +265,6 @@ Released on March 20, 2017.
224265
``nirum.datastructures.List`` as ``list_type`` to avoid name
225266
conflict with user-defined types.
226267

268+
__ https://github.com/spoqa/nirum/blob/f1629787f45fef17eeab8b4f030c34580e0446b8/docs/serialization.md
227269
.. _#66: https://github.com/spoqa/nirum-python/pull/66
228270
.. _#49: https://github.com/spoqa/nirum-python/issues/49
229-
__ https://github.com/spoqa/nirum/blob/f1629787f45fef17eeab8b4f030c34580e0446b8/docs/serialization.md

nirum/datastructures.py

Lines changed: 32 additions & 0 deletions
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())
@@ -56,6 +72,19 @@ class List(collections.Sequence):
5672
def __init__(self, items):
5773
self.items = list(items)
5874

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)
87+
5988
def __getitem__(self, index):
6089
return self.items[index]
6190

@@ -68,6 +97,9 @@ def __contains__(self, item):
6897
def __iter__(self):
6998
return iter(self.items)
7099

100+
def __hash__(self):
101+
return hash(tuple(self.items))
102+
71103
def index(self, item):
72104
return self.items.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']
@@ -90,6 +115,31 @@ def test_list():
90115
assert immutable_list.count(3) == 0
91116

92117

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)
141+
142+
93143
def test_list_immutable():
94144
mutable_list = [1, 2]
95145
immutable_list = List(mutable_list)

0 commit comments

Comments
 (0)
0