Last active
October 6, 2022 07:43
-
-
Save alpden550/185750bfca009ccefd19a7dd7c2fa8c6 to your computer and use it in GitHub Desktop.
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
from functools import wraps | |
import warnings | |
def deprecated(func): | |
code = func.__code__ | |
warnings.warn_explicit( | |
func.__name__ + 'is deprecated', | |
category=DeprecationWarning, | |
filename=code.co_filename, | |
lineno=code.co_firstlineno + 1) | |
return func |
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
from functools import wraps | |
def once(func): | |
@wraps(func) | |
def inner(*args, **kwargs): | |
if not inner.called: | |
result = func(*args, ** kwargs) | |
inner.called = True | |
return result | |
inner.called = False | |
return inner |
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
from functools import wraps | |
# more contracts https://pypi.org/project/contracts/ | |
def pre(condition, message): | |
def wrapper(func): | |
@wraps(func) | |
def inner(*args, **kwargs): | |
assert condition(*args, **kwargs), message | |
return func(*args, **kwargs) | |
return inner | |
return wrapper |
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
from functools import wraps | |
def logger(filename): | |
def decorator(func): | |
@wraps(func) | |
def wrapped(*args, **kwargs): | |
result = func(*args, **kwargs) | |
with open(filename, 'w') as f: | |
f.write(str(result)) | |
return result | |
return wrapped | |
return 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
from functools import wraps | |
import random | |
import time | |
def wait_random(min_wait=1, max_wait=30): | |
def inner_function(func): | |
@wraps(func) | |
def wrapper(args, **kwargs): | |
time.sleep(random.randint(min_wait, max_wait)) | |
return func(args, **kwargs) | |
return wrapper | |
return inner_function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment