forked from VimPlug/jedi
add an cache.underscore_memoization decorator to make some recurring patterns easier to read
This commit is contained in:
@@ -102,6 +102,37 @@ def cache_call_signatures(stmt):
|
||||
return None if module_path is None else (module_path, stmt.start_pos)
|
||||
|
||||
|
||||
def underscore_memoization(func):
|
||||
"""
|
||||
Decorator for methods::
|
||||
|
||||
class A(object):
|
||||
def x(self):
|
||||
if self._x:
|
||||
self._x = 10
|
||||
return self._x
|
||||
|
||||
Becomes::
|
||||
|
||||
class A(object):
|
||||
@underscore_memoization
|
||||
def x(self):
|
||||
return 10
|
||||
|
||||
A now has an attribute ``_x`` written by this decorator.
|
||||
"""
|
||||
def wrapper(self):
|
||||
name = '_' + func.__name__
|
||||
try:
|
||||
return getattr(self, name)
|
||||
except AttributeError:
|
||||
result = func(self)
|
||||
setattr(self, name, result)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def cache_star_import(func):
|
||||
def wrapper(evaluator, scope, *args, **kwargs):
|
||||
with common.ignored(KeyError):
|
||||
|
||||
Reference in New Issue
Block a user