def test_bf_clear(self): """ test clearing out the bloom filter """ blm = BloomFilter(est_elements=10, false_positive_rate=0.05) self.assertEqual(blm.elements_added, 0) for i in range(0, 10): tmp = "this is a test {0}".format(i) blm.add(tmp) self.assertEqual(blm.elements_added, 10) blm.clear() self.assertEqual(blm.elements_added, 0) for idx in range(blm.bloom_length): self.assertEqual(blm._get_element(idx), 0)
def test_bf_clear(self): ''' test clearing out the bloom filter ''' blm = BloomFilter(est_elements=10, false_positive_rate=0.05) self.assertEqual(blm.elements_added, 0) for i in range(0, 10): tmp = 'this is a test {0}'.format(i) blm.add(tmp) self.assertEqual(blm.elements_added, 10) blm.clear() self.assertEqual(blm.elements_added, 0) for idx in range(blm.bloom_length): self.assertEqual(blm._get_element(idx), 0)
class Doorkeeper: def __init__(self, cap=100000, false_positive=0.01): self.bloom = BloomFilter(cap, false_positive) def __insert(self, key: str): already_present = self.bloom.check(key) self.bloom.add(key) return already_present def allow(self, key: str): return self.__insert(key) def reset(self): self.bloom.clear()