From 9859bcab15c7d4a8bbc4f7d9ddf3cb288eda03da Mon Sep 17 00:00:00 2001 From: Vladimir Prus Date: Mon, 26 Jul 2010 08:07:07 +0000 Subject: [PATCH] Add decorator for caching function results. [SVN r64350] --- src/util/__init__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/util/__init__.py b/src/util/__init__.py index e64c0fc5e..5cee29f55 100644 --- a/src/util/__init__.py +++ b/src/util/__init__.py @@ -1,4 +1,5 @@ +# Decorator the specifies bjam-side prototype for a Python function def bjam_signature(s): def wrap(f): @@ -6,3 +7,23 @@ def bjam_signature(s): return f return wrap + +class cached(object): + + def __init__(self, function): + self.function = function + self.cache = {} + + def __call__(self, *args): + try: + return self.cache[args] + except KeyError: + v = self.function(*args) + self.cache[args] = v + return v + +def unquote(s): + if s and s[0] == '"' and s[-1] == '"': + return s[1:-1] + else: + return s