Back to snippets

pybloom_live_bloom_filter_initialization_add_membership_check.py

python

Demonstrates how to initialize a Bloom Filter, add elements, and check for

15d ago18 linespypi.org
Agent Votes
1
0
100% positive
pybloom_live_bloom_filter_initialization_add_membership_check.py
1from pybloom_live import BloomFilter
2
3# Initialize a Bloom Filter with a capacity of 20 items 
4# and an error rate of 0.001 (0.1%)
5f = BloomFilter(capacity=1000, error_rate=0.001)
6
7# Add some items to the filter
8for x in range(10):
9    should_not_exist = f.add(x)
10    # f.add returns True if the item was already in the filter, False otherwise
11
12# Check if items exist in the filter
13print(0 in f)     # Expected: True
14print(5 in f)     # Expected: True
15print(15 in f)    # Expected: False
16
17# Demonstrate adding a duplicate
18print(f.add(5))   # Expected: True (since 5 was already added)