Skip to content

Utils

timer

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

Source code in Client/Listener/utils.py
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
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 to log the message
            message: str
                the message to log
        """
        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, value, traceback):
        """
        context exit will write this
        """
        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/Listener/utils.py
61
62
63
64
65
66
67
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

Source code in Client/Listener/utils.py
69
70
71
72
73
74
def __exit__(self, context, value, traceback):
    """
    context exit will write this
    """
    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

Logger the logger to log the message

required
message str

str the message to log

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

    Args:
        logger: Logger
            the logger to log the message
        message: str
            the message to log
    """
    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]

str the name of the logger

None
stream bool

bool whether to log in the terminal stream

True
Source code in Client/Listener/utils.py
 9
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
37
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: str
            the name of the logger
        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