diff options
| author | Scott Gasch <[email protected]> | 2021-07-08 19:44:27 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2021-07-08 19:44:27 -0700 |
| commit | 3bc4daf1edc121cd633429187392227f2fa61885 (patch) | |
| tree | 0663cf35f562c7023c914454c85050d502ad9f3c /decorator_utils.py | |
| parent | 5fd30ef12c100cbb936aa0fdb515b67cff4064db (diff) | |
Lots of changes.
Diffstat (limited to 'decorator_utils.py')
| -rw-r--r-- | decorator_utils.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/decorator_utils.py b/decorator_utils.py index 03e7c88..c07023b 100644 --- a/decorator_utils.py +++ b/decorator_utils.py @@ -5,6 +5,7 @@ import datetime import enum import functools +import inspect import logging import math import multiprocessing @@ -20,6 +21,7 @@ import warnings import exceptions import thread_utils + logger = logging.getLogger(__name__) @@ -517,3 +519,19 @@ def call_with_sample_rate(sample_rate: float) -> Callable: ) return _call_with_sample_rate return decorator + + +def decorate_matching_methods_with(decorator, acl=None): + """Apply decorator to all methods in a class whose names begin with + prefix. If prefix is None (default), decorate all methods in the + class. + """ + def decorate_the_class(cls): + for name, m in inspect.getmembers(cls, inspect.isfunction): + if acl is None: + setattr(cls, name, decorator(m)) + else: + if acl(name): + setattr(cls, name, decorator(m)) + return cls + return decorate_the_class |
