mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-07 13:41:21 +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:
parent
b0702d309f
commit
51f1f44162
@ -17,6 +17,23 @@ else:
|
|||||||
from shlex import quote
|
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):
|
def which(program):
|
||||||
"""Returns `program` path or `None`."""
|
"""Returns `program` path or `None`."""
|
||||||
|
|
||||||
@ -55,22 +72,6 @@ def wrap_settings(params):
|
|||||||
return decorator
|
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):
|
def get_closest(word, possibilities, n=3, cutoff=0.6, fallback_to_first=True):
|
||||||
"""Returns closest match or just first from possibilities."""
|
"""Returns closest match or just first from possibilities."""
|
||||||
possibilities = list(possibilities)
|
possibilities = list(possibilities)
|
||||||
@ -94,7 +95,7 @@ def get_all_executables():
|
|||||||
tf_alias = thefuck_alias()
|
tf_alias = thefuck_alias()
|
||||||
return [exe.name
|
return [exe.name
|
||||||
for path in os.environ.get('PATH', '').split(':')
|
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)] + [
|
if not _safe(exe.is_dir, True)] + [
|
||||||
alias for alias in get_aliases() if alias != tf_alias]
|
alias for alias in get_aliases() if alias != tf_alias]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user