Skip to content

PyoSet

Bases: PyoCollection[T], Set[T]

Base trait for eager pyochain set-like collections.

PyoSet[T] is the shared trait for concrete, eager set-like collections: Set and FrozenSet.

It extends PyoCollection[T] and collections.abc.Set[T].

This is equivalent to subclassing collections.abc.Set[T] (this trait already does), meaning any concrete subclass must implement the required Set dunder methods:

  • __contains__
  • __iter__
  • __len__

On top of the standard Set protocol, it provides the additional pyochain API (from PyoCollection, Pipeable, Checkable, plus any helpers defined here).

Source code in src/pyochain/traits/_iterable.py
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
class PyoSet[T](PyoCollection[T], AbstractSet[T]):
    """Base trait for eager pyochain set-like collections.

    `PyoSet[T]` is the shared trait for concrete, eager set-like
    collections: `Set` and `FrozenSet`.

    It extends `PyoCollection[T]` and `collections.abc.Set[T]`.

    This is equivalent to subclassing `collections.abc.Set[T]` (this trait
    already does), meaning any concrete subclass must implement the required
    `Set` dunder methods:

    - `__contains__`
    - `__iter__`
    - `__len__`

    On top of the standard `Set` protocol, it provides the additional
    pyochain API (from `PyoCollection`, `Pipeable`, `Checkable`, plus any helpers defined here).

    """

    __slots__ = ()

    def is_subset(self, other: AbstractSet[T]) -> bool:
        """Check if the `Set` is a subset of another set.

        Args:
            other (AbstractSet[T]): The other set to compare with.

        Returns:
            bool: True if the `Set` is a subset of the other set, False otherwise.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({1, 2}).is_subset({1, 2, 3})
        True
        >>> pc.Set({1, 4}).is_subset({1, 2, 3})
        False

        ```
        """
        return self <= other

    def is_subset_strict(self, other: AbstractSet[T]) -> bool:
        """Check if the `Set` is a proper subset of another set.

        Args:
            other (AbstractSet[T]): The other set to compare with.

        Returns:
            bool: `True` if the `Set` is a proper subset of the other set, `False` otherwise.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({1, 2}).is_subset_strict({1, 2, 3})
        True
        >>> pc.Set({1, 2}).is_subset_strict({1, 2})
        False

        ```
        """
        return self < other

    def eq(self, other: AbstractSet[T]) -> bool:
        """Check if the `Set` is equal to another set.

        Args:
            other (AbstractSet[T]): The other set to compare with.

        Returns:
            bool: True if the `Set` is equal to the other set, False otherwise.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({1, 2}).eq({2, 1})
        True
        >>> pc.Set({1, 2}).eq({1, 2, 3})
        False

        ```
        """
        return self == other

    def is_superset(self, other: AbstractSet[T]) -> bool:
        """Check if the `Set` is a superset of another set.

        Args:
            other (AbstractSet[T]): The other set to compare with.

        Returns:
            bool: True if the `Set` is a superset of the other set, False otherwise.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({1, 2, 3}).is_superset({1, 2})
        True
        >>> pc.Set({1, 2}).is_superset({1, 2, 3})
        False

        ```
        """
        return self >= other

    def intersection(self, other: AbstractSet[T]) -> Self:
        """Return the intersection of the `Set` with another set.

        Args:
            other (AbstractSet[T]): The other set to intersect with.

        Returns:
            Self: A new `Set` containing the intersection of the two sets.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({1, 2, 2}).intersection({2, 3})
        Set(2,)

        ```
        """
        return self.__class__(self & other)

    def r_intersection(self, other: AbstractSet[T]) -> Self:
        """Return the intersection of another set with the `Set`.

        Args:
            other (AbstractSet[T]): The other set to intersect with.

        Returns:
            Self: A new `Set` containing the intersection of the two sets.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({2, 3}).r_intersection({1, 2})
        Set(2,)

        ```
        """
        return self.__class__(other & self)

    def union(self, other: AbstractSet[T]) -> Self:
        """Return the union of the `Set` with another set.

        Args:
            other (AbstractSet[T]): The other set to unite with.

        Returns:
            Self: A new `Set` containing the union of the two sets.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({1, 2, 2}).union([2, 3]).union([4]).iter().sort()
        Vec(1, 2, 3, 4)

        ```
        """
        return self.__class__(self | other)

    def r_union(self, other: AbstractSet[T]) -> Self:
        """Return the union of another set with the `Set`.

        Args:
            other (AbstractSet[T]): The other set to unite with.

        Returns:
            Self: A new `Set` containing the union of the two sets.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({2, 3}).r_union({1, 2}).iter().sort()
        Vec(1, 2, 3)

        ```
        """
        return self.__class__(other | self)

    def difference(self, other: AbstractSet[T]) -> Self:
        """Return the difference of the `Set` with another set.

        Args:
            other (AbstractSet[T]): The other set to subtract.

        Returns:
            Self: A new `Set` containing the difference of the two sets.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({1, 2, 2}).difference([2, 3])
        Set(1,)

        ```
        """
        return self.__class__(self - other)

    def r_difference(self, other: AbstractSet[T]) -> Self:
        """Return the reverse difference of the `Set` with another set.

        Args:
            other (AbstractSet[T]): The other set to subtract from.

        Returns:
            Self: A new `Set` containing the reverse difference of the two sets.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({2, 3}).r_difference({1, 2})
        Set(1,)

        ```
        """
        return self.__class__(other - self)

    def symmetric_difference(self, other: AbstractSet[T]) -> Self:
        """Return the symmetric difference of the `Set` with another set.

        Args:
            other (AbstractSet[T]): The other set to symmetric difference with.

        Returns:
            Self: A new `Set` containing the symmetric difference of the two sets.
        ```python
        >>> import pyochain as pc
        >>> pc.Set({1, 2, 2}).symmetric_difference([2, 3]).iter().sort()
        Vec(1, 3)
        >>> pc.Set({1, 2, 3}).symmetric_difference([3, 4, 5]).iter().sort()
        Vec(1, 2, 4, 5)

        ```
        """
        return self.__class__(self ^ other)

    def r_symmetric_difference(self, other: AbstractSet[T]) -> Self:
        """Return the symmetric difference of the `Set` with another set.

        Args:
            other (AbstractSet[T]): The other set to symmetric difference with.

        Returns:
            Self: A new `Set` containing the symmetric difference of the two sets.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({2, 3}).r_symmetric_difference({1, 2}).iter().sort()
        Vec(1, 3)

        ```
        """
        return self.__class__(other ^ self)

    def is_disjoint(self, other: AbstractSet[T]) -> bool:
        """Check if the `Set` has no elements in common with another set.

        Args:
            other (AbstractSet[T]): The other set to compare with.

        Returns:
            bool: True if the `Set` and the other set have no elements in common, False otherwise.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Set({1, 2}).is_disjoint({3, 4})
        True
        >>> pc.Set({1, 2}).is_disjoint({2, 3})
        False

        ```
        """
        return self.isdisjoint(other)

difference(other)

Return the difference of the Set with another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to subtract.

required

Returns:

Name Type Description
Self Self

A new Set containing the difference of the two sets.

Example:

>>> import pyochain as pc
>>> pc.Set({1, 2, 2}).difference([2, 3])
Set(1,)

Source code in src/pyochain/traits/_iterable.py
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
def difference(self, other: AbstractSet[T]) -> Self:
    """Return the difference of the `Set` with another set.

    Args:
        other (AbstractSet[T]): The other set to subtract.

    Returns:
        Self: A new `Set` containing the difference of the two sets.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({1, 2, 2}).difference([2, 3])
    Set(1,)

    ```
    """
    return self.__class__(self - other)

eq(other)

Check if the Set is equal to another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to compare with.

required

Returns:

Name Type Description
bool bool

True if the Set is equal to the other set, False otherwise.

Example:

>>> import pyochain as pc
>>> pc.Set({1, 2}).eq({2, 1})
True
>>> pc.Set({1, 2}).eq({1, 2, 3})
False

Source code in src/pyochain/traits/_iterable.py
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
def eq(self, other: AbstractSet[T]) -> bool:
    """Check if the `Set` is equal to another set.

    Args:
        other (AbstractSet[T]): The other set to compare with.

    Returns:
        bool: True if the `Set` is equal to the other set, False otherwise.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({1, 2}).eq({2, 1})
    True
    >>> pc.Set({1, 2}).eq({1, 2, 3})
    False

    ```
    """
    return self == other

intersection(other)

Return the intersection of the Set with another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to intersect with.

required

Returns:

Name Type Description
Self Self

A new Set containing the intersection of the two sets.

Example:

>>> import pyochain as pc
>>> pc.Set({1, 2, 2}).intersection({2, 3})
Set(2,)

Source code in src/pyochain/traits/_iterable.py
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
def intersection(self, other: AbstractSet[T]) -> Self:
    """Return the intersection of the `Set` with another set.

    Args:
        other (AbstractSet[T]): The other set to intersect with.

    Returns:
        Self: A new `Set` containing the intersection of the two sets.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({1, 2, 2}).intersection({2, 3})
    Set(2,)

    ```
    """
    return self.__class__(self & other)

is_disjoint(other)

Check if the Set has no elements in common with another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to compare with.

required

Returns:

Name Type Description
bool bool

True if the Set and the other set have no elements in common, False otherwise.

Example:

>>> import pyochain as pc
>>> pc.Set({1, 2}).is_disjoint({3, 4})
True
>>> pc.Set({1, 2}).is_disjoint({2, 3})
False

Source code in src/pyochain/traits/_iterable.py
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
def is_disjoint(self, other: AbstractSet[T]) -> bool:
    """Check if the `Set` has no elements in common with another set.

    Args:
        other (AbstractSet[T]): The other set to compare with.

    Returns:
        bool: True if the `Set` and the other set have no elements in common, False otherwise.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({1, 2}).is_disjoint({3, 4})
    True
    >>> pc.Set({1, 2}).is_disjoint({2, 3})
    False

    ```
    """
    return self.isdisjoint(other)

is_subset(other)

Check if the Set is a subset of another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to compare with.

required

Returns:

Name Type Description
bool bool

True if the Set is a subset of the other set, False otherwise.

Example:

>>> import pyochain as pc
>>> pc.Set({1, 2}).is_subset({1, 2, 3})
True
>>> pc.Set({1, 4}).is_subset({1, 2, 3})
False

Source code in src/pyochain/traits/_iterable.py
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
def is_subset(self, other: AbstractSet[T]) -> bool:
    """Check if the `Set` is a subset of another set.

    Args:
        other (AbstractSet[T]): The other set to compare with.

    Returns:
        bool: True if the `Set` is a subset of the other set, False otherwise.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({1, 2}).is_subset({1, 2, 3})
    True
    >>> pc.Set({1, 4}).is_subset({1, 2, 3})
    False

    ```
    """
    return self <= other

is_subset_strict(other)

Check if the Set is a proper subset of another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to compare with.

required

Returns:

Name Type Description
bool bool

True if the Set is a proper subset of the other set, False otherwise.

Example:

>>> import pyochain as pc
>>> pc.Set({1, 2}).is_subset_strict({1, 2, 3})
True
>>> pc.Set({1, 2}).is_subset_strict({1, 2})
False

Source code in src/pyochain/traits/_iterable.py
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
def is_subset_strict(self, other: AbstractSet[T]) -> bool:
    """Check if the `Set` is a proper subset of another set.

    Args:
        other (AbstractSet[T]): The other set to compare with.

    Returns:
        bool: `True` if the `Set` is a proper subset of the other set, `False` otherwise.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({1, 2}).is_subset_strict({1, 2, 3})
    True
    >>> pc.Set({1, 2}).is_subset_strict({1, 2})
    False

    ```
    """
    return self < other

is_superset(other)

Check if the Set is a superset of another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to compare with.

required

Returns:

Name Type Description
bool bool

True if the Set is a superset of the other set, False otherwise.

Example:

>>> import pyochain as pc
>>> pc.Set({1, 2, 3}).is_superset({1, 2})
True
>>> pc.Set({1, 2}).is_superset({1, 2, 3})
False

Source code in src/pyochain/traits/_iterable.py
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
def is_superset(self, other: AbstractSet[T]) -> bool:
    """Check if the `Set` is a superset of another set.

    Args:
        other (AbstractSet[T]): The other set to compare with.

    Returns:
        bool: True if the `Set` is a superset of the other set, False otherwise.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({1, 2, 3}).is_superset({1, 2})
    True
    >>> pc.Set({1, 2}).is_superset({1, 2, 3})
    False

    ```
    """
    return self >= other

r_difference(other)

Return the reverse difference of the Set with another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to subtract from.

required

Returns:

Name Type Description
Self Self

A new Set containing the reverse difference of the two sets.

Example:

>>> import pyochain as pc
>>> pc.Set({2, 3}).r_difference({1, 2})
Set(1,)

Source code in src/pyochain/traits/_iterable.py
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
def r_difference(self, other: AbstractSet[T]) -> Self:
    """Return the reverse difference of the `Set` with another set.

    Args:
        other (AbstractSet[T]): The other set to subtract from.

    Returns:
        Self: A new `Set` containing the reverse difference of the two sets.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({2, 3}).r_difference({1, 2})
    Set(1,)

    ```
    """
    return self.__class__(other - self)

r_intersection(other)

Return the intersection of another set with the Set.

Parameters:

Name Type Description Default
other Set[T]

The other set to intersect with.

required

Returns:

Name Type Description
Self Self

A new Set containing the intersection of the two sets.

Example:

>>> import pyochain as pc
>>> pc.Set({2, 3}).r_intersection({1, 2})
Set(2,)

Source code in src/pyochain/traits/_iterable.py
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
def r_intersection(self, other: AbstractSet[T]) -> Self:
    """Return the intersection of another set with the `Set`.

    Args:
        other (AbstractSet[T]): The other set to intersect with.

    Returns:
        Self: A new `Set` containing the intersection of the two sets.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({2, 3}).r_intersection({1, 2})
    Set(2,)

    ```
    """
    return self.__class__(other & self)

r_symmetric_difference(other)

Return the symmetric difference of the Set with another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to symmetric difference with.

required

Returns:

Name Type Description
Self Self

A new Set containing the symmetric difference of the two sets.

Example:

>>> import pyochain as pc
>>> pc.Set({2, 3}).r_symmetric_difference({1, 2}).iter().sort()
Vec(1, 3)

Source code in src/pyochain/traits/_iterable.py
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
def r_symmetric_difference(self, other: AbstractSet[T]) -> Self:
    """Return the symmetric difference of the `Set` with another set.

    Args:
        other (AbstractSet[T]): The other set to symmetric difference with.

    Returns:
        Self: A new `Set` containing the symmetric difference of the two sets.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({2, 3}).r_symmetric_difference({1, 2}).iter().sort()
    Vec(1, 3)

    ```
    """
    return self.__class__(other ^ self)

r_union(other)

Return the union of another set with the Set.

Parameters:

Name Type Description Default
other Set[T]

The other set to unite with.

required

Returns:

Name Type Description
Self Self

A new Set containing the union of the two sets.

Example:

>>> import pyochain as pc
>>> pc.Set({2, 3}).r_union({1, 2}).iter().sort()
Vec(1, 2, 3)

Source code in src/pyochain/traits/_iterable.py
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
def r_union(self, other: AbstractSet[T]) -> Self:
    """Return the union of another set with the `Set`.

    Args:
        other (AbstractSet[T]): The other set to unite with.

    Returns:
        Self: A new `Set` containing the union of the two sets.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({2, 3}).r_union({1, 2}).iter().sort()
    Vec(1, 2, 3)

    ```
    """
    return self.__class__(other | self)

symmetric_difference(other)

Return the symmetric difference of the Set with another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to symmetric difference with.

required

Returns:

Name Type Description
Self Self

A new Set containing the symmetric difference of the two sets.

>>> import pyochain as pc
>>> pc.Set({1, 2, 2}).symmetric_difference([2, 3]).iter().sort()
Vec(1, 3)
>>> pc.Set({1, 2, 3}).symmetric_difference([3, 4, 5]).iter().sort()
Vec(1, 2, 4, 5)
Source code in src/pyochain/traits/_iterable.py
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
def symmetric_difference(self, other: AbstractSet[T]) -> Self:
    """Return the symmetric difference of the `Set` with another set.

    Args:
        other (AbstractSet[T]): The other set to symmetric difference with.

    Returns:
        Self: A new `Set` containing the symmetric difference of the two sets.
    ```python
    >>> import pyochain as pc
    >>> pc.Set({1, 2, 2}).symmetric_difference([2, 3]).iter().sort()
    Vec(1, 3)
    >>> pc.Set({1, 2, 3}).symmetric_difference([3, 4, 5]).iter().sort()
    Vec(1, 2, 4, 5)

    ```
    """
    return self.__class__(self ^ other)

union(other)

Return the union of the Set with another set.

Parameters:

Name Type Description Default
other Set[T]

The other set to unite with.

required

Returns:

Name Type Description
Self Self

A new Set containing the union of the two sets.

Example:

>>> import pyochain as pc
>>> pc.Set({1, 2, 2}).union([2, 3]).union([4]).iter().sort()
Vec(1, 2, 3, 4)

Source code in src/pyochain/traits/_iterable.py
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
def union(self, other: AbstractSet[T]) -> Self:
    """Return the union of the `Set` with another set.

    Args:
        other (AbstractSet[T]): The other set to unite with.

    Returns:
        Self: A new `Set` containing the union of the two sets.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Set({1, 2, 2}).union([2, 3]).union([4]).iter().sort()
    Vec(1, 2, 3, 4)

    ```
    """
    return self.__class__(self | other)