Skip to main content

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.

Release files for bitsets 0.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bitsets 0.9.1
File Size Uploaded
bitsets-0.9.1.tar.gz 106.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bitsets 0.9.1
File Interpreter ABI Platform
bitsets-0.9.1-py3-none-any.whl Python 3 none any Details

Total release size: 120.7 kB

Release files / bitsets-0.9.1.tar.gz

Download URL bitsets-0.9.1.tar.gz
Size 106.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f4268875468ed3ba8657b1e09a984c74c88622dd7d29fa1d8d0d474b7e2daff9
BLAKE2b-256 checksum
How to use checksums
45d39f6b7e955c23655c2b9051f1a221c7fa5caad7a695bcecc90cbf6b1fe8db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / bitsets-0.9.1-py3-none-any.whl

Download URL bitsets-0.9.1-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b80c6e2d377fbf835f9d053e5546f10aa0f1b4bbd0e93e09329247dd8adc1922
BLAKE2b-256 checksum
How to use checksums
4c3fbf1ff4c8394fcb7a7a55e216af0394cc3a8970c5db5ebb0d351d06333c6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release history Release notifications | RSS feed

This release

0.9.1 This release

2 release files

0.9

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8

2 release files

0.7.16

2 release files

0.7.14

2 release files

0.7.13

2 release files

0.7.12

2 release files

0.7.11

2 release files

0.7.10

2 release files

0.7.9

2 release files

0.7.8

2 release files

0.7.7

2 release files

0.7.6

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

1 release file

0.7

1 release file

0.6.1

1 release file

0.6

1 release file

0.5.1

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1

1 release 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