File tree 4 files changed +49
-3
lines changed 4 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -11,5 +11,7 @@ To be released.
11
11
- Added :mod: `nirum.datastructures ` module and
12
12
:class: `~nirum.datastructures.Map ` which is an immutable dictionary.
13
13
[:issue: `66 `]
14
+ - Added :class: `nirum.datastructures.List ` which is an immutable list.
15
+ [:issue: `49 `]
14
16
15
17
__ https://github.com/spoqa/nirum/blob/f1629787f45fef17eeab8b4f030c34580e0446b8/docs/serialization.md
Original file line number Diff line number Diff line change 6
6
"""
7
7
import collections
8
8
9
- __all__ = 'Map' ,
9
+ __all__ = 'List' , 'Map'
10
10
11
11
12
12
class Map (collections .Mapping ):
@@ -49,3 +49,27 @@ def __repr__(self):
49
49
else :
50
50
args = ''
51
51
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 )
Original file line number Diff line number Diff line change 13
13
from iso8601 import iso8601 , parse_date
14
14
from six import text_type
15
15
16
- from ._compat import get_tuple_param_types , get_union_types , is_union_type
17
16
from .datastructures import Map
17
+ from ._compat import get_tuple_param_types , get_union_types , is_union_type
18
18
19
19
__all__ = (
20
20
'deserialize_abstract_type' ,
Original file line number Diff line number Diff line change 3
3
4
4
from pytest import raises
5
5
6
- from nirum .datastructures import Map
6
+ from nirum .datastructures import List , Map
7
7
8
8
9
9
def test_map_init ():
@@ -68,3 +68,23 @@ def test_map_repr():
68
68
assert repr (Map ()) == 'nirum.datastructures.Map()'
69
69
assert repr (Map (a = 1 )) == "nirum.datastructures.Map({'a': 1})"
70
70
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
You can’t perform that action at this time.
0 commit comments