Skip to content

Instantly share code, notes, and snippets.

@T1T4N
Last active September 14, 2024 09:31
Show Gist options
  • Save T1T4N/22ca7b0764cefe917b2b3a6bb056364c to your computer and use it in GitHub Desktop.
Save T1T4N/22ca7b0764cefe917b2b3a6bb056364c to your computer and use it in GitHub Desktop.
Python conditional decorator
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