Open the context
Choose the project folder and name the files or behavior involved.
Store byte keys of at most 1024 bytes in a Bloom filter with one to 8192 bits. Check duplicate insertion and inserted keys, demonstrating that a positive result can be a false positive rather than proof of membership.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from hashlib import sha256
class Bloom:
def __init__(self, width):
if type(width) is not int or not 1 <= width <= 8192:
raise ValueError("width.range")
self.width, self.bits = width, 0
def positions(self, key):
if not isinstance(key, bytes) or len(key) > 1024:
raise ValueError("key.bytes")
digest = sha256(key).digest()
return [int.from_bytes(digest[i:i+4], "big") % self.width
for i in (0, 4, 8, 12)]
def add(self, key):
for position in self.positions(key):
self.bits |= 1 << position
def maybe_contains(self, key):
return all(self.bits & (1 << p) for p in self.positions(key))The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.