changeset 1034:1a9cfb730bc1

New decorator for faking functions.
author Martin Geisler <mg@daimi.au.dk>
date Sun, 30 Nov 2008 23:13:12 +0100
parents dcb0e2794219
children f7d1f7cd3dda
files viff/util.py
diffstat 1 files changed, 22 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/viff/util.py	Sun Nov 30 17:36:39 2008 +0100
+++ b/viff/util.py	Sun Nov 30 23:13:12 2008 +0100
@@ -88,6 +88,28 @@
             return f
         return decorator
 
+def fake(replacement):
+    """Replace a function with a fake version.
+
+    This decorator is meant to be used for profiling where specific
+    functions can be replaced with fake versions that are faster. The
+    fake version should take the same input as the original function
+    and deliver output of the correct type.
+
+    The environment variable :envvar:`VIFF_FAKE` must be set to enable
+    this decorator. The variable is treated as a list of function
+    names, separated on whitespace. Setting :envvar:`VIFF_FAKE` to the
+    special value ``*`` will enable all fake replacements known.
+    """
+
+    def decorator(func):
+        fakes = os.environ.get('VIFF_FAKE', '')
+        if fakes == '*' or func.__name__ in fakes.split():
+            return replacement
+        else:
+            return func
+
+    return decorator
 
 def deprecation(message):
     """Issue a deprecation warning."""