From a68b7d2d37dfd2c4fad5bbca22c34ee3ff5e53de Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 10 Nov 2009 20:29:35 -0500 Subject: Some per-instance caching to speed code parsing and analysis. --- coverage/misc.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'coverage/misc.py') diff --git a/coverage/misc.py b/coverage/misc.py index 8e4b3362..329a8417 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -45,6 +45,20 @@ def format_lines(statements, lines): return ret +def expensive(fn): + """A decorator to cache the result of an expensive operation. + + Only applies to methods with no arguments. + + """ + attr = "_cache_" + fn.__name__ + def _wrapped(self): + if not hasattr(self, attr): + setattr(self, attr, fn(self)) + return getattr(self, attr) + return _wrapped + + class CoverageException(Exception): """An exception specific to Coverage.""" pass -- cgit v1.2.1