Skip to main content

Ordered subsets over a predefined domain

Project description

Latest PyPI Version License Supported Python Versions Monthly downloads

Build Codecov Readthedocs stable Readthedocs latest

This package provides a memory-efficient pure-python immutable ordered set data type for working with large numbers of subsets from a predetermined pool of objects.

Given a name and a tuple of hashable objects, the bitset()-function returns a custom integer subclass for creating ordered subsets from the object pool. Each instance is a Python integer where the presence/absence of the nth pool item is its nth bit being set/unset (a.k.a. bit strings, powers of two, rank in colexicographical order).

Being just a regular (arbitrary precision) integer with some convenience methods for translating between unique object collections and bit patterns allows to perform certain tasks in a more natural way, e.g. sorting a collection of sets lexicographically, or enumerating possible combinations in an ordered fashion.

Installation

This package runs under Python 3.10+ and has no required dependencies, use pip to install:

$ pip install bitsets

Quickstart

Create a class for sets taken from your pool of objects:

>>> from bitsets import bitset

>>> PYTHONS = ('Chapman', 'Cleese', 'Gilliam', 'Idle', 'Jones', 'Palin')

>>> Pythons = bitset('Pythons', PYTHONS)

Access its maximal and minimal instances. Retrieve instance members in definition order:

>>> Pythons.supremum
Pythons(['Chapman', 'Cleese', 'Gilliam', 'Idle', 'Jones', 'Palin'])

>>> Pythons.infimum
Pythons()

>>> Pythons(['Idle', 'Gilliam', 'Idle', 'Idle']).members()
('Gilliam', 'Idle')

Translate to/from bit string, boolean sequence, and int:

>>> Pythons(['Chapman', 'Gilliam']).bits()
'101000'

>>> Pythons.frombits('101000')
Pythons(['Chapman', 'Gilliam'])

>>> Pythons(['Chapman', 'Gilliam']).bools()
(True, False, True, False, False, False)

>>> Pythons.frombools([True, None, 1, False, 0])
Pythons(['Chapman', 'Gilliam'])

>>> int(Pythons(['Chapman', 'Gilliam']))
5

>>> Pythons.fromint(5)
Pythons(['Chapman', 'Gilliam'])

Set operation and comparison methods (cf. build-in frozenset):

>>> Pythons(['Jones', 'Cleese', 'Idle']).intersection(Pythons(['Idle']))
Pythons(['Idle'])

>>> Pythons(['Idle']).union(Pythons(['Jones', 'Cleese']))
Pythons(['Cleese', 'Idle', 'Jones'])

>>> Pythons.supremum.difference(Pythons(['Chapman', 'Cleese']))
Pythons(['Gilliam', 'Idle', 'Jones', 'Palin'])

>>> Pythons(['Palin', 'Jones']).symmetric_difference(Pythons(['Cleese', 'Jones']))
Pythons(['Cleese', 'Palin'])

>>> Pythons(['Gilliam']).issubset(Pythons(['Cleese', 'Palin']))
False

>>> Pythons(['Cleese', 'Palin']).issuperset(Pythons())
True

Further reading

See also

  • bitarray – efficient boolean array implemented as C extension

  • bitstring – pure-Python bit string based on bytearray

  • BitVector – pure-Python bit array based on unsigned short array

  • Bitsets – Cython interface to fast bitsets in Sage

  • bitfield – Cython positive integer sets

  • intbitset – integer bit sets as C extension

  • gmpy2 – fast arbitrary precision integer arithmetic

License

Bitsets is distributed under the MIT license.

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

bitsets-0.9.1.tar.gz (106.8 kB view details)

Uploaded Source

Built Distribution

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

bitsets-0.9.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file bitsets-0.9.1.tar.gz.

File metadata

  • Download URL: bitsets-0.9.1.tar.gz
  • Upload date:
  • Size: 106.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for bitsets-0.9.1.tar.gz
Algorithm Hash digest
SHA256 f4268875468ed3ba8657b1e09a984c74c88622dd7d29fa1d8d0d474b7e2daff9
MD5 fbd02397c2fc18680432c40f10ea2d77
BLAKE2b-256 45d39f6b7e955c23655c2b9051f1a221c7fa5caad7a695bcecc90cbf6b1fe8db

See more details on using hashes here.

File details

Details for the file bitsets-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: bitsets-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for bitsets-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b80c6e2d377fbf835f9d053e5546f10aa0f1b4bbd0e93e09329247dd8adc1922
MD5 8149e9cf74590bcd5b4e51f0cb949052
BLAKE2b-256 4c3fbf1ff4c8394fcb7a7a55e216af0394cc3a8970c5db5ebb0d351d06333c6c

See more details on using hashes here.

Supported by

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