好记性不如铅笔头

python && jython, 编程

python下使用pybloomfiltermmap

CONTENTS

bloom filter原理介绍:

【 http://www.cnblogs.com/heaad/archive/2011/01/02/1924195.html

【 https://en.wikipedia.org/wiki/Bloom_filter

python 版本的bloom filter代码实现:

【 http://axiak.github.io/pybloomfiltermmap/index.html#

pybloomfiltermmap安装:

本地编译安装pybloomfiltermmap时需要使用下面的环境:

pydev:

作者本地有两个版本的python,这里就下载了两个版本
sudo apt-get install python-dev python3-dev

setuptools:

参考官网的介绍,直接使用wget方式,这里同样在两个版本上安装:

wget https://bootstrap.pypa.io/ez_setup.py -O – | sudo python
wget https://bootstrap.pypa.io/ez_setup.py -O – | sudo python3

libcrypto.so

编译时需要使用库libcrypto.so,作者本地环境里面有,就没有额外下载,如果没有,需要安装openssl:

cstriker1407@cstriker1407-x64:/lib$ find . -name "*libcrypto*"
./x86_64-linux-gnu/libcrypto.so.1.0.0
./i386-linux-gnu/libcrypto.so.1.0.0
cstriker1407@cstriker1407-x64:/lib$ sudo  ln -s /lib/x86_64-linux-gnu/libcrypto.so.1.0.0  /usr/lib64/libcrypto.so
cstriker1407@cstriker1407-x64:/lib$ sudo  ln -s /lib/i386-linux-gnu/libcrypto.so.1.0.0  /usr/lib32/libcrypto.so

上面的环境都搞定之后,就可以开始下载安装了。

从github上下个最新版本,下载网址:【 https://github.com/axiak/pybloomfiltermmap/tags

然后执行命令:

cstriker1407@cstriker1407-x64:~$ tar -zxf pybloomfiltermmap-release-0.3.12.tar.gz 
cstriker1407@cstriker1407-x64:~$ cd pybloomfiltermmap-release-0.3.12/

然后打开setup.py文件,将31行和43行的print xx 改为print(xx),保存退出,然后开始安装:

cstriker1407@cstriker1407-x64:~/pybloomfiltermmap-release-0.3.12$  sudo python setup.py install
。。。。。。
Adding pybloomfiltermmap 0.3.12 to easy-install.pth file

Installed /usr/local/lib/python2.7/dist-packages/pybloomfiltermmap-0.3.12-py2.7-linux-x86_64.egg
Processing dependencies for pybloomfiltermmap==0.3.12
Finished processing dependencies for pybloomfiltermmap==0.3.12

cstriker1407@cstriker1407-x64:~/pybloomfiltermmap-release-0.3.12$  sudo python3 setup.py install
。。。。。
Adding pybloomfiltermmap 0.3.12 to easy-install.pth file

Installed /usr/local/lib/python3.4/dist-packages/pybloomfiltermmap-0.3.12-py3.4-linux-x86_64.egg
Processing dependencies for pybloomfiltermmap==0.3.12
Finished processing dependencies for pybloomfiltermmap==0.3.12

简单使用:

API:【 http://axiak.github.io/pybloomfiltermmap/ref.html#

    from pybloomfilter import BloomFilter;
    #新建一个bloomFileter,预计容量为10,错误率为30%,本位文件为tmpfile.bf
    #you add less than capacity items, and the Bloom Filter will have an error rate less than error_rate.
    #If you specify None for the filename, then the bloom filter will be backed by malloc’d memory, rather than by a file.
    mybf = BloomFilter(10, 0.3,b'tmpfile.bf');
    print(mybf.capacity);
    print(mybf.error_rate);
    print(mybf.name);
    print("len:", len(mybf));

    for i in range(1,10):
        print( i,mybf.add(i) ); #如果当前已经有该元素了,返回true。
      
    print("len:", len(mybf));
        
    for i in range(1,10):
        print( i,  i in mybf );   

    mybf.sync();#同步到文件        
    mybf.clear_all(); #清空

    mybf.sync();#同步到文件
    print("len:", len(mybf)); #此处输出没有变化,比较奇怪

输出:

10
0.5
b'tmpfile.bf'
len: 0
1 False
2 False
3 False
4 False
5 False
6 False
7 True
8 True
9 False
len: 7
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
len: 7

 

发表评论

4 × 3 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据