python中的概率数据结构
pyprobables的Python项目详细描述
pyprobables是用于概率数据结构的纯python库。 目标是为开发人员提供 在工作中使用的常见概率数据结构。
为了获得更好的原始性能,建议提供替代方案 已在C中编译的哈希算法。这可能包括使用 提供或安装第三方软件包的MD5和SHA512算法,以及 写你自己的散列策略。有些选择包括杂音杂凑 mmh3或来自 pyhash库。中的每个数据对象 pyprobables使传递自定义哈希函数变得容易。
阅读有关如何使用Supplying a pre-defined, alternative hashing strategies的详细信息 或者Defining hashing function using the provided decorators。
安装
PIP安装:
$ pip install pyprobables
从源安装:
要安装pyprobables
,只需克隆repository on GitHub,然后从文件夹运行:
$ python setup.py install
pyprobables
支持python版本2.7和3.3-3.6
自动测试
要运行自动测试,只需从 下载的文件夹:
$ python setup.py test
快速启动
导入pyprobables并设置bloom过滤器
fromprobablesimport(BloomFilter)blm=BloomFilter(est_elements=1000,false_positive_rate=0.05)blm.add('google.com')blm.check('facebook.com')# should return Falseblm.check('google.com')# should return True
导入PyProbables并设置计数最小草图
fromprobablesimport(CountMinSketch)cms=CountMinSketch(width=1000,depth=5)cms.add('google.com')# should return 1cms.add('facebook.com',25)# insert 25 at once; should return 25
导入pyprobables并设置布谷鸟过滤器
fromprobablesimport(CuckooFilter)cko=CuckooFilter(capacity=100,max_swaps=10)cko.add('google.com')cko.check('facebook.com')# should return Falsecko.check('google.com')# should return True
提供预定义的可选哈希策略
fromprobablesimport(BloomFilter)fromprobables.hashesimport(default_sha256)blm=BloomFilter(est_elements=1000,false_positive_rate=0.05,hash_function=default_sha256)blm.add('google.com')blm.check('facebook.com')# should return Falseblm.check('google.com')# should return True
使用提供的decorators定义散列函数
importmmh3# murmur hash 3 implementation (pip install mmh3)frompyprobables.hashesimport(hash_with_depth_bytes)frompyprobablesimport(BloomFilter)@hash_with_depth_bytesdefmy_hash(key):returnmmh3.hash_bytes(key)blm=BloomFilter(est_elements=1000,false_positive_rate=0.05,hash_function=my_hash)
importmmh3# murmur hash 3 implementation (pip install mmh3)frompyprobables.hashesimport(hash_with_depth_int)frompyprobablesimport(BloomFilter)@hash_with_depth_intdefmy_hash(key,encoding='utf-8'):max64mod=UINT64_T_MAX+1val=int(hashlib.sha512(key.encode(encoding)).hexdigest(),16)returnval%max64modblm=BloomFilter(est_elements=1000,false_positive_rate=0.05,hash_function=my_hash)
见API documentation 对于其他可用的数据结构和 quickstart page 更多的例子!
更改日志
请参阅changelog获取列表 所有的变化。