Example #1
0
    def test_another_hashing_algo(self):
        """ test defining a completely different hashing strategy """
        md5_val = "7f590086f9b962387e145899dd001256"  # for default hash used
        filename = "test.blm"
        results = [
            14409285476674975580,
            1383622036369840193,
            10825905054403519891,
            3456253732347153957,
            1494124715262089992,
        ]

        def my_hash(key, depth, encoding="utf-8"):
            """ my hashing strategy """
            max64mod = UINT64_T_MAX + 1
            results = list()
            for i in range(0, depth):
                tmp = key[i:] + key[:i]
                val = int(hashlib.sha512(tmp.encode(encoding)).hexdigest(), 16)
                results.append(val % max64mod)
            return results

        blm = BloomFilter(est_elements=10,
                          false_positive_rate=0.05,
                          hash_function=my_hash)

        self.assertEqual(blm.elements_added, 0)
        blm.add("this is a test")
        blm.export(filename)

        md5_out = calc_file_md5(filename)
        self.assertNotEqual(md5_out, md5_val)
        os.remove(filename)

        for i in range(0, 10):
            tmp = "this is a test {0}".format(i)
            blm.add(tmp)

        self.assertEqual(blm.elements_added, 11)

        for i in range(0, 10):
            tmp = "this is a test {0}".format(i)
            self.assertTrue(blm.check(tmp))

        self.assertEqual(blm.hashes("this is a test", 5), results)
        res = blm.hashes("this is a test", 1)
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0], results[0])
Example #2
0
    def test_bf_use_different_hash(self):
        """test that the different hash works as intended"""
        md5_val = "7f590086f9b962387e145899dd001256"  # for default hash used
        results = [
            14409285476674975580,
            6203976290780191624,
            5074829385518853901,
            3953072760750514173,
            11782747630324011555,
        ]

        @hash_with_depth_int
        def my_hash(key, depth=1, encoding="utf-8"):
            """my hash function"""
            max64mod = UINT64_T_MAX + 1
            val = int(hashlib.sha512(key.encode(encoding)).hexdigest(), 16)
            return val % max64mod

        blm = BloomFilter(est_elements=10,
                          false_positive_rate=0.05,
                          hash_function=my_hash)
        self.assertEqual(blm.elements_added, 0)
        blm.add("this is a test")
        with NamedTemporaryFile(dir=os.getcwd(),
                                suffix=".blm",
                                delete=DELETE_TEMP_FILES) as fobj:
            blm.export(fobj.name)

            md5_out = calc_file_md5(fobj.name)
        self.assertNotEqual(md5_out, md5_val)

        for i in range(0, 10):
            tmp = "this is a test {0}".format(i)
            blm.add(tmp)

        self.assertEqual(blm.elements_added, 11)

        for i in range(0, 10):
            tmp = "this is a test {0}".format(i)
            self.assertTrue(blm.check(tmp))

        self.assertEqual(blm.hashes("this is a test", 5), results)
        res = blm.hashes("this is a test", 1)
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0], results[0])
Example #3
0
    def test_another_hashing_algo(self):
        ''' test defining a completely different hashing strategy '''
        md5_val = '7f590086f9b962387e145899dd001256'  # for default hash used
        filename = 'test.blm'
        results = [14409285476674975580,
                   1383622036369840193,
                   10825905054403519891,
                   3456253732347153957,
                   1494124715262089992]

        def my_hash(key, depth, encoding='utf-8'):
            ''' my hashing strategy '''
            max64mod = UINT64_T_MAX + 1
            results = list()
            for i in range(0, depth):
                tmp = key[i:] + key[:i]
                val = int(hashlib.sha512(tmp.encode(encoding)).hexdigest(), 16)
                results.append(val % max64mod)
            return results

        blm = BloomFilter(est_elements=10, false_positive_rate=0.05,
                          hash_function=my_hash)

        self.assertEqual(blm.elements_added, 0)
        blm.add('this is a test')
        blm.export(filename)

        md5_out = calc_file_md5(filename)
        self.assertNotEqual(md5_out, md5_val)
        os.remove(filename)

        for i in range(0, 10):
            tmp = 'this is a test {0}'.format(i)
            blm.add(tmp)

        self.assertEqual(blm.elements_added, 11)

        for i in range(0, 10):
            tmp = 'this is a test {0}'.format(i)
            self.assertTrue(blm.check(tmp))

        self.assertEqual(blm.hashes('this is a test', 5), results)
        res = blm.hashes('this is a test', 1)
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0], results[0])
Example #4
0
    def test_bf_use_different_hash(self):
        ''' test that the different hash works as intended '''
        md5_val = '7f590086f9b962387e145899dd001256'  # for default hash used
        filename = 'test.blm'
        results = [
            14409285476674975580, 6203976290780191624, 5074829385518853901,
            3953072760750514173, 11782747630324011555
        ]

        @hash_with_depth_int
        def my_hash(key, encoding='utf-8'):
            ''' my hash function '''
            max64mod = UINT64_T_MAX + 1
            val = int(hashlib.sha512(key.encode(encoding)).hexdigest(), 16)
            return val % max64mod

        blm = BloomFilter(est_elements=10,
                          false_positive_rate=0.05,
                          hash_function=my_hash)
        self.assertEqual(blm.elements_added, 0)
        blm.add('this is a test')
        blm.export(filename)

        md5_out = calc_file_md5(filename)
        self.assertNotEqual(md5_out, md5_val)
        os.remove(filename)

        for i in range(0, 10):
            tmp = 'this is a test {0}'.format(i)
            blm.add(tmp)

        self.assertEqual(blm.elements_added, 11)

        for i in range(0, 10):
            tmp = 'this is a test {0}'.format(i)
            self.assertTrue(blm.check(tmp))

        self.assertEqual(blm.hashes('this is a test', 5), results)
        res = blm.hashes('this is a test', 1)
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0], results[0])
Example #5
0
    def test_bf_use_different_hash(self):
        ''' test that the different hash works as intended '''
        md5_val = '7f590086f9b962387e145899dd001256'  # for default hash used
        filename = 'test.blm'
        results = [14409285476674975580,
                   6203976290780191624,
                   5074829385518853901,
                   3953072760750514173,
                   11782747630324011555]

        @hash_with_depth_int
        def my_hash(key, encoding='utf-8'):
            ''' my hash function '''
            max64mod = UINT64_T_MAX + 1
            val = int(hashlib.sha512(key.encode(encoding)).hexdigest(), 16)
            return val % max64mod

        blm = BloomFilter(est_elements=10, false_positive_rate=0.05,
                          hash_function=my_hash)
        self.assertEqual(blm.elements_added, 0)
        blm.add('this is a test')
        blm.export(filename)

        md5_out = calc_file_md5(filename)
        self.assertNotEqual(md5_out, md5_val)
        os.remove(filename)

        for i in range(0, 10):
            tmp = 'this is a test {0}'.format(i)
            blm.add(tmp)

        self.assertEqual(blm.elements_added, 11)

        for i in range(0, 10):
            tmp = 'this is a test {0}'.format(i)
            self.assertTrue(blm.check(tmp))

        self.assertEqual(blm.hashes('this is a test', 5), results)
        res = blm.hashes('this is a test', 1)
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0], results[0])