8000 Add List (#65) · nirum-lang/nirum-python@64b59e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 64b59e9

Browse files
authored
Add List (#65)
* Add ImmutableList * Rename it * Move test code to datastructures_test.py * Rename ImmutableList -> List * Teset whether immutable_list is mutable or not * Write changelog for nirum.datastructures.List
1 parent b3e3b28 commit 64b59e9

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ To be released.
1111
- Added :mod:`nirum.datastructures` module and
1212
:class:`~nirum.datastructures.Map` which is an immutable dictionary.
1313
[:issue:`66`]
14+
- Added :class:`nirum.datastructures.List` which is an immutable list.
15+
[:issue:`49`]
1416

1517
__ https://github.com/spoqa/nirum/blob/f1629787f45fef17eeab8b4f030c34580e0446b8/docs/serialization.md

nirum/datastructures.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77
import collections
88

9-
__all__ = 'Map',
9+
__all__ = 'List', 'Map'
1010

1111

1212
class Map(collections.Mapping):
@@ -49,3 +49,27 @@ def __repr__(self):
4949
else:
5050
args = ''
5151
return '{0.__module__}.{0.__name__}({1})'.format(type(self), args)
52+
53+
54+
class List(collections.Sequence):
55+
56+
def __init__(self, l):
57+
self.l = l
58+
59+
def __getitem__(self, index):
60+
return self.l[index]
61+
62+
def __len__(self):
63+
return len(self.l)
64+
65+
def __contains__(self, item):
66+
return item in self.l
67+
68+
def __iter__(self):
69+
return iter(self.l)
70+
71+
def index(self, item):
72+
return self.l.index(item)
73+
74+
def count(self, item):
75+
return self.l.count(item)

nirum/deserialize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from iso8601 import iso8601, parse_date
1414
from six import text_type
1515

16-
from ._compat import get_tuple_param_types, get_union_types, is_union_type
1716
from .datastructures import Map
17+
from ._compat import get_tuple_param_types, get_union_types, is_union_type
1818

1919
__all__ = (
2020
'deserialize_abstract_type',

tests/datastructures_test.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from pytest import raises
55

6-
from nirum.datastructures import Map
6+
from nirum.datastructures import List, Map
77

88

99
def test_map_init():
@@ -68,3 +68,23 @@ def test_map_repr():
6868
assert repr(Map()) == 'nirum.datastructures.Map()'
6969
assert repr(Map(a=1)) == "nirum.datastructures.Map({'a': 1})"
7070
assert repr(Map(a=1, b=2)) == "nirum.datastructures.Map({'a': 1, 'b': 2})"
71+
72+
73+
def test_list():
74+
immutable_list = List([1, 2])
75+
with raises(AttributeError):
76+
immutable_list.append(1)
77+
78+
with raises(TypeError):
79+
immutable_list + [3]
80+
81+
assert isinstance(immutable_list, collections.Sequence)
82+
assert not isinstance(immutable_list, collections.MutableSequence)
83+
assert immutable_list[0] == 1
84+
assert len(immutable_list) == 2
85+
assert 2 in immutable_list
86+
assert next(iter(immutable_list)) == 1
87+
assert immutable_list.index(2) == 1
88+
assert immutable_list.count(1) == 1
89+
assert immutable_list.count(2) == 1
90+
assert immutable_list.count(3) == 0

0 commit comments

Comments
 (0)
0