Skip to content

Utils

timer

util function used to log the time taken by a part of program

Source code in Client/Responder/utils.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class timer:
    """
    util function used to log the time taken by a part of program
    """

    def __init__(self, logger: Logger, message: str):
        """
        init the timer

        Args:
            logger (Logger): the logger
            message (str): the message to be logged
        """
        self.message = message
        self.logger = logger
        self.start = 0
        self.duration = 0
        self.sub_timers = []

    def __enter__(self):
        """
        context enter to start write this
        """
        self.start = time.time()
        self.logger.info("Starting %s" % self.message)
        return self

    def __exit__(
        self,
        context: Optional[Type[BaseException]],
        value: Optional[BaseException],
        traceback: Optional[TracebackType],
    ):
        """
        context exit will write this

        Args:
            context (Optional[Type[BaseException]]): the context
            value (Optional[BaseException]): the value
            traceback (Optional[TracebackType]): the traceback
        """
        self.duration = time.time() - self.start
        self.logger.info(f"Finished {self.message}, that took {self.duration:.3f}")

__enter__()

context enter to start write this

Source code in Client/Responder/utils.py
58
59
60
61
62
63
64
def __enter__(self):
    """
    context enter to start write this
    """
    self.start = time.time()
    self.logger.info("Starting %s" % self.message)
    return self

__exit__(context, value, traceback)

context exit will write this

Parameters:

Name Type Description Default
context Optional[Type[BaseException]]

the context

required
value Optional[BaseException]

the value

required
traceback Optional[TracebackType]

the traceback

required
Source code in Client/Responder/utils.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def __exit__(
    self,
    context: Optional[Type[BaseException]],
    value: Optional[BaseException],
    traceback: Optional[TracebackType],
):
    """
    context exit will write this

    Args:
        context (Optional[Type[BaseException]]): the context
        value (Optional[BaseException]): the value
        traceback (Optional[TracebackType]): the traceback
    """
    self.duration = time.time() - self.start
    self.logger.info(f"Finished {self.message}, that took {self.duration:.3f}")

__init__(logger, message)

init the timer

Parameters:

Name Type Description Default
logger Logger

the logger

required
message str

the message to be logged

required
Source code in Client/Responder/utils.py
44
45
46
47
48
49
50
51
52
53
54
55
56
def __init__(self, logger: Logger, message: str):
    """
    init the timer

    Args:
        logger (Logger): the logger
        message (str): the message to be logged
    """
    self.message = message
    self.logger = logger
    self.start = 0
    self.duration = 0
    self.sub_timers = []

get_logger(logger_name=None, stream=True)

init the logger, give it proper format, log them both in terminal stream and file

Parameters:

Name Type Description Default
logger_name Optional[str]

the logger name

None
stream bool

whether to log in the terminal stream

True
Source code in Client/Responder/utils.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def get_logger(logger_name: Optional[str] = None, stream: bool = True):
    """
    init the logger, give it proper format, log them both in terminal stream and file

    Args:
        logger_name (Optional[str]): the logger name
        stream (bool): whether to log in the terminal stream
    """
    logging.basicConfig(
        format="%(name)s: %(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s",
        datefmt="%Y-%m-%d:%H:%M:%S",
        level=logging.INFO,
    )

    logger = logging.getLogger(logger_name)
    logger.setLevel(logging.INFO)
    logger.propagate = False
    formatter = logging.Formatter(
        "CLIENT: %(name)s | %(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s",
    )
    if not logger.hasHandlers() and stream:
        stdout_handler = logging.StreamHandler()
        stdout_handler.setFormatter(formatter)
        stdout_handler.setLevel(logging.INFO)
        logger.addHandler(stdout_handler)

    return logger