summaryrefslogtreecommitdiff
path: root/numpy/testing/utils.py
diff options
context:
space:
mode:
authorAlan McIntyre <alan.mcintyre@local>2008-07-15 17:42:50 +0000
committerAlan McIntyre <alan.mcintyre@local>2008-07-15 17:42:50 +0000
commitdc850ae31bf4937eace3f6b62a615a78bac2d73d (patch)
tree1bae1a99a27df2627fb5ecf42bb42095993f9ec4 /numpy/testing/utils.py
parent9ad992f74222d99ed2f66c5e3e9a2a90cdc31cd2 (diff)
downloadnumpy-dc850ae31bf4937eace3f6b62a615a78bac2d73d.tar.gz
Added isfunction and decorate_methods in support of SciPy switching to
use numpy.testing.
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r--numpy/testing/utils.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index ffd67e73f..5e0ef06f3 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -7,12 +7,14 @@ import sys
import re
import difflib
import operator
+from inspect import isfunction
from nosetester import import_nose
__all__ = ['assert_equal', 'assert_almost_equal','assert_approx_equal',
'assert_array_equal', 'assert_array_less', 'assert_string_equal',
'assert_array_almost_equal', 'assert_raises', 'build_err_msg',
- 'jiffies', 'memusage', 'raises', 'rand', 'rundocs', 'runstring']
+ 'decorate_methods', 'jiffies', 'memusage', 'raises', 'rand',
+ 'rundocs', 'runstring']
def rand(*args):
"""Returns an array of random numbers with the given shape.
@@ -327,3 +329,37 @@ def raises(*args,**kwargs):
def assert_raises(*args,**kwargs):
nose = import_nose()
return nose.tools.assert_raises(*args,**kwargs)
+
+def decorate_methods(cls, decorator, testmatch=None):
+ ''' Apply decorator to all methods in class matching testmatch
+
+ Parameters
+ ----------
+ cls : class
+ Class to decorate methods for
+ decorator : function
+ Decorator to apply to methods
+ testmatch : compiled regexp or string to compile to regexp
+ Decorators are applied if testmatch.search(methodname)
+ is not None. Default value is
+ re.compile(r'(?:^|[\\b_\\.%s-])[Tt]est' % os.sep)
+ (the default for nose)
+ '''
+ if testmatch is None:
+ testmatch = re.compile(r'(?:^|[\\b_\\.%s-])[Tt]est' % os.sep)
+ else:
+ testmatch = re.compile(testmatch)
+ cls_attr = cls.__dict__
+ methods = filter(isfunction, cls_attr.values())
+ for function in methods:
+ try:
+ if hasattr(function, 'compat_func_name'):
+ funcname = function.compat_func_name
+ else:
+ funcname = function.__name__
+ except AttributeError:
+ # not a function
+ continue
+ if testmatch.search(funcname) and not funcname.startswith('_'):
+ setattr(cls, funcname, decorator(function))
+ return