slight efficiency improvement

This commit is contained in:
adam j hartz 2015-03-16 08:46:43 -04:00
parent 90adbaf070
commit 20e698f6f1
2 changed files with 8 additions and 4 deletions

View file

@ -365,12 +365,12 @@ def run_subproc(cmds, captured=True):
suggested = {}
path = ENV.get('PATH',[])
for a in builtins.aliases:
if a not in suggested and levenshtein(a, cmd) < threshold:
if a not in suggested and levenshtein(a, cmd, threshold) < threshold:
suggested[a] = 'Alias'
for d in path:
if os.path.isdir(d):
for f in os.listdir(d):
if f not in suggested and levenshtein(f, cmd) < threshold:
if f not in suggested and levenshtein(f, cmd, threshold) < threshold:
fname = os.path.join(d,f)
suggested[f] = 'Command ({0})'.format(fname)
suggested = OrderedDict(sorted(suggested.items(),

View file

@ -212,11 +212,15 @@ class redirect_stderr(_RedirectStream):
_stream = "stderr"
# Public Domain code, by Magnus Lie Hetland
# Modified from Public Domain code, by Magnus Lie Hetland
# from http://hetland.org/coding/python/levenshtein.py
def levenshtein(a,b):
def levenshtein(a,b,max_dist=float('inf')):
"Calculates the Levenshtein distance between a and b."
n, m = len(a), len(b)
if abs(n-m) > max_dist:
return float('inf')
if n > m:
# Make sure n <= m, to use O(min(n,m)) space
a,b = b,a