PyoSet
Bases: PyoCollection[T], Set[T], ABC
flowchart TD
pyochain.abc._set.PyoSet[PyoSet]
pyochain.abc._collection.PyoCollection[PyoCollection]
pyochain.abc._iterable.PyoIterable[PyoIterable]
pyochain.rs.Pipeable[Pipeable]
pyochain.rs.Into[Into]
pyochain.rs.Inspect[Inspect]
pyochain.rs.Checkable[Checkable]
pyochain.abc._collection.PyoContainer[PyoContainer]
pyochain.abc._collection.PyoSized[PyoSized]
pyochain.abc._collection.PyoCollection --> pyochain.abc._set.PyoSet
pyochain.abc._iterable.PyoIterable --> pyochain.abc._collection.PyoCollection
pyochain.rs.Pipeable --> pyochain.abc._iterable.PyoIterable
pyochain.rs.Into --> pyochain.rs.Pipeable
pyochain.rs.Inspect --> pyochain.rs.Pipeable
pyochain.rs.Checkable --> pyochain.abc._iterable.PyoIterable
pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
click pyochain.abc._set.PyoSet href "" "pyochain.abc._set.PyoSet"
click pyochain.abc._collection.PyoCollection href "" "pyochain.abc._collection.PyoCollection"
click pyochain.abc._iterable.PyoIterable href "" "pyochain.abc._iterable.PyoIterable"
click pyochain.rs.Pipeable href "" "pyochain.rs.Pipeable"
click pyochain.rs.Into href "" "pyochain.rs.Into"
click pyochain.rs.Inspect href "" "pyochain.rs.Inspect"
click pyochain.rs.Checkable href "" "pyochain.rs.Checkable"
click pyochain.abc._collection.PyoContainer href "" "pyochain.abc._collection.PyoContainer"
click pyochain.abc._collection.PyoSized href "" "pyochain.abc._collection.PyoSized"
Extends PyoCollection[T] and collections.abc.Set[T].
Is the shared ABC for concrete set-like collections: Set and FrozenSet.
Any concrete subclass must implement the required Set dunder methods:
__contains____iter____len__
The following informations comes directly from the official Python documentation regarding Set ABCs, and also applies for PyoSet and its subclasses:
Since some set operations create new sets, the default mixin methods need a way to create new instances from an iterable.
The class constructor is assumed to have a signature in the form ClassName(iterable).
That assumption is factored-out to an internal classmethod called _from_iterable() which calls cls(iterable) to produce a new set.
If the Set mixin is being used in a class with a different constructor signature,
you will need to override _from_iterable() with a classmethod or regular method that can construct new instances from an iterable argument.
See Also
The official Python documentation for more details:
https://docs.python.org/3/library/collections.abc.html#examples-and-recipes
Example
>>> from pyochain.abc import PyoSet
>>> class MySet(PyoSet[int]):
... def __init__(self, data: set[int]):
... self._data = data
...
... def __contains__(self, item: int) -> bool:
... return item in self._data
...
... def __iter__(self) -> Iterator[int]:
... return iter(self._data)
...
... def __len__(self) -> int:
... return len(self._data)
>>>
>>> my_set = MySet({10, 20, 30})
>>> my_set.is_subset({10, 20, 30, 40})
True
>>> my_set.is_superset({10})
True
>>> my_set.iter().sort()
Vec(10, 20, 30)
Source code in src/pyochain/abc/_set.py
10 11 12 13 14 15 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
difference(other)
Create a new set with elements in this set but not in other.
The result contains every element that is in this set EXCEPT those that are also present in other.
This operation is NOT commutative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set whose elements should be excluded. |
required |
Returns:
| Type | Description |
|---|---|
Set[T]
|
AbstractSet[T]: A new set containing elements unique to this set. |
Example
>>> from pyochain import Set, Dict
>>> Set((1, 2)).difference({2, 3})
Set(1,)
>>> Set((1, 2)).difference({3, 4}).iter().sort()
Vec(1, 2)
>>> dct = Dict.from_ref({"a": 1, "b": 2, "c": 3})
>>> from_keys = dct.keys().difference({"b", "c", "d"}).iter().sort()
>>> from_keys
Vec('a')
>>> from_items = (
... dct.items().difference({("b", 2), ("c", 3), ("d", 4)}).iter().sort()
... )
>>> from_items
Vec(('a', 1))
Source code in src/pyochain/abc/_set.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
eq(other)
Test whether this set contains exactly the same elements as other.
Sets are equal if they have the same number of elements and every element in one is present in the other.
This is an explicit method; you can also use the == operator directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set to compare with. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Example
>>> from pyochain import Set
>>> Set((1, 2)).eq({2, 1}) # Same elements, different order
True
>>> Set((1, 2)).eq({1, 2, 3}) # Different number of elements
False
>>> Set((1, 2)).eq({1, 2}) # Identical
True
Source code in src/pyochain/abc/_set.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
intersection(other)
Create a new set containing only elements present in both sets.
If the sets have no common elements, the result is empty.
This operation is commutative: A.intersection(B) == B.intersection(A).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set to intersect with. |
required |
Returns:
| Type | Description |
|---|---|
Set[T]
|
AbstractSet[T]: A new |
Example
>>> from pyochain import Set, Dict
>>> from_set = Set((1, 2))
>>> from_set.intersection({2, 3})
Set(2,)
>>> from_set.intersection({3, 4})
Set()
>>> dct = Dict.from_ref({"a": 1, "b": 2, "c": 3})
>>> from_keys = dct.keys().intersection({"b", "c", "d"}).iter().sort()
>>> from_keys
Vec('b', 'c')
>>> from_items = (
... dct.items()
... .intersection({("b", 2), ("c", 3), ("d", 4)})
... .iter()
... .sort()
... )
>>> from_items
Vec(('b', 2), ('c', 3))
Source code in src/pyochain/abc/_set.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
is_disjoint(other)
Test whether this set and other have no elements in common.
Returns True if the intersection of the two sets is empty.
This is the opposite of having any overlap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set to compare with. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Example
>>> from pyochain import Set
>>> Set((1, 2)).is_disjoint((3, 4)) # No overlap
True
>>> Set((1, 2)).is_disjoint((2, 3)) # Share element 2
False
>>> Set((1, 2)).is_disjoint((1, 2)) # Identical sets
False
Source code in src/pyochain/abc/_set.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
is_subset(other)
Test whether all elements of this set are in other (including equality).
Returns True if every element in this set is also present in other.
This includes the case where both sets are identical.
Use is_subset_strict() to exclude the equality case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set to check containment against. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Example
>>> from pyochain import Set
>>> Set((1, 2)).is_subset({1, 2, 3}) # All elements present
True
>>> Set((1, 2)).is_subset({1, 2}) # Also True: they're equal
True
>>> Set((1, 4)).is_subset({1, 2, 3}) # 4 is not in the other set
False
Source code in src/pyochain/abc/_set.py
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 | |
is_subset_strict(other)
Test whether all elements of this set are in other, excluding equality.
Returns True if every element in this set is also present in other, AND other contains at least one element not in this set.
This is a proper (or strict) subset relation.
Use is_subset() if you want to accept equal sets as well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set to check strict containment against. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Example
>>> from pyochain import Set
>>> Set((1, 2)).is_subset_strict({1, 2, 3}) # Proper subset
True
>>> Set((1, 2)).is_subset_strict({1, 2}) # Equal, not proper
False
>>> Set((1, 4)).is_subset_strict({1, 2, 3}) # 4 not contained
False
Source code in src/pyochain/abc/_set.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
is_superset(other)
Test whether all elements of other are in this set (including equality).
Returns True if this set contains every element from other.
This is the inverse of PyoSet::is_subset ->
- if A is a subset of B, then B is a superset of A.
Use PyoSet::is_superset_strict to exclude equality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set to check containment for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Example
>>> from pyochain import Set
>>> Set((1, 2, 3)).is_superset({1, 2}) # Contains all
True
>>> Set((1, 2)).is_superset({1, 2}) # Also True: they're equal
True
>>> Set((1, 2)).is_superset({1, 2, 3}) # Missing element 3
False
Source code in src/pyochain/abc/_set.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
is_superset_strict(other)
Test whether all elements of other are in this set, excluding equality.
Returns True if this set contains every element from other, AND this set has at least one element not in other.
This is a proper (or strict) superset relation.
Use PyoSet::is_superset if you want to accept equal sets as well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set to check strict containment for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Example
>>> from pyochain import Set
>>> Set((1, 2, 3)).is_superset_strict({1, 2}) # Proper superset
True
>>> Set((1, 2)).is_superset_strict({1, 2}) # Equal, not proper
False
>>> Set((1, 2)).is_superset_strict({1, 2, 3}) # Missing element 3
False
Source code in src/pyochain/abc/_set.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
symmetric_difference(other)
Create a new set with elements in either set but not in both.
The result contains elements that are in this set XOR other—i.e., elements present in one set but not in both.
This is the opposite of [Set::intersection][pyochain._set.Set.intersection].
This operation is commutative: A.symmetric_difference(B) == B.symmetric_difference(A).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set to compute symmetric difference with. |
required |
Returns:
| Type | Description |
|---|---|
Set[T]
|
AbstractSet[T]: A new set containing elements unique to each set. |
Example
>>> from pyochain import Set, Dict
>>> Set((1, 2)).symmetric_difference({2, 3}).iter().sort()
Vec(1, 3)
>>> Set((1, 2, 3)).symmetric_difference({3, 4, 5}).iter().sort()
Vec(1, 2, 4, 5)
>>> dct = Dict.from_ref({"a": 1, "b": 2, "c": 3})
>>> from_keys = (
... dct.keys().symmetric_difference({"b", "c", "d"}).iter().sort()
... )
>>> from_keys
Vec('a', 'd')
>>> from_items = (
... dct.items()
... .symmetric_difference({("b", 2), ("c", 3), ("d", 4)})
... .iter()
... .sort()
... )
>>> from_items
Vec(('a', 1), ('d', 4))
Source code in src/pyochain/abc/_set.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
union(other)
Create a new set containing all unique elements from both sets.
This operation is commutative: A.union(B) == B.union(A).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Set[T]
|
The set to combine with. |
required |
Returns:
| Type | Description |
|---|---|
Set[T]
|
AbstractSet[T]: A new set containing all elements from self and other. |
Example
>>> from pyochain import Set, Dict
>>> Set((1, 2)).union({2, 3}).union({4}).iter().sort()
Vec(1, 2, 3, 4)
>>> dct = Dict.from_ref({"a": 1, "b": 2, "c": 3})
>>> from_keys = dct.keys().union({"b", "c", "d"}).iter().sort()
>>> from_keys
Vec('a', 'b', 'c', 'd')
>>> from_items = (
... dct.items().union({("b", 2), ("c", 3), ("d", 4)}).iter().sort()
... )
>>> from_items
Vec(('a', 1), ('b', 2), ('c', 3), ('d', 4))
Source code in src/pyochain/abc/_set.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |