From db364bc44d91fc68bbad601e8367157956328ff5 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 30 Mar 2017 01:52:01 +0200 Subject: [PATCH] Move underscore memoization. --- jedi/cache.py | 36 +++++++++++++++++++++++++++++++++++- jedi/parser/utils.py | 35 ----------------------------------- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/jedi/cache.py b/jedi/cache.py index 0d662aff..4b5dac5e 100644 --- a/jedi/cache.py +++ b/jedi/cache.py @@ -12,14 +12,48 @@ there are global variables, which are holding the cache information. Some of these variables are being cleaned after every API usage. """ import time +import inspect from jedi import settings from jedi.parser.utils import parser_cache -from jedi.parser.utils import underscore_memoization _time_caches = {} +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. + """ + name = '_' + func.__name__ + + def wrapper(self): + try: + return getattr(self, name) + except AttributeError: + result = func(self) + if inspect.isgenerator(result): + result = list(result) + setattr(self, name, result) + return result + + return wrapper + + def clear_time_caches(delete_all=False): """ Jedi caches many things, that should be completed after each completion finishes. diff --git a/jedi/parser/utils.py b/jedi/parser/utils.py index c3b741d2..29ed4d76 100644 --- a/jedi/parser/utils.py +++ b/jedi/parser/utils.py @@ -1,4 +1,3 @@ -import inspect import time import os import sys @@ -45,40 +44,6 @@ parser_cache = {} -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. - """ - name = '_' + func.__name__ - - def wrapper(self): - try: - return getattr(self, name) - except AttributeError: - result = func(self) - if inspect.isgenerator(result): - result = list(result) - setattr(self, name, result) - return result - - return wrapper - - class _NodeCacheItem(object): def __init__(self, node, lines, change_time=None): self.node = node