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) 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 cache_star_import(func):
def wrapper(evaluator, scope, *args, **kwargs): def wrapper(evaluator, scope, *args, **kwargs):
with common.ignored(KeyError): with common.ignored(KeyError):

View File

@@ -67,23 +67,20 @@ class ModuleWithCursor(object):
self.position = position self.position = position
self._path_until_cursor = None self._path_until_cursor = None
self._line_cache = None self._line_cache = None
self._parser = None
# this two are only used, because there is no nonlocal in Python 2 # this two are only used, because there is no nonlocal in Python 2
self._line_temp = None self._line_temp = None
self._relevant_temp = None self._relevant_temp = None
@property @property
@cache.underscore_memoization
def parser(self): def parser(self):
""" get the parser lazy """ """ get the parser lazy """
if not self._parser: cache.invalidate_star_import_cache(self.path)
cache.invalidate_star_import_cache(self.path) parser = fast.FastParser(self.source, self.path, self.position)
self._parser = fast.FastParser(self.source, self.path, self.position) # Don't pickle that module, because the main module is changing quickly
# don't pickle that module, because the main module is changing cache.save_parser(self.path, self.name, parser, pickling=False)
# quickly usually. return parser
cache.save_parser(self.path, self.name, self._parser,
pickling=False)
return self._parser
def get_path_until_cursor(self): def get_path_until_cursor(self):
""" Get the path under the cursor. """ """ Get the path under the cursor. """