https://github.com/oldpanda/bloomfilter-py
Yet another Bloomfilter implementation in Python, compatible with Java's Guava library
https://github.com/oldpanda/bloomfilter-py
bloomfilter bloomfilter-python python python-library python3
Last synced: 2 days ago
JSON representation
Yet another Bloomfilter implementation in Python, compatible with Java's Guava library
- Host: GitHub
- URL: https://github.com/oldpanda/bloomfilter-py
- Owner: OldPanda
- License: mit
- Created: 2020-12-27T00:54:34.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-08-10T21:02:10.000Z (about 1 year ago)
- Last Synced: 2025-04-13T22:40:54.667Z (6 months ago)
- Topics: bloomfilter, bloomfilter-python, python, python-library, python3
- Language: Python
- Homepage: https://pypi.org/project/bloomfilter-py/
- Size: 49.8 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bloomfilter-py


[](https://codecov.io/gh/OldPanda/bloomfilter-py)
[](https://pepy.tech/project/bloomfilter-py)## Overview
Yet another Bloomfilter implementation in Python, compatible with Java's Guava library.I was looking for a Python library which is capable of reading what Bloomfilter of Java's Guava library serializes and is also able to output byte array which is recognizable by Java. But unfortunately failed. Hence I developed this library by borrowing how Guava implements Bloomfilter serialization/deserialization a lot to deal with Bloomfilters on both Python and Java sides.
As for Bloomfilter usage in Java world, please refer to [this post](https://www.baeldung.com/guava-bloom-filter).
Here's a brief [introduction](https://en.wikipedia.org/wiki/Bloom_filter) to Bloomfilter.
## Requirements
* Python 3.8+## Install
```
pip install bloomfilter-py
```## Usage Examples
### Basic Usage
```Python
>>> from bloomfilter import BloomFilter
>>> bloom_filter = BloomFilter(expected_insertions=500, err_rate=0.01)
>>> for i in range(100):
... bloom_filter.put(i)
...
>>> 1 in bloom_filter
True
>>> 100 in bloom_filter
False
>>>
```### Serialize Bloomfilter
You can easily serialize `BloomFilter` instance to a byte array
```Python
>>> dumps = bloom_filter.dumps()
>>> with open("dumps.out", "wb") as f:
... f.write(dumps)
...
>>>
```or to a hex string
```Python
>>> hex_str = bloom_filter.dumps_to_hex()
```or to a base64 encoded bytes
```Python
base64_bytes = bloom_filter.dumps_to_base64()
```### Deserialize Bloomfilter
And you can easily initialize a `BloomFilter` instance from a byte array
```Python
>>> with open("dumps.out", "rb") as f:
... bf = BloomFilter.loads(f.read())
...
>>> 1 in bf
True
>>> 100 in bf
False
>>>
```or from a hex string
```Python
>>> bf = BloomFilter.loads_from_hex(hex_str)
>>> 1 in bf
True
>>> 100 in bf
False
```or from a base64 encoded bytes
```Python
>>> bf = BloomFilter.loads_from_base64(base64_bytes)
>>> 100 in bf
False
>>> 200 in bf
False
>>> 1 in bf
True
>>> 99 in bf
True
```