Package: infinite-sets
Infinite SetsThis package gives a pythonic way to deal with the universal set and set complements. Here, the universal set represents
a set that containseverything, whilst thecomplementof a set A is the set of everything that is not in A. This
allows the expression of concepts such aseverythingorall but these thingsin a consistent way.The universal set may be obtained using theeverything()function. Everything is in the universal set:frominfinite_setsimporteverything4ineverything()# true"abc"ineverything()# trueThe complement of a set is can be created using thecomplement()function:frominfinite_setsimportcomplementA={1,4}B=complement(A)1inA# true1inB# false7inA# false7inB# trueBoth the universal set and the set complement of a set A areinfinite 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 afinite 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.ExampleAn example of a use for infinite sets is a function that takes a set of objects, where only certain values would be
valid:fromtypingimportSetVALID_NAMES={"chapman","cleese","gilliam","idle","jones","palin"}defmy_function(names:Set[str]):names=names&VALID_NAMES# filter out invalid namesfornameinnames:...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'}))TypingFor typing, there is anInfiniteSet[TItem]protocol, which defines an infinite set similarly to
theAbstractSet[TItem]fromcollections.abc, but without the__iter__or__len__methods. Therefore, the
universal set, set complements and all python sets are examples of anInfiniteSet.If you have a method which takes an argument annotated withSet[TItem], but you only check for membership of the set
and never iterate through it, you can change this annotation toInfiniteSet[TItem]to indicate that you could pass an
infinite set such aseverything()to your method.Implementation DetailsThe universal set is a singleton - theeverything()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.ChangelogAll notable changes to this project will be documented in this file.The format is based onKeep a Changelog,
and this project adheres toSemantic Versioning.[1.0.2] - 2021-03-30AddedAdd license_file entry to setup.cfg.1.0.1- 2021-03-30ChangedSwitch to package format to allow py.typed and hence typing.1.0.0- 2021-03-30AddedInitial release of code.everything()method to return the universal set.complement(set)method to get the complement of a set.