Skip to main content

Lightweight Bloom filter data structure derived from the built-in bytearray type.

Project description

Lightweight Bloom filter data structure derived from the built-in bytearray type.

PyPI version and link. Read the Docs documentation status. GitHub Actions status. Coveralls test coverage summary.

Purpose

This library provides a simple and lightweight data structure for representing Bloom filters that is derived from the built-in bytearray type. The data structure has methods for the insertion, membership, union, and subset operations. In addition, methods for estimating capacity and for converting to and from Base64 strings are available.

Installation and Usage

This library is available as a package on PyPI:

python -m pip install blooms

The library can be imported in the usual ways:

import blooms
from blooms import blooms

Examples

This library makes it possible to concisely create, populate, and query simple Bloom filters. The example below constructs a Bloom filter that is 32 bits (i.e., four bytes) in size:

>>> from blooms import blooms
>>> b = blooms(4)

A bytes-like object can be inserted into an instance using the insertion operator @=. It is the responsibility of the user of the library to hash and truncate the bytes-like object being inserted. Only the bytes that remain after truncation contribute to the membership of the bytes-like object within the Bloom filter:

>>> from hashlib import sha256
>>> x = 'abc' # Value to insert.
>>> h = sha256(x.encode()).digest() # Hash of value.
>>> t = h[:2] # Truncated hash.
>>> b @= t # Insert the value into the Bloom filter.
>>> b.hex()
'00000004'

When testing whether a bytes-like object is a member using the membership operator @ of an instance, the same hashing and truncation operations should be applied:

>>> sha256('abc'.encode()).digest()[:2] @ b
True
>>> sha256('xyz'.encode()).digest()[:2] @ b
False

The insertion operator @= also accepts iterable containers:

>>> x = sha256('x'.encode()).digest()[:2]
>>> y = sha256('y'.encode()).digest()[:2]
>>> z = sha256('z'.encode()).digest()[:2]
>>> b @= [x, y, z]
>>> b.hex()
'02200006'

The union of two Bloom filters (both having the same size) can be computed via the built-in | operator:

>>> c = blooms(4)
>>> c @= sha256('xyz'.encode()).digest()[:2]
>>> d = c | b
>>> sha256('abc'.encode()).digest()[:2] @ d
True
>>> sha256('xyz'.encode()).digest()[:2] @ d
True

It is also possible to check whether the members of one Bloom filter are a subset of the members of another Bloom filter:

>>> b.issubset(c)
False
>>> b.issubset(d)
True

The saturation method calculates the saturation of a Bloom filter. The saturation is a float value (between 0.0 and 1.0) that represents an upper bound on the rate with which false positives will occur when testing bytes-like objects (of a specific length) for membership within the Bloom filter:

>>> b = blooms(32)
>>> from secrets import token_bytes
>>> for _ in range(8):
...     b @= token_bytes(4)
>>> b.saturation(4)
0.03125

It is also possible to determine the approximate maximum capacity of a Bloom filter for a given saturation limit using the capacity method. For example, the output below indicates that a saturation of 0.05 will likely be reached after more than 28 insertions of bytes-like objects of length 8:

>>> b = blooms(32)
>>> b.capacity(8, 0.05)
28

In addition, conversion methods to and from Base64 strings are included to support concise encoding and decoding:

>>> b.to_base64()
'AiAABg=='
>>> sha256('abc'.encode()).digest()[:2] @ blooms.from_base64('AiAABg==')
True

If it is preferable to have a Bloom filter data structure that encapsulates a particular serialization, hashing, and truncation scheme, the recommended approach is to define a derived class. The specialize method makes it possible to do so in a concise way:

>>> encode = lambda x: sha256(x).digest()[:2]
>>> blooms_custom = blooms.specialize(name='blooms_custom', encode=encode)
>>> b = blooms_custom(4)
>>> b @= bytes([1, 2, 3])
>>> bytes([1, 2, 3]) @ b
True

The user of the library is responsible for ensuring that Base64-encoded Bloom filters are converted back into an an instance of the appropriate derived class by using the from_base64 method that belongs to that derived class:

>>> isinstance(blooms_custom.from_base64(b.to_base64()), blooms_custom)
True

Development

All installation and development dependencies are fully specified in pyproject.toml. The project.optional-dependencies object is used to specify optional requirements for various development tasks. This makes it possible to specify additional options (such as docs, lint, and so on) when performing installation using pip:

python -m pip install .[docs,lint]

Documentation

The documentation can be generated automatically from the source files using Sphinx:

python -m pip install .[docs]
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make html

Testing and Conventions

All unit tests are executed and their coverage is measured when using pytest (see the pyproject.toml file for configuration details):

python -m pip install .[test]
python -m pytest

The subset of the unit tests included in the module itself and can be executed using doctest:

python src/blooms/blooms.py -v

Style conventions are enforced using Pylint:

python -m pip install .[lint]
python -m pylint src/blooms test/test_blooms.py

Contributions

In order to contribute to the source code, open an issue or submit a pull request on the GitHub page for this library.

Versioning

The version number format for this library and the changes to the library associated with version number increments conform with Semantic Versioning 2.0.0.

Publishing

This library can be published as a package on PyPI by a package maintainer. First, install the dependencies required for packaging and publishing:

python -m pip install .[publish]

Ensure that the correct version number appears in pyproject.toml, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has an automation rule that activates and sets as the default all tagged versions. Create and push a tag for this version (replacing ?.?.? with the version number):

git tag ?.?.?
git push origin ?.?.?

Remove any old build/distribution files. Then, package the source into a distribution archive:

rm -rf build dist src/*.egg-info
python -m build --sdist --wheel .

Finally, upload the package distribution archive to PyPI:

python -m twine upload dist/*

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

blooms-1.2.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

blooms-1.2.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file blooms-1.2.0.tar.gz.

File metadata

  • Download URL: blooms-1.2.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.2

File hashes

Hashes for blooms-1.2.0.tar.gz
Algorithm Hash digest
SHA256 0241ce7ff51ea809862f9eaec69662d3ca78a23eb3abe8cf0d9053e27020384b
MD5 e6acb0b7b9fddbff0e840896fa2077ea
BLAKE2b-256 91d31f69e63b42797b06af3186c781f2c9f8919e589040022ee71dffe5ffb158

See more details on using hashes here.

File details

Details for the file blooms-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: blooms-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.2

File hashes

Hashes for blooms-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ce48470aa75ee399ec306318e1a5586400229a32656c462edb558466dc60a44
MD5 3c383334c56c47502a56c3bd69da7bb7
BLAKE2b-256 44781d991202500b4a43004cff95ce8727248934242ee3f946c2d9ccfa88b93a

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