From 9f7600130bbd5c72591ed797bdc16568d4542f6c Mon Sep 17 00:00:00 2001 From: projectdx Date: Sun, 15 May 2022 15:33:25 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9D=B8=ED=94=84=EB=9F=B0=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=EB=AA=85=20=EB=B3=80=EA=B2=BD=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debugger1.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 debugger1.py diff --git a/debugger1.py b/debugger1.py new file mode 100644 index 0000000..6807eac --- /dev/null +++ b/debugger1.py @@ -0,0 +1,69 @@ +import functools +import logging +from functools import wraps +import time + +logging.basicConfig( + level=logging.DEBUG, + format="[%(asctime)s] [%(levelname)s] %(name)s: %(message)s", + datefmt="%Y-%m-%d %H:%M:%S %z", +) +logger = logging.getLogger(__name__) + + +def timerun(func): + """Calculate the execution time of a method and return it back""" + + @functools.wraps(func) + def wrapper(*args, **kwargs): + start = time.time() + result = func(*args, **kwargs) + duration = time.time() - start + + logger.debug(f"Duration of {func.__name__} function was {duration}") + + return result + + return wrapper + + +def yommi_logger( + *, logging_type +): # I prefer keyword-only arugments for decorators, but that is your call... + def decorator(func): + @wraps(func) + def debug(*args, **kwargs): + logger.debug(f"Calling : {func.__name__}") + logger.debug(f"args, kwargs: {args, kwargs}") + start = time.time() + result = func(*args, **kwargs) + duration = time.time() - start + logger.debug(f"{func.__name__} function was {duration}") + return result + + @wraps(func) + def another_option(*args, **kwargs): + print("another option") + return func(*args, **kwargs) + + options = {"debug": debug, "another_option": another_option} + return options[logging_type] + + return decorator + + +class LoggerYommiOld: + def __init__(self, logging_type: str = "debug"): + self.logging_function = getattr(self, logging_type) + + def __call__(self, decorated_function: callable): + def f(*args, **kwargs): + return self.logging_function(decorated_function, *args, **kwargs) + + return f + + def debug(self, decorated_function, *args, **kwargs): + print("starting function") + output = decorated_function(*args, **kwargs) + print("Completing Function") + return output