1
0
forked from VimPlug/jedi

add an cache.underscore_memoization decorator to make some recurring patterns easier to read

This commit is contained in:
Dave Halter
2014-01-05 10:37:28 +01:00
parent 4fdfbcd7e4
commit 471cf742dc
2 changed files with 37 additions and 9 deletions

View File

@@ -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):