Skip to content

Instantly share code, notes, and snippets.

@T1T4N
Last active March 14, 2023 01:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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