Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: tools/telemetry/third_party/rope/rope/base/utils.py

Issue 1132103009: Example of refactoring using rope library. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 import warnings
2
3
4 def saveit(func):
5 """A decorator that caches the return value of a function"""
6
7 name = '_' + func.__name__
8
9 def _wrapper(self, *args, **kwds):
10 if not hasattr(self, name):
11 setattr(self, name, func(self, *args, **kwds))
12 return getattr(self, name)
13 return _wrapper
14
15 cacheit = saveit
16
17
18 def prevent_recursion(default):
19 """A decorator that returns the return value of `default` in recursions"""
20 def decorator(func):
21 name = '_calling_%s_' % func.__name__
22
23 def newfunc(self, *args, **kwds):
24 if getattr(self, name, False):
25 return default()
26 setattr(self, name, True)
27 try:
28 return func(self, *args, **kwds)
29 finally:
30 setattr(self, name, False)
31 return newfunc
32 return decorator
33
34
35 def ignore_exception(exception_class):
36 """A decorator that ignores `exception_class` exceptions"""
37 def _decorator(func):
38 def newfunc(*args, **kwds):
39 try:
40 return func(*args, **kwds)
41 except exception_class:
42 pass
43 return newfunc
44 return _decorator
45
46
47 def deprecated(message=None):
48 """A decorator for deprecated functions"""
49 def _decorator(func, message=message):
50 if message is None:
51 message = '%s is deprecated' % func.__name__
52
53 def newfunc(*args, **kwds):
54 warnings.warn(message, DeprecationWarning, stacklevel=2)
55 return func(*args, **kwds)
56 return newfunc
57 return _decorator
58
59
60 def cached(count):
61 """A caching decorator based on parameter objects"""
62 def decorator(func):
63 return _Cached(func, count)
64 return decorator
65
66
67 class _Cached(object):
68
69 def __init__(self, func, count):
70 self.func = func
71 self.cache = []
72 self.count = count
73
74 def __call__(self, *args, **kwds):
75 key = (args, kwds)
76 for cached_key, cached_result in self.cache:
77 if cached_key == key:
78 return cached_result
79 result = self.func(*args, **kwds)
80 self.cache.append((key, result))
81 if len(self.cache) > self.count:
82 del self.cache[0]
83 return result
OLDNEW
« no previous file with comments | « tools/telemetry/third_party/rope/rope/base/taskhandle.py ('k') | tools/telemetry/third_party/rope/rope/base/worder.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698