MCPcopy Create free account
hub / github.com/numpy/numpy / decorate_methods

Function decorate_methods

numpy/testing/_private/utils.py:1319–1363  ·  view source on GitHub ↗

Apply a decorator to all methods in a class matching a regular expression. The given decorator is applied to all public methods of `cls` that are matched by the regular expression `testmatch` (``testmatch.search(methodname)``). Methods that are private, i.e. start with an under

(cls, decorator, testmatch=None)

Source from the content-addressed store, hash-verified

1317
1318
1319def decorate_methods(cls, decorator, testmatch=None):
1320 """
1321 Apply a decorator to all methods in a class matching a regular expression.
1322
1323 The given decorator is applied to all public methods of `cls` that are
1324 matched by the regular expression `testmatch`
1325 (``testmatch.search(methodname)``). Methods that are private, i.e. start
1326 with an underscore, are ignored.
1327
1328 Parameters
1329 ----------
1330 cls : class
1331 Class whose methods to decorate.
1332 decorator : function
1333 Decorator to apply to methods
1334 testmatch : compiled regexp or str, optional
1335 The regular expression. Default value is None, in which case the
1336 nose default (``re.compile(r'(?:^|[\\b_\\.%s-])[Tt]est' % os.sep)``)
1337 is used.
1338 If `testmatch` is a string, it is compiled to a regular expression
1339 first.
1340
1341 """
1342 if testmatch is None:
1343 testmatch = re.compile(r'(?:^|[\\b_\\.%s-])[Tt]est' % os.sep)
1344 else:
1345 testmatch = re.compile(testmatch)
1346 cls_attr = cls.__dict__
1347
1348 # delayed import to reduce startup time
1349 from inspect import isfunction
1350
1351 methods = [_m for _m in cls_attr.values() if isfunction(_m)]
1352 for function in methods:
1353 try:
1354 if hasattr(function, 'compat_func_name'):
1355 funcname = function.compat_func_name
1356 else:
1357 funcname = function.__name__
1358 except AttributeError:
1359 # not a function
1360 continue
1361 if testmatch.search(funcname) and not funcname.startswith('_'):
1362 setattr(cls, funcname, decorator(function))
1363 return
1364
1365
1366def measure(code_str, times=1, label=None):

Callers

nothing calls this directly

Calls 5

isfunctionFunction · 0.90
valuesMethod · 0.80
startswithMethod · 0.80
decoratorFunction · 0.70
compileMethod · 0.45

Tested by

no test coverage detected