Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reloading from a File does not appear to work in python 3 #7

Open
bscain opened this issue Apr 7, 2017 · 5 comments
Open

Reloading from a File does not appear to work in python 3 #7

bscain opened this issue Apr 7, 2017 · 5 comments

Comments

@bscain
Copy link

bscain commented Apr 7, 2017

The following sample code has been executed on both python 2 and python 3 with the same result

from pybloom_live import BloomFilter

if __name__ == '__main__':

    bf1 = BloomFilter(capacity=1000, error_rate=0.001)
    bf2 = BloomFilter(capacity=1000, error_rate=0.001)
    
    bf1.add(1)
    bf1.add(2)
    
    print("Test 1 in bf1: " + str(1 in bf1))
    print("Test 2 in bf1: " + str(2 in bf1))
    
    with open("/tmp/bloomTest.txt", "wb") as fp:
        bf1.tofile(fp)
    
    with open("/tmp/bloomTest.txt", "rb") as fp2:
        bf2.fromfile(fp2)
        
    print("Test 1 in bf2: " + str(1 in bf2))
    print("Test 2 in bf2: " + str(2 in bf2))

Output:
Test 1 in bf1: True
Test 2 in bf1: True
Test 1 in bf2: False
Test 2 in bf2: False

@bscain bscain closed this as completed Apr 7, 2017
@bscain
Copy link
Author

bscain commented Apr 7, 2017

Turns out I was using it incorrect, recommend adding samples on how to use the to / from file

@bscain bscain reopened this Apr 7, 2017
@ghost
Copy link

ghost commented Apr 11, 2017

I will take a look at it as soon as I can. Thanks @bscain

@evanfoster
Copy link

@bscain would you be able to comment with a very short example of how to use this feature? I'd just read the source but I'm away from my computer. No worries if you aren't able to, I'll do it once I can.

@paularmand
Copy link

This should work:

from io import BytesIO
bytesio = BytesIO()

# sbf is your scalable bloom filter
sbf.tofile(bytesio)

# reset the stream handle to the start
bytesio.seek(0)
# you can check it with: print(bytesio.getvalue().hex())

sbf_read = pybloom_live.ScalableBloomFilter.fromfile(bytesio)
bytesio.close()

@Huangvivi
Copy link

The following sample code has been executed on both python 2 and python 3 with the same result

from pybloom_live import BloomFilter

if __name__ == '__main__':

    bf1 = BloomFilter(capacity=1000, error_rate=0.001)
    bf2 = BloomFilter(capacity=1000, error_rate=0.001)
    
    bf1.add(1)
    bf1.add(2)
    
    print("Test 1 in bf1: " + str(1 in bf1))
    print("Test 2 in bf1: " + str(2 in bf1))
    
    with open("/tmp/bloomTest.txt", "wb") as fp:
        bf1.tofile(fp)
    
    with open("/tmp/bloomTest.txt", "rb") as fp2:
        bf2.fromfile(fp2)
        
    print("Test 1 in bf2: " + str(1 in bf2))
    print("Test 2 in bf2: " + str(2 in bf2))

Output:
Test 1 in bf1: True
Test 2 in bf1: True
Test 1 in bf2: False
Test 2 in bf2: False

Hey guy, I met this problem at a time, then I found the method fromfile is a classmethod.
It means you don't need to initial bf2 with args, just

with open("/tmp/bloomTest.txt", "rb") as fp2:
    bf2 = BloomFilter.fromfile(fp2)

It works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants