public
Last active

  • Download Gist
autocomplete.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
# autocomplete.py - Redis autocomplete example
# download female-names.txt from http://antirez.com/misc/female-names.txt
# Ruby original: http://gist.github.com/574044
 
# Requires http://github.com/andymccurdy/redis-py/
 
from redis import Redis
 
r = Redis()
KEY = 'compl'
 
# Create the completion sorted set
if not r.exists(KEY):
print "Loading entries in the Redis DB"
for line in open('female-names.txt').readlines():
line = line.strip()
for end_index in range(1, len(line)):
prefix = line[0:end_index]
r.zadd(KEY, prefix, 0)
r.zadd(KEY, line + '*', 0)
else:
print "NOT loading entries, there is already a %s key" % KEY
 
def complete(r, prefix, count):
results = []
rangelen = 50
start = r.zrank(KEY, prefix)
if not start:
return []
while len(results) != count:
range = r.zrange(KEY, start, start + rangelen - 1)
start += rangelen
if not range or len(range) == 0:
break
for entry in range:
minlen = min((len(entry), len(prefix)))
if entry[0:minlen] != prefix[0:minlen]:
count = len(results)
break
if entry[-1] == '*' and len(results) != count:
results.append(entry[0:-1])
return results
 
# Complete the string "marcell"
for result in complete(r, "marcell", 50):
print result

Fairly sure this isn't the most Pythonic way of doing it, but it was an exercise in making a direct Ruby -> Python port. Feel free to fork and fix...

Awesome - thanks for sharing this. I branched it and gave i support for multi-word phrases. https://gist.github.com/925979

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.