Skip to content

Commit

Permalink
Implement SortedDict methods: __or__, __ror__, and __ior__ (#171)
Browse files Browse the repository at this point in the history
Implement SortedDict methods: __or__, __ror__, and __ior__ (#171)

Fixes #169 .
  • Loading branch information
grantjenks committed May 16, 2021
1 parent 9887989 commit 2678a78
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
27 changes: 25 additions & 2 deletions sortedcontainers/sorteddict.py
Expand Up @@ -19,6 +19,8 @@
import sys
import warnings

from itertools import chain

from .sortedlist import SortedList, recursive_repr
from .sortedset import SortedSet

Expand All @@ -27,9 +29,11 @@
###############################################################################

try:
from collections.abc import ItemsView, KeysView, ValuesView, Sequence
from collections.abc import (
ItemsView, KeysView, Mapping, ValuesView, Sequence
)
except ImportError:
from collections import ItemsView, KeysView, ValuesView, Sequence
from collections import ItemsView, KeysView, Mapping, ValuesView, Sequence

###############################################################################
# END Python 2/3 Shims
Expand Down Expand Up @@ -298,6 +302,25 @@ def __setitem__(self, key, value):
_setitem = __setitem__


def __or__(self, other):
if not isinstance(other, Mapping):
return NotImplemented
items = chain(self.items(), other.items())
return self.__class__(self._key, items)


def __ror__(self, other):
if not isinstance(other, Mapping):
return NotImplemented
items = chain(other.items(), self.items())
return self.__class__(self._key, items)


def __ior__(self, other):
self._update(other)
return self


def copy(self):
"""Return a shallow copy of the sorted dict.
Expand Down
35 changes: 35 additions & 0 deletions tests/test_coverage_sorteddict.py
Expand Up @@ -491,3 +491,38 @@ def test_ref_counts():
del temp
del_count = len(gc.get_objects())
assert start_count == del_count

class CustomOr:
def __or__(self, other):
return NotImplemented

def __ror__(self, other):
return self

def test_or():
mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
temp1 = SortedDict(mapping[:13])
temp2 = SortedDict(mapping[13:])
temp3 = temp1 | temp2
assert temp3 == dict(mapping)

def test_or_not_implemented():
SortedDict() | CustomOr()

def test_ror():
mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
temp1 = dict(mapping[:13])
temp2 = SortedDict(mapping[13:])
temp3 = temp1 | temp2
assert temp3 == dict(mapping)

def test_ror_not_implemented():
with pytest.raises(TypeError):
CustomOr() | SortedDict()

def test_ior():
mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
temp1 = SortedDict(mapping[:13])
temp2 = SortedDict(mapping[13:])
temp1 |= temp2
assert temp1 == dict(mapping)

0 comments on commit 2678a78

Please sign in to comment.