Skip to content

Instantly share code, notes, and snippets.

@shiumachi
Created November 26, 2018 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiumachi/f4e617cf1a3afa8c122f52e8334d6656 to your computer and use it in GitHub Desktop.
Save shiumachi/f4e617cf1a3afa8c122f52e8334d6656 to your computer and use it in GitHub Desktop.
Bloomfilter sample
#!/usr/bin/python
import hashlib
startKey = 2
endKey = 6
inputNum = 1000
testNum = 100000
def check_bl(bloom, a):
aa = hashlib.md5(a).hexdigest()[startKey:endKey]
ab = hashlib.sha1(a).hexdigest()[startKey:endKey]
ac = hashlib.sha256(a).hexdigest()[startKey:endKey]
ad = hashlib.sha224(a).hexdigest()[startKey:endKey]
if bloom.get(aa, 0) == 1 \
and bloom.get(ab, 0) == 1 \
and bloom.get(ac, 0) == 1 \
and bloom.get(ad, 0) == 1:
return True
return False
def bl(bloom, a):
bloom[hashlib.md5(a).hexdigest()[startKey:endKey]] = 1
bloom[hashlib.sha1(a).hexdigest()[startKey:endKey]] = 1
bloom[hashlib.sha256(a).hexdigest()[startKey:endKey]] = 1
bloom[hashlib.sha224(a).hexdigest()[startKey:endKey]] = 1
def initialize(bloom):
for i in xrange(inputNum):
bl(bloom, 'd' + str(i))
def testBloom(bloom):
count = 0
for i in xrange(inputNum, inputNum + testNum):
if check_bl(bloom, 'a' + str(i)):
print "%s is false positive" % ('a' + str(i),)
count += 1
print "false positive count: %d" % (count,)
def main():
bloom = {}
initialize(bloom)
testBloom(bloom)
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment