Infinite Sets
This package gives a pythonic way to deal with the universal set and set complements. Here, the universal set represents a set that contains everything, whilst the complement of a set A is the set of everything that is not in A. This allows the expression of concepts such as everything or all but these things in a consistent way.
The universal set may be obtained using the everything() function. Everything is in the universal set:
from infinite_sets import everything
4 in everything() # true
"abc" in everything() # true
The complement of a set is can be created using the complement() function:
from infinite_sets import complement
A = {1, 4}
B = complement(A)
1 in A # true
1 in B # false
7 in A # false
7 in B # true
Both the universal set and the set complement of a set A are infinite sets. Whilst you can query if a given object is in them, you can't iterate through them in the same way you could a python set (which represents a finite set).
These infinite sets support all the set operations that are supported by python sets, namely equality (==), subset (<=), strict subset (<), superset (>=), strict superset (>), intersection (&), union (|), set difference (-) and symmetric difference (^).
They however do not support in place modification, such as (&=). This is because this could change the type of the object.
Example
An example of a use for infinite sets is a function that takes a set of objects, where only certain values would be valid:
from typing import Set
VALID_NAMES = {
"chapman",
"cleese",
"gilliam",
"idle",
"jones",
"palin"
}
def my_function(names: Set[str]):
names = names & VALID_NAMES # filter out invalid names
for name in names:
...
To call this function on all the valid names, you need to have access to the set of names ahead of time and import it from wherever it is:
my_function(VALID_NAMES)
However, using infinite sets we could change our method to use infinite sets. By intersecting an infinite set with our list of predefined possible names, we get the set of names the user intended to give.
The user can indicate they mean all valid names:
my_function(everything())
Or that they want all names except 'cleese':
my_function(complement({'cleese'}))
Typing
For typing, there is an InfiniteSet[TItem] protocol, which defines an infinite set similarly to
the AbstractSet[TItem] from collections.abc, but without the __iter__ or __len__ methods. Therefore, the
universal set, set complements and all python sets are examples of an InfiniteSet.
If you have a method which takes an argument annotated with Set[TItem], but you only check for membership of the set
and never iterate through it, you can change this annotation to InfiniteSet[TItem] to indicate that you could pass an
infinite set such as everything() to your method.
Implementation Details
The universal set is a singleton - the everything() command returns the same object every time. Its __contains__
function returns true unconditionally.
The set complement is an object that internally stores the set of which it is a complement. Its __contains__ function
returns true if the given item is not in its internal set.
Changelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
[1.0.2] - 2021-03-30
Added
- Add license_file entry to setup.cfg.
1.0.1 - 2021-03-30
Changed
- Switch to package format to allow py.typed and hence typing.
1.0.0 - 2021-03-30
Added
- Initial release of code.
everything()method to return the universal set.complement(set)method to get the complement of a set.
Metadata
Release files for infinite-sets 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| infinite-sets-1.0.2.tar.gz | 5.9 kB | Without provenance | Details for infinite-sets-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| infinite_sets-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for infinite_sets-1.0.2-py3-none-any.whl |
Total release size: 13.2 kB
Release files / infinite-sets-1.0.2.tar.gz
| Download URL | infinite-sets-1.0.2.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0b0120292cdabd229dd8cefc4d285b53109bc26a6fefa67dae81655ee2c6275a
|
|
BLAKE2b-256 checksum How to use checksums |
4497bc48ed71170bc18895fadc5e38948f00b027750af2dcd9551a238b127472
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2
|
Release files / infinite_sets-1.0.2-py3-none-any.whl
| Download URL | infinite_sets-1.0.2-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6a062eeaecd642d5451568156d9590f5a67d3c3f61579497130a0de0b627add8
|
|
BLAKE2b-256 checksum How to use checksums |
b44a033f2a4ef3a77d7984cdfa1b206cdc9019e8d10532031a137388eb0bd302
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2
|
Security for infinite-sets 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.