Created
November 26, 2018 07:59
-
-
Save shiumachi/f4e617cf1a3afa8c122f52e8334d6656 to your computer and use it in GitHub Desktop.
Bloomfilter sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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