Skip to main content

Code to express the universal set and set complements in a pythonic manner

Project description

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.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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

infinite-sets-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

infinite_sets-1.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file infinite-sets-1.0.0.tar.gz.

File metadata

  • Download URL: infinite-sets-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.4

File hashes

Hashes for infinite-sets-1.0.0.tar.gz
Algorithm Hash digest
SHA256 24dd22eb4cbecb29a21d95f0e1ee0dda6a6675f8add97b82270ea5a7337ec245
MD5 8fb210ac97d716570f9dc77e8584bd3b
BLAKE2b-256 7edc1ba2ff964b9b2c1516dd27ad32982b1d905935376b6566f3dde16394d194

See more details on using hashes here.

File details

Details for the file infinite_sets-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: infinite_sets-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.4

File hashes

Hashes for infinite_sets-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e28173ae47d05e2b272d9e0fdf248a117a93a6d9e637416030a1b29dc80e7998
MD5 828188607f9baa364310de8f7d184e5b
BLAKE2b-256 d5bcd596dea4a0f216b70ae9237a138afa173eff8bdbfb6db83f608e8514b221

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page