Created
December 8, 2021 02:11
-
-
Save avcaliani/aa5464715bba407a028d2143dd346ca4 to your computer and use it in GitHub Desktop.
#python #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
import logging as log | |
import sys | |
from functools import wraps | |
from random import choice | |
from typing import Any, List | |
FRUITS = ['π', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π₯', 'π', 'π₯₯', 'π₯', 'π '] | |
SPORTS = ['β½οΈ', 'π', 'π', 'βΎοΈ', 'πΎ', 'π', 'π±'] | |
def debug(func: Any) -> Any: | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
log.debug(f'-------------< func >--------------') | |
log.debug(f'Function ') | |
log.debug(f' βββ {func.__name__}') | |
log.debug(f'Arguments ') | |
for arg in args: | |
log.debug(f' βββ {arg}') | |
log.debug(f' βββ The End') | |
log.debug(f'K. Arguments ') | |
for k, v in kwargs.items(): | |
log.debug(f' βββ {k}: {v}') | |
log.debug(f' βββ The End') | |
log.debug(f'-------------------------------------') | |
return func(*args, **kwargs) | |
return wrapper | |
def repeat(n: int = 4) -> Any: | |
def decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
log.debug(f'Repeating execution {n} times...') | |
result = [] | |
for _ in range(n): | |
result.append(func(*args, **kwargs)) | |
return result | |
return wrapper | |
return decorator | |
@debug | |
def get_emojis(how_many: int, **kwargs) -> List[str]: | |
if kwargs.get('section') == 'fruits': | |
return FRUITS[0:how_many] | |
else: | |
return SPORTS[0:how_many] | |
@debug | |
@repeat(n=3) | |
def pick(values: List[str]) -> str: | |
return choice(values) | |
if __name__ == '__main__': | |
log.basicConfig( | |
format='[%(asctime)s] %(message)s', | |
level=log.DEBUG, | |
datefmt='%Y-%m-%d %H:%M:%S', | |
stream=sys.stdout | |
) | |
emojis = get_emojis(4, section=choice(['sports', 'fruits'])) | |
log.info(f'Emojis Set: {emojis}') | |
picked = pick(emojis) | |
log.info(f'Picked Emoji(s): {picked}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment