Skip to content

Timer

timer

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

Source code in API/authenticate/utils/timer.py
 5
 6
 7
 8
 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
38
39
40
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

        Parameters
        ----------
        logger: Logger
            logger to write the logs
        message: str
            message to log, like start xxx
        """
        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 API/authenticate/utils/timer.py
27
28
29
30
31
32
33
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 API/authenticate/utils/timer.py
35
36
37
38
39
40
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

logger: Logger logger to write the logs message: str message to log, like start xxx

Source code in API/authenticate/utils/timer.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def __init__(self, logger: Logger, message: str):
    """
    init the timer

    Parameters
    ----------
    logger: Logger
        logger to write the logs
    message: str
        message to log, like start xxx
    """
    self.message = message
    self.logger = logger
    self.start = 0
    self.duration = 0
    self.sub_timers = []