From 049b11e7e4f54effeb020ba1ca8698f44b979f12 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] --- v2/util/__init__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/v2/util/__init__.py b/v2/util/__init__.py index e64c0fc5e..5cee29f55 100644 --- a/v2/util/__init__.py +++ b/v2/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