8000 Add List by kanghyojun · Pull Request #65 · nirum-lang/nirum-python · GitHub
[go: up one dir, main page]

Skip to content

Add List #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ To be released.
- Added :mod:`nirum.datastructures` module and
:class:`~nirum.datastructures.Map` which is an immutable dictionary.
[:issue:`66`]
- Added :class:`nirum.datastructures.List` which is an immutable list.
[:issue:`49`]

__ https://github.com/spoqa/nirum/blob/f1629787f45fef17eeab8b4f030c34580e0446b8/docs/serialization.md
26 changes: 25 additions & 1 deletion nirum/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
import collections

__all__ = 'Map',
__all__ = 'List', 'Map'


class Map(collections.Mapping):
Expand Down Expand Up @@ -49,3 +49,27 @@ def __repr__(self):
else:
args = ''
return '{0.__module__}.{0.__name__}({1})'.format(type(self), args)


class List(collections.Sequence):

def __init__(self, l):
self.l = l

def __getitem__(self, index):
return self.l[index]

def __len__(self):
return len(self.l)

def __contains__(self, item):
return item in self.l

def __iter__(self):
return iter(self.l)

def index(self, item):
return self.l.index(item)

def count(self, item):
return self.l.count(item)
2 changes: 1 addition & 1 deletion nirum/deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from iso8601 import iso8601, parse_date
from six import text_type

from ._compat import get_tuple_param_types, get_union_types, is_union_type
from .datastructures import Map
from ._compat import get_tuple_param_types, get_union_types, is_union_type

__all__ = (
'deserialize_abstract_type',
Expand Down
22 changes: 21 additions & 1 deletion tests/datastructures_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pytest import raises

from nirum.datastructures import Map
from nirum.datastructures import List, Map


def test_map_init():
Expand Down Expand Up @@ -68,3 +68,23 @@ def test_map_repr():
assert repr(Map()) == 'nirum.datastructures.Map()'
assert repr(Map(a=1)) == "nirum.datastructures.Map({'a': 1})"
assert repr(Map(a=1, b=2)) == "nirum.datastructures.Map({'a': 1, 'b': 2})"


def test_list():
immutable_list = List([1, 2])
with raises(AttributeError):
immutable_list.append(1)

with raises(TypeError):
immutable_list + [3]

assert isinstance(immutable_list, collections.Sequence)
assert not isinstance(immutable_list, collections.MutableSequence)
assert immutable_list[0] == 1
assert len(immutable_list) == 2
assert 2 in immutable_list
assert next(iter(immutable_list)) == 1
assert immutable_list.index(2) == 1
assert immutable_list.count(1) == 1
assert immutable_list.count(2) == 1
assert immutable_list.count(3) == 0
0