Skip to content

chainable_set_base

chainable_set_base

This module providesa superclass for filter class implementations.

NB: This is used internally, and is not intended for use by solvis API users.

Classes:

Name Description
ChainableSetBase

a base class to help making subclass methods chainable & set-like

ChainableSetBase

A base class to help making subclass methods chainable & set-like.

Source code in solvis/filter/chainable_set_base.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class ChainableSetBase:
    """A base class to help making subclass methods chainable & set-like."""

    _chained_set: Set[Any] = set()  # the set

    @property
    def chained_set(self) -> Set[Any]:
        return self._chained_set

    def new_chainable_set(
        self, result, *init_args, join_prior: Union[SetOperationEnum, str] = SetOperationEnum.INTERSECTION
    ) -> 'ChainableSetBase':

        if isinstance(join_prior, str):
            join_prior = SetOperationEnum.__members__[join_prior.upper()]

        instance = self.__class__(*init_args)

        if join_prior == SetOperationEnum.INTERSECTION:
            instance._chained_set = set.intersection(result, self.chained_set) if self.chained_set else result
        elif join_prior == SetOperationEnum.UNION:
            instance._chained_set = set.union(result, self.chained_set) if self.chained_set else result
        elif join_prior == SetOperationEnum.DIFFERENCE:
            instance._chained_set = set.difference(result, self.chained_set) if self.chained_set else result
        elif join_prior == SetOperationEnum.SYMMETRIC_DIFFERENCE:
            instance._chained_set = set.symmetric_difference(result, self.chained_set) if self.chained_set else result
        else:
            raise ValueError(f"Unsupported join type {join_prior}")  # pragma: no cover
        return instance

    def __eq__(self, other) -> bool:
        return other == self.chained_set

    def __iter__(self):
        return self.chained_set.__iter__()

    def __len__(self):
        return self.chained_set.__len__()

    # Set logical operands (&, |, -)

    def __or__(self, *others):
        return self.union(*others)

    def __and__(self, *others):
        return self.intersection(*others)

    def __sub__(self, *others):
        return self.difference(*others)

    def __xor__(self, *other):
        return self.symmetric_difference(*other)

    def __le__(self, *other) -> bool:
        """Test whether every element in the set is in other."""
        return self.issubset(*other)

    def __lt__(self, *other) -> bool:
        """Test whether the set is a proper subset of other.

        That is, set <= other and set != other.
        """
        return self.issubset(*other) and not self.__eq__(*other)

    def __ge__(self, *other) -> bool:
        """Test whether every element in other is in the set."""
        return self.issuperset(*other)

    def __gt__(self, *other) -> bool:
        """Test whether the set is a proper superset of other.

        That is, set >= other and set != other.
        """
        return self.issuperset(*other) and not self.__eq__(*other)

    # Set methods are proxied to the _chained_set ...
    def union(self, *others):
        instance = copy.deepcopy(self)
        instance._chained_set = self.chained_set.union(*others)
        return instance

    def intersection(self, *others):
        instance = copy.deepcopy(self)
        instance._chained_set = self.chained_set.intersection(*others)
        return instance

    def difference(self, *others):
        instance = copy.deepcopy(self)
        instance._chained_set = self.chained_set.difference(*others)
        return instance

    def symmetric_difference(self, *other):
        instance = copy.deepcopy(self)
        instance._chained_set = self.chained_set.symmetric_difference(*other)
        return instance

    def issuperset(self, *others) -> bool:
        return self.chained_set.issuperset(*others)

    def issubset(self, *other) -> bool:
        return self.chained_set.issubset(*other)

chained_set: Set[Any] property

__and__(*others)

Source code in solvis/filter/chainable_set_base.py
60
61
def __and__(self, *others):
    return self.intersection(*others)

__eq__(other) -> bool

Source code in solvis/filter/chainable_set_base.py
46
47
def __eq__(self, other) -> bool:
    return other == self.chained_set

__ge__(*other) -> bool

Test whether every element in other is in the set.

Source code in solvis/filter/chainable_set_base.py
80
81
82
def __ge__(self, *other) -> bool:
    """Test whether every element in other is in the set."""
    return self.issuperset(*other)

__gt__(*other) -> bool

Test whether the set is a proper superset of other.

That is, set >= other and set != other.

Source code in solvis/filter/chainable_set_base.py
84
85
86
87
88
89
def __gt__(self, *other) -> bool:
    """Test whether the set is a proper superset of other.

    That is, set >= other and set != other.
    """
    return self.issuperset(*other) and not self.__eq__(*other)

__iter__()

Source code in solvis/filter/chainable_set_base.py
49
50
def __iter__(self):
    return self.chained_set.__iter__()

__le__(*other) -> bool

Test whether every element in the set is in other.

Source code in solvis/filter/chainable_set_base.py
69
70
71
def __le__(self, *other) -> bool:
    """Test whether every element in the set is in other."""
    return self.issubset(*other)

__len__()

Source code in solvis/filter/chainable_set_base.py
52
53
def __len__(self):
    return self.chained_set.__len__()

__lt__(*other) -> bool

Test whether the set is a proper subset of other.

That is, set <= other and set != other.

Source code in solvis/filter/chainable_set_base.py
73
74
75
76
77
78
def __lt__(self, *other) -> bool:
    """Test whether the set is a proper subset of other.

    That is, set <= other and set != other.
    """
    return self.issubset(*other) and not self.__eq__(*other)

__or__(*others)

Source code in solvis/filter/chainable_set_base.py
57
58
def __or__(self, *others):
    return self.union(*others)

__sub__(*others)

Source code in solvis/filter/chainable_set_base.py
63
64
def __sub__(self, *others):
    return self.difference(*others)

__xor__(*other)

Source code in solvis/filter/chainable_set_base.py
66
67
def __xor__(self, *other):
    return self.symmetric_difference(*other)

difference(*others)

Source code in solvis/filter/chainable_set_base.py
102
103
104
105
def difference(self, *others):
    instance = copy.deepcopy(self)
    instance._chained_set = self.chained_set.difference(*others)
    return instance

intersection(*others)

Source code in solvis/filter/chainable_set_base.py
 97
 98
 99
100
def intersection(self, *others):
    instance = copy.deepcopy(self)
    instance._chained_set = self.chained_set.intersection(*others)
    return instance

issubset(*other) -> bool

Source code in solvis/filter/chainable_set_base.py
115
116
def issubset(self, *other) -> bool:
    return self.chained_set.issubset(*other)

issuperset(*others) -> bool

Source code in solvis/filter/chainable_set_base.py
112
113
def issuperset(self, *others) -> bool:
    return self.chained_set.issuperset(*others)

new_chainable_set(result, *init_args, join_prior: Union[SetOperationEnum, str] = SetOperationEnum.INTERSECTION) -> ChainableSetBase

Source code in solvis/filter/chainable_set_base.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def new_chainable_set(
    self, result, *init_args, join_prior: Union[SetOperationEnum, str] = SetOperationEnum.INTERSECTION
) -> 'ChainableSetBase':

    if isinstance(join_prior, str):
        join_prior = SetOperationEnum.__members__[join_prior.upper()]

    instance = self.__class__(*init_args)

    if join_prior == SetOperationEnum.INTERSECTION:
        instance._chained_set = set.intersection(result, self.chained_set) if self.chained_set else result
    elif join_prior == SetOperationEnum.UNION:
        instance._chained_set = set.union(result, self.chained_set) if self.chained_set else result
    elif join_prior == SetOperationEnum.DIFFERENCE:
        instance._chained_set = set.difference(result, self.chained_set) if self.chained_set else result
    elif join_prior == SetOperationEnum.SYMMETRIC_DIFFERENCE:
        instance._chained_set = set.symmetric_difference(result, self.chained_set) if self.chained_set else result
    else:
        raise ValueError(f"Unsupported join type {join_prior}")  # pragma: no cover
    return instance

symmetric_difference(*other)

Source code in solvis/filter/chainable_set_base.py
107
108
109
110
def symmetric_difference(self, *other):
    instance = copy.deepcopy(self)
    instance._chained_set = self.chained_set.symmetric_difference(*other)
    return instance

union(*others)

Source code in solvis/filter/chainable_set_base.py
92
93
94
95
def union(self, *others):
    instance = copy.deepcopy(self)
    instance._chained_set = self.chained_set.union(*others)
    return instance