-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add abstract mapping collection with support for set op…
…erations
- Loading branch information
Showing
12 changed files
with
291 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Mapping | ||
|
||
from typing_extensions import Self | ||
|
||
|
||
class MapSet(Mapping): | ||
def _check_conflict(self, other): | ||
common_keys = self.keys() & other.keys() | ||
for key in common_keys: | ||
left, right = self[key], other[key] | ||
if left != right: | ||
raise ValueError( | ||
f"Conflicting values for key `{key}`: {left} != {right}" | ||
) | ||
return common_keys | ||
|
||
def __ge__(self, other: Self) -> bool: | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
common_keys = self._check_conflict(other) | ||
return other.keys() == common_keys | ||
|
||
def __gt__(self, other: Self) -> bool: | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
return len(self) > len(other) and self.__ge__(other) | ||
|
||
def __le__(self, other: Self) -> bool: | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
common_keys = self._check_conflict(other) | ||
return self.keys() == common_keys | ||
|
||
def __lt__(self, other: Self) -> bool: | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
return len(self) < len(other) and self.__le__(other) | ||
|
||
def __and__(self, other: Self) -> Self: | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
common_keys = self._check_conflict(other) | ||
intersection = {k: v for k, v in self.items() if k in common_keys} | ||
return self.__class__(intersection) | ||
|
||
def __sub__(self, other: Self) -> Self: | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
common_keys = self._check_conflict(other) | ||
difference = {k: v for k, v in self.items() if k not in common_keys} | ||
return self.__class__(difference) | ||
|
||
def __or__(self, other: Self) -> Self: | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
self._check_conflict(other) | ||
union = {**self, **other} | ||
return self.__class__(union) | ||
|
||
def __xor__(self, other: Self) -> Self: | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
left = self - other | ||
right = other - self | ||
left._check_conflict(right) | ||
union = {**left, **right} | ||
return self.__class__(union) | ||
|
||
def isdisjoint(self, other: Self) -> bool: | ||
common_keys = self._check_conflict(other) | ||
return not common_keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
from collections.abc import ItemsView, Iterator, KeysView, ValuesView | ||
|
||
import pytest | ||
|
||
from ibis.common.collections import MapSet | ||
|
||
|
||
class MySchema(MapSet): | ||
def __init__(self, dct=None, **kwargs): | ||
self._fields = dict(dct or kwargs) | ||
|
||
def __repr__(self): | ||
return f'{self.__class__.__name__}({self._fields})' | ||
|
||
def __getitem__(self, key): | ||
return self._fields[key] | ||
|
||
def __iter__(self): | ||
return iter(self._fields) | ||
|
||
def __len__(self): | ||
return len(self._fields) | ||
|
||
def identical(self, other): | ||
return type(self) == type(other) and tuple(self.items()) == tuple(other.items()) | ||
|
||
|
||
def test_myschema_identical(): | ||
ms1 = MySchema(a=1, b=2) | ||
ms2 = MySchema(a=1, b=2) | ||
ms3 = MySchema(b=2, a=1) | ||
ms4 = MySchema(a=1, b=2, c=3) | ||
ms5 = {} | ||
|
||
assert ms1.identical(ms2) | ||
assert not ms1.identical(ms3) | ||
assert not ms1.identical(ms4) | ||
assert not ms1.identical(ms5) | ||
|
||
|
||
def test_mapset_mapping_api(): | ||
ms = MySchema(a=1, b=2) | ||
assert ms['a'] == 1 | ||
assert ms['b'] == 2 | ||
assert len(ms) == 2 | ||
assert isinstance(iter(ms), Iterator) | ||
assert list(ms) == ['a', 'b'] | ||
assert isinstance(ms.keys(), KeysView) | ||
assert list(ms.keys()) == ['a', 'b'] | ||
assert isinstance(ms.values(), ValuesView) | ||
assert list(ms.values()) == [1, 2] | ||
assert isinstance(ms.items(), ItemsView) | ||
assert list(ms.items()) == [('a', 1), ('b', 2)] | ||
assert ms.get('a') == 1 | ||
assert ms.get('c') is None | ||
assert ms.get('c', 3) == 3 | ||
assert 'a' in ms | ||
assert 'c' not in ms | ||
assert ms == ms | ||
assert ms != MySchema(a=1, b=2, c=3) | ||
|
||
|
||
def test_mapset_set_api(): | ||
a = MySchema(a=1, b=2) | ||
a_ = MySchema(a=1, b=-2) | ||
b = MySchema(a=1, b=2, c=3) | ||
b_ = MySchema(a=1, b=2, c=-3) | ||
f = MySchema(d=4, e=5) | ||
|
||
# disjoint | ||
assert not a.isdisjoint(b) | ||
assert a.isdisjoint(f) | ||
|
||
# __eq__, __ne__ | ||
assert a == a | ||
assert a != a_ | ||
assert b == b | ||
assert b != b_ | ||
|
||
# __le__, __lt__ | ||
assert a < b | ||
assert a <= b | ||
assert a <= a | ||
assert not b <= a | ||
assert not b < a | ||
with pytest.raises(ValueError, match="Conflicting values"): | ||
# duplicate keys with different values | ||
a <= a_ # noqa: B015 | ||
|
||
# __gt__, __ge__ | ||
assert b > a | ||
assert b >= a | ||
assert a >= a | ||
assert not a >= b | ||
assert not a > b | ||
assert not a_ > a | ||
with pytest.raises(ValueError, match="Conflicting values"): | ||
a_ >= a # noqa: B015 | ||
|
||
# __and__ | ||
with pytest.raises(ValueError, match="Conflicting values"): | ||
a & a_ | ||
with pytest.raises(ValueError, match="Conflicting values"): | ||
b & b_ | ||
assert (a & b).identical(a) | ||
assert (a & f).identical(MySchema()) | ||
|
||
# __or__ | ||
assert (a | a).identical(a) | ||
assert (a | b).identical(b) | ||
assert (a | f).identical(MySchema(a=1, b=2, d=4, e=5)) | ||
with pytest.raises(ValueError, match="Conflicting values"): | ||
a | a_ | ||
|
||
# __sub__ | ||
with pytest.raises(ValueError, match="Conflicting values"): | ||
a - a_ | ||
assert (a - b).identical(MySchema()) | ||
assert (b - a).identical(MySchema(c=3)) | ||
assert (a - f).identical(a) | ||
assert (f - a).identical(f) | ||
|
||
# __xor__ | ||
with pytest.raises(ValueError, match="Conflicting values"): | ||
a ^ a_ | ||
|
||
assert (a ^ b).identical(MySchema(c=3)) | ||
assert (b ^ a).identical(MySchema(c=3)) | ||
assert (a ^ f).identical(MySchema(a=1, b=2, d=4, e=5)) | ||
assert (f ^ a).identical(MySchema(d=4, e=5, a=1, b=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.