def search(self, query_string): """Return an iterable of ITerms matching `query_string`. A term matches, if its normalized title starts with `query_string`. "normalized" means what `dinsort` defines as normalizing. We will delver at most 10 entries. Why so few? Because the normally used autocomplete widget only displays 10 items and does its own sorting. This led to the following unfortunate possibility: if a term contains umlauts and is sorted by autocomplete widget relatively late, then the term might not show up in the first ten items displayed and is therefore unpickable at all. Giving 10 entries, we can be sure that the (in *our* ordering) first picked term is displayed by the autocomplete widget. """ query_string = normalize(query_string) search_term = "(%s" % to_string(query_string) db_entries = self._get_client().zrangebylex( self.zset_name, search_term, "+", 0, 10) for entry in db_entries: normalized, token = self._split_entry(entry) term = self.getTerm(token) yield term
def __init__(self, host='localhost', port=6379, db=0, zset_name="autocomplete", separator="&&", allow_iter=True): self.host = host self.port = port self.db = db self.zset_name = zset_name self.separator = to_string(separator) self.allow_iter = allow_iter
def _split_entry(self, entry): """Split an entry as found in ZSETs into pieces. """ normalized, title = to_string(entry).split(self.separator, 1) return normalized.encode("utf-8"), title.decode("utf-8")
def test_to_string_from_number(self): # we can turn numbers into strings result = to_string(999) self.assertEqual(result, "999")
def test_to_string_from_unicode(self): # we can turn unicodes into strings result = to_string(u"äöü") self.assertEqual(result, "äöü")
def test_to_string_from_string(self): # we can turn strings into strings result = to_string("äöü") self.assertEqual(result, "äöü")