Skip to content
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

Binary operators #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 14 additions & 11 deletions orderedmultidict/orderedmultidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,19 +815,22 @@ def __str__(self):
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self.allitems())

def __or__(self, other):
return self.__class__(chain(_get_items(self), _get_items(other)))
def __add__(self, other):
return self.__class__(
chain(self._items_iterator(self), self._items_iterator(other)))

def __ior__(self, other):
for k, v in _get_items(other):
def __iadd__(self, other):
for k, v in self._items_iterator(other):
self.add(k, value=v)
return self

def __or__(self, other):
c = self.copy()
for k, v in self._items_iterator(other):
c.set(k, value=v)
return c

def _get_items(mapping):
"""Find item iterator for an object."""
names = ('iterallitems', 'allitems', 'iteritems', 'items')
exist = (n for n in names if callable_attr(mapping, n))
for a in exist:
return getattr(mapping, a)()
raise TypeError("Object {} has no compatible items interface.".format(mapping))
def __ior__(self, other):
for k, v in self._items_iterator(other):
self.set(k, value=v)
return self
48 changes: 43 additions & 5 deletions tests/test_orderedmultidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,14 +852,36 @@ def _items(self):
return original, one_different, all_different, duplicate_key, empty

@property
def _or_params(self):
original, one_different, all_different, duplicate_key, empty = self._items
def _add_params(self):
original, one_different, all_different, duplicate_key, empty = \
self._items
return [
# self, other, other as dict, other as omdict
(original, original, original + original, original + original),
(original, one_different, original + one_different, original + one_different),
(original, all_different, original + all_different, original + all_different),
(original, duplicate_key, original + ((1, 'e'),), original + duplicate_key),
(original, one_different, original + one_different,
original + one_different),
(original, all_different, original + all_different,
original + all_different),
(original, duplicate_key, original + ((1, 'e'),),
original + duplicate_key),
(original, empty, original, original),
]

@property
def _or_params(self):
original, one_different, all_different, duplicate_key, empty = \
self._items
return [
# self items, other items, other as dict result, other as omdict
# result can intermittently fail with regular dict because of
# order, use odict
(original, original, original, original),
(original, one_different, original + ((3, 'd'),),
original + ((3, 'd'),)),
(original, all_different, ((1, 'c'), (2, 'b'), (3, 'd')),
((1, 'c'), (2, 'b'), (3, 'd'))),
(original, duplicate_key, ((1, 'e'), (2, 'b')),
((1, 'e'), (2, 'b'))),
(original, empty, original, original),
]

Expand All @@ -879,6 +901,22 @@ def test_ior(self):
a |= omdict(t)
assert a == omdict(o)

def test_add(self):
for s, t, d, o in self._add_params:
assert omdict(s) + odict(t) == omdict(d)
assert omdict(s) + omdict(t) == omdict(o)

def test_iadd(self):
for s, t, d, o in self._add_params:
# test with dict
a = omdict(s)
a += odict(t)
assert a == omdict(d)
# test with omdict
a = omdict(s)
a += omdict(t)
assert a == omdict(o)


class TestUtilities(unittest.TestCase):

Expand Down