do not reference bytearray in a method definition, fixes #5

This is a severe issue which makes the current voters bloomfilter
completely useless. Functions are first-class objects in Python, which
lead to interesting "issues" like:

    >>> def foo(x=[]):
    ...     x.append(1)
    ...     print x
    ...

    >>> foo()
    [1]

    >>> foo()
    [1, 1]

For Isso, this means the bloomfilter, which is usually only initialized
with the author's IP address, is now initialized with pretty much all
ip addresses from previous authors, thus makes it impossible for the
author to vote on other's people comments.
This commit is contained in:
Martin Zimmermann 2013-11-13 18:27:35 +01:00
parent 5431dd0f0b
commit 58645ca945

View File

@ -56,15 +56,31 @@ class Bloomfilter:
of space efficiency (array is saved for each comment) and 11 hash functions of space efficiency (array is saved for each comment) and 11 hash functions
because of best overall false-positive rate in that range. because of best overall false-positive rate in that range.
>>> bf = Bloomfilter()
>>> bf.add("127.0.0.1")
>>> not any(map(bf.__contains__, ("1.2.%i.4" for i in range(256))))
True
>>> bf = Bloomfilter()
>>> for i in range(256):
... bf.add("1.2.%i.4" % i)
...
>>> len(bf)
256
>>> "1.2.3.4" in bf
True
>>> "127.0.0.1" in bf
False
-- via Raymond Hettinger -- via Raymond Hettinger
http://code.activestate.com/recipes/577684-bloom-filter/ http://code.activestate.com/recipes/577684-bloom-filter/
""" """
def __init__(self, array=bytearray(256), elements=0, iterable=()): def __init__(self, array=None, elements=0, iterable=()):
self.array = array self.array = array or bytearray(256)
self.elements = elements self.elements = elements
self.k = 11 self.k = 11
self.m = len(array) * 8 self.m = len(self.array) * 8
for item in iterable: for item in iterable:
self.add(item) self.add(item)