Skip to main content

eset

Extended sets - support for set complement

Overview

An eset works like a normal python set except that you can invert it to generate its complement. For example, let's say you have the following:

>>> from esets import eset
>>> s = eset(['hello', 'there'])
>>> s_invert = ~s
>>> s_invert
~eset(['hello', 'there'])

In this example, s_invert contains everything except 'hello' and 'there'.

Logic

All the logic operations you'd expect from sets are available in esets, including intersection, union, difference, and symmetric difference. Use the &, |, -, and ^ operators respectively.

Similarly, conditional expressions are available to determine subset relationships. If A <= B, that A is a subset of B.

The named forms of these operations are there too -- union, intersection, difference, symmetric_difference, isdisjoint, issubset, issuperset -- along with their in-place counterparts (update, intersection_update, difference_update, symmetric_difference_update) and the usual add, discard, remove, pop, clear and copy. As with the builtin set, the operators accept only sets, dicts and esets, while the named methods raise TypeError on anything else.

Membership is what mutates

Mutation is defined in terms of membership, not storage, so it reads the same either side of an inversion: after s.add(x), x in s is always true, and after s.discard(x) it is always false -- even when s is a complement, where the item is dropped from (or added to) the excluded items to make that hold.

>>> s = ~eset(['hello'])
>>> s.add('hello')
~eset()
>>> 'hello' in s
True

Iteration and length

A complement eset has infinitely many members, so it can neither be iterated nor measured with len(); both raise TypeError. Invert it to walk the finite set of items it excludes, and use abs() for its cardinality.

>>> list(~~eset(['hello', 'there']))
['hello', 'there']

Frozen esets

frozen() returns an immutable eset, which -- unlike a plain one -- is hashable and so can be a dict key or live inside another set. A frozen eset hashes the same as the frozenset it compares equal to.

>>> from esets import frozen
>>> frozen(['hello']) in {frozen(['hello'])}
True

Anything that would mutate one -- add, discard, clear, update and the other *_update methods -- raises TypeError. The augmented operators are the exception, and behave as they do for frozenset: f &= x never mutated anything to begin with, it rebound the name, so it yields a new frozen eset and leaves the original alone.

>>> f = frozen(['hello', 'there'])
>>> f &= {'hello'}
>>> f
frozen(eset(['hello']))

Use freeze() and thaw() to flip an existing eset between the two states.

Infinities

You'll note there's an inf.py module that implement א0 and א1 infinities, that is, countable and uncountable infinities. This was implemented so that cardinality calculations would be correct for complement sets: discussed below.

>>> from esets import inf
>>> inf.countable, inf.uncountable
(א0, א1)
>>> inf.countable < inf.uncountable
True

Indeterminate forms such as א0 - א0 yield inf.nan, which propagates through any further arithmetic and compares False against everything ordered -- but, unlike float('nan'), equals itself so results can be checked against it.

>>> inf.countable - inf.countable
nan
>>> inf.countable - inf.countable == inf.nan
True

Cardinality

The abs expression (also cardinality method) will return a countably infinite scalar if in complement mode; otherwise it returns the count of items in set.

>>> from esets import eset, inf
>>> s = eset(['hello', 'there'])
>>> abs(s)
2
>>> abs(~s)
א0

Download files

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

Source Distribution

esets-0.3.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

esets-0.3.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file esets-0.3.0.tar.gz.

File metadata

  • Download URL: esets-0.3.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for esets-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d4d8463f93bfccdaf37f5160c947f399124e84ecf870459ffeca4d2b0884c39b
MD5 fff1f8a0810e41000d05c14cc0e8f23a
BLAKE2b-256 3f82c36a3d269459f22dab11bb509e5dc5b7ad428a58968974a5b69392e303d8

See more details on using hashes here.

File details

Details for the file esets-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: esets-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for esets-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b82f72940cfd7e915eaee037a3fdf755a361476d4fe1e64691b9711b5fc8b51c
MD5 f35dd05ace050f29f4b023ff19f14b04
BLAKE2b-256 bd2c5b288aa6809815cafc6ef80f7f2e35002f44d4fa425581e3890546d6f960

See more details on using hashes here.

Release history Release notifications | RSS feed

0.4.0

2 files

This release

0.3.0 This release

2 files

0.2.0

1 file

0.1.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page