moved movies_test to doctest

This commit is contained in:
Isidro Arias 2023-04-07 16:01:35 +02:00
parent cc54095c47
commit 78d19fd19b

View File

@ -1,5 +1,21 @@
""" """
See https://en.wikipedia.org/wiki/Bloom_filter See https://en.wikipedia.org/wiki/Bloom_filter
>>> b = Bloom()
>>> b.add("Titanic")
>>> b.add("Avatar")
>>> b.exists("Titanic")
True
>>> b.exists("Avatar")
True
>>> b.exists("The Goodfather")
False
>>> b.exists("Interstellar")
False
>>> b.exists("Parasite")
False
>>> b.exists("Pulp Fiction")
False
""" """
from hashlib import md5, sha256 from hashlib import md5, sha256
from random import choices from random import choices
@ -17,26 +33,27 @@ class Bloom:
def add(self, value: str) -> None: def add(self, value: str) -> None:
h = self.hash_(value) h = self.hash_(value)
self.bitstring |= h self.bitstring |= h
print(
f"""\ # print(
[add] value = {value} # f"""\
hash = {self.format_bin(h)} # [add] value = {value}
filter = {self.format_bin(self.bitstring)} # hash = {self.format_bin(h)}
""" # filter = {self.format_bin(self.bitstring)}
) # """
# )
def exists(self, value: str) -> bool: def exists(self, value: str) -> bool:
h = self.hash_(value) h = self.hash_(value)
res = (h & self.bitstring) == h res = (h & self.bitstring) == h
print( # print(
f"""\ # f"""\
[exists] value = {value} # [exists] value = {value}
hash = {self.format_bin(h)} # hash = {self.format_bin(h)}
filter = {self.format_bin(self.bitstring)} # filter = {self.format_bin(self.bitstring)}
res = {res} # res = {res}
""" # """
) # )
return res return res
def format_bin(self, value: int) -> str: def format_bin(self, value: int) -> str:
@ -52,20 +69,6 @@ class Bloom:
return res return res
def test_movies() -> None:
b = Bloom()
b.add("Titanic")
b.add("Avatar")
assert b.exists("Titanic")
assert b.exists("Avatar")
assert b.exists("The Goodfather") in (True, False)
assert b.exists("Interstellar") in (True, False)
assert b.exists("Parasite") in (True, False)
assert b.exists("Pulp Fiction") in (True, False)
def random_string(size: int) -> str: def random_string(size: int) -> str:
return "".join(choices(ascii_lowercase + " ", k=size)) return "".join(choices(ascii_lowercase + " ", k=size))
@ -101,5 +104,4 @@ def test_probability(filter_bits: int = 64, added_elements: int = 20) -> None:
if __name__ == "__main__": if __name__ == "__main__":
test_movies()
test_probability() test_probability()