1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-31 10:11:14 +00:00

Memoize thefuck.utils.which

It is used by some rules to determine if they should be enabled by
default and searches in the $PATH, which can be quiet slow.
This commit is contained in:
mcarton 2015-08-26 19:55:40 +02:00
parent b0702d309f
commit 51f1f44162

View File

@ -17,6 +17,23 @@ else:
from shlex import quote
def memoize(fn):
"""Caches previous calls to the function."""
memo = {}
@wraps(fn)
def wrapper(*args, **kwargs):
key = pickle.dumps((args, kwargs))
if key not in memo or memoize.disabled:
memo[key] = fn(*args, **kwargs)
return memo[key]
return wrapper
memoize.disabled = False
@memoize
def which(program):
"""Returns `program` path or `None`."""
@ -55,22 +72,6 @@ def wrap_settings(params):
return decorator
def memoize(fn):
"""Caches previous calls to the function."""
memo = {}
@wraps(fn)
def wrapper(*args, **kwargs):
key = pickle.dumps((args, kwargs))
if key not in memo or memoize.disabled:
memo[key] = fn(*args, **kwargs)
return memo[key]
return wrapper
memoize.disabled = False
def get_closest(word, possibilities, n=3, cutoff=0.6, fallback_to_first=True):
"""Returns closest match or just first from possibilities."""
possibilities = list(possibilities)
@ -94,7 +95,7 @@ def get_all_executables():
tf_alias = thefuck_alias()
return [exe.name
for path in os.environ.get('PATH', '').split(':')
for exe in _safe(lambda: list(Path(path).iterdir()), [])
for exe in _safe(Path(path).iterdir, [])
if not _safe(exe.is_dir, True)] + [
alias for alias in get_aliases() if alias != tf_alias]