Last active
September 14, 2024 09:31
-
-
Save T1T4N/22ca7b0764cefe917b2b3a6bb056364c to your computer and use it in GitHub Desktop.
Python conditional decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class conditional_decorator(object): | |
def __init__(self, dec, condition): | |
self.decorator = dec | |
self.condition = condition | |
def __call__(self, func): | |
if not self.condition: | |
# Return the function unchanged, not decorated. | |
return func | |
return self.decorator(func) | |
# @functools.lru_cache(maxsize=256, typed=True) | |
@conditional_decorator(lambda func: functools.lru_cache(maxsize=256, typed=True)(func), hasattr(functools, 'lru_cache')) | |
def _compile_pattern(pat): | |
return re.compile(pat).match |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment