Skip to content

Commit

Permalink
support slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed May 27, 2019
1 parent 72dd7b1 commit defb770
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
29 changes: 28 additions & 1 deletion tests/test_nod.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,38 @@ def test_new_attr_on_existing():
def test_addition_arithmetic():
a = NOD(4, 2)
b = a + a
assert a.as_list() == [8, 4]
assert b.as_list() == [8, 4]


def test_illegal_overwrite():
with pytest.raises(IllegalAttributeKey) as exc_info:
NOD(update=2)

assert exc_info.type is IllegalAttributeKey


def test_slice_get():
a = NOD(4, 2)
b = a + a
assert b[:1] == [8]
assert b[-1:] == [4]


def test_slice_get_adv():
a = NOD(4, 2)
b = a[:1] + a[0:]
assert b[:1] == [4]
assert b[-1:] == [2]


def test_slice_set():
a = NOD(4, 2)
a[:1] = [8]
assert a.as_list() == [8, 2]


def test_slice_all():
a = NOD(4, 2)
b = a + a + a
b[:1] = [8]
assert b.as_list() == [8, 6]
26 changes: 19 additions & 7 deletions warg/named_ordered_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,18 @@ def __setattr__(self, key, value):
self.__dict__[key] = value

def __getitem__(self, item) -> Any:
if isinstance(item, slice):
keys = list(self.__dict__.keys())[item]
return [self.__dict__[a] for a in keys]
return self.__dict__[item]

def __setitem__(self, key, value):
self.__dict__[key] = value
if isinstance(key, slice):
keys = list(self.__dict__.keys())[key]
for a, v in zip(keys, value):
self.__dict__[a] = v
else:
self.__dict__[key] = value

def keys(self):
return self.__dict__.keys()
Expand Down Expand Up @@ -236,24 +244,28 @@ def update(self, *args: Any, **kwargs: Any) -> None:
self.__dict__.update(args_dict)

def __add__(self, other):
cop = self.__dict__.copy()
if isinstance(other, NamedOrderedDictionary):
for k in other.keys():
if k in self.__dict__:
self.__dict__[k] += other.__dict__[k]
if k in cop:
cop[k] += other.__dict__[k]
else:
self.__dict__[k] = other.__dict__[k]
cop[k] = other.__dict__[k]
elif isinstance(other, Iterable):
for arg in other:
self.add_unnamed_arg(arg)
else:
self.add_unnamed_arg(other)
return self.__dict__
return NOD(cop)

def __sub__(self, other):
cop = self.__dict__.copy()
if isinstance(other, NamedOrderedDictionary):

for k in other.keys():
if k in self.__dict__:
self.__dict__[k] -= other.__dict__[k]
if k in cop:
cop[k] -= other.__dict__[k]
return NOD(cop)
else:
raise ArithmeticError(f"Can not subtract {type(other)} from {type(self)}")

Expand Down
2 changes: 1 addition & 1 deletion warg/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pip._internal.utils.misc import dist_is_editable

__author__ = "cnheider"
__version__ = "0.1.9"
__version__ = "0.2.0"
__version_info__ = tuple(int(segment) for segment in __version__.split("."))
__doc__ = """
Created on 27/04/2019
Expand Down

0 comments on commit defb770

Please sign in to comment.