Skip to content

Commit

Permalink
add has_keys_with_prefix method (python, tests and docs)
Browse files Browse the repository at this point in the history
  • Loading branch information
hickford committed Oct 2, 2013
1 parent 8200dbd commit ea9189e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
MANIFEST
src/*.html

*.py[cod]

# C extensions
*.so

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
lib
lib64
__pycache__

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox
nosetests.xml

3 changes: 2 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Authors & Contributors
* Mikhail Korobov <[email protected]>;
* Dan Blanchard;
* Jakub Wilk;
* Alex Moiseenko.
* Alex Moiseenko;
* `Matt Hickford <https://github.com/matt-hickford>`_.

This module uses `dawgdic`_ C++ library by
Susumu Yata & contributors.
Expand Down
5 changes: 5 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ prefix in a ``CompletionDAWG``::
>>> completion_dawg.keys(u'foo')
>>> [u'foo', u'foobar']

to test whether some key begins with a given prefix::

>>> completion_dawg.has_keys_with_prefix(u'foo')
>>> True

and to find all prefixes of a given key::

>>> base_dawg.prefixes(u'foobarz')
Expand Down
12 changes: 12 additions & 0 deletions src/dawg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@ cdef class CompletionDAWG(DAWG):
key = (<char*>completer.key()).decode('utf8')
yield key

def has_keys_with_prefix(self, unicode prefix):
cdef bytes b_prefix = prefix.encode('utf8')
cdef BaseType index = self.dct.root()

if not self.dct.Follow(b_prefix, &index):
return False

cdef Completer completer
init_completer(completer, self.dct, self.guide)
completer.Start(index, b_prefix)

return completer.Next()

cpdef bytes tobytes(self) except +:
"""
Expand Down
9 changes: 9 additions & 0 deletions tests/test_dawg.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ def test_completion(self):
assert d.keys('b') == ['bar']
assert d.keys('foo') == ['foo', 'foobar']

def test_has_keys_with_prefix(self):
assert self.empty_dawg().has_keys_with_prefix('') == False

d = self.dawg()
assert d.has_keys_with_prefix('') == True
assert d.has_keys_with_prefix('b') == True
assert d.has_keys_with_prefix('fo') == True
assert d.has_keys_with_prefix('bo') == False

def test_completion_dawg_saveload(self):
buf = BytesIO()
self.dawg().write(buf)
Expand Down

0 comments on commit ea9189e

Please sign in to comment.