Skip to content

Commit

Permalink
Merge pull request #50 from peterjc/py_contains
Browse files Browse the repository at this point in the history
Support 'key in bloom' in Python wrapper.
  • Loading branch information
Justin Hines committed Oct 12, 2012
2 parents 670cf92 + 194e71b commit f405ee4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 22 additions & 1 deletion pydablooms/pydablooms.c
Expand Up @@ -45,6 +45,16 @@ static int Dablooms_init(Dablooms *self, PyObject *args, PyObject *kwds)
return 0;
}

static int contains(Dablooms *self, PyObject *key)
{
if (!PyString_Check(key)) {
return 0; /* return False */
}
return scaling_bloom_check(self->filter,
PyString_AsString(key),
(int)PyString_Size(key));
}

static PyObject *check(Dablooms *self, PyObject *args)
{
const char *hash;
Expand Down Expand Up @@ -113,6 +123,17 @@ static PyMemberDef Dablooms_members[] = {
{NULL} /* Sentinel */
};

static PySequenceMethods Dablooms_sequence = {
NULL, /*sq_length*/
NULL, /*sq_concat*/
NULL, /*sq_repeat*/
NULL, /*sq_item*/
NULL, /*sq_slice*/
NULL, /*sq_ass_item*/
NULL, /*sq_ass_slice*/
(objobjproc)contains, /*sq_contains*/
};

static PyTypeObject DabloomsType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
Expand All @@ -126,7 +147,7 @@ static PyTypeObject DabloomsType = {
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
&Dablooms_sequence, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
Expand Down
6 changes: 5 additions & 1 deletion pydablooms/test_pydablooms.py
Expand Up @@ -46,7 +46,11 @@
i = 0
for line in words_file:
exists = bloom.check(line.rstrip())

contains = line.rstrip() in bloom
assert exists == contains, \
"ERROR: %r from 'bloom.check(x)', %i from 'x in bloom'" \
% (exists, contains)

if i % 5 == 0:
if exists:
false_positives += 1
Expand Down

0 comments on commit f405ee4

Please sign in to comment.