Skip to content

Instantly share code, notes, and snippets.

@matze
Created September 4, 2017 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matze/e096322975fd5b15cd3739d5beae24ff to your computer and use it in GitHub Desktop.
Save matze/e096322975fd5b15cd3739d5beae24ff to your computer and use it in GitHub Desktop.
Comparison of search algorithms
import os
import sys
import re
import difflib
store = os.path.abspath('/home/matthias/dev/password-store/')
def get_password_names_old(name):
names = []
for root, dirs, files in os.walk(store):
dir_path = root[len(store) + 1:]
if dir_path.startswith('.'):
continue
for filename in files:
path = os.path.join(dir_path, filename)
if re.match(r'.*{0}.*\.gpg$'.format(name), path, re.IGNORECASE):
names.append(path[:-4])
return names
def get_password_names_new(name):
def is_junk(x):
return x == ' '
matcher = difflib.SequenceMatcher(isjunk=is_junk, b=name, autojunk=False)
matches = []
for root, dirs, files in os.walk(store):
dir_path = root[len(store) + 1:]
if dir_path.startswith('.'):
continue
for filename in files:
path = os.path.join(dir_path, filename)
matcher.set_seq1(path[:-4])
score = matcher.ratio()
if score >= 0.5:
matches.append((score, path[:-4]))
return [x[1] for x in sorted(matches, key=lambda x: x[0], reverse=True)]
def get_password_names_other(name):
matcher = difflib.SequenceMatcher(b=name, autojunk=False)
matches = {}
for root, dirs, files in os.walk(store):
dir_path = root[len(store) + 1:]
if dir_path.startswith('.'):
continue
for filename in files:
path = os.path.join(dir_path, filename)[:-4]
for name in path.split('/'):
matcher.set_seq1(name)
score = matcher.ratio()
if score >= 0.5 and (path not in matches or score > matches[path]):
matches[path] = score
return sorted(matches, key=matches.__getitem__, reverse=True)
if __name__ == '__main__':
print "old =", get_password_names_old(sys.argv[1])
print "new =", get_password_names_new(sys.argv[1])
print "oth =", get_password_names_other(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment