Skip to content

Timer

timer

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

Source code in Docs2KG/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
class timer:
    """
    util function used to log the time taken by a part of program
    """

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

        Args:
            the_logger (Logger): logger object
            message (str): message to be logged


        """
        self.message = message
        self.logger = the_logger
        self.start = 0
        self.duration = 0
        self.sub_timers = []

    def __enter__(self):
        """
        context enters to start to 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 enters to start to write this

Source code in Docs2KG/utils/timer.py
26
27
28
29
30
31
32
def __enter__(self):
    """
    context enters to start to 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 Docs2KG/utils/timer.py
34
35
36
37
38
39
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__(the_logger, message)

init the timer

Parameters:

Name Type Description Default
the_logger Logger

logger object

required
message str

message to be logged

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

    Args:
        the_logger (Logger): logger object
        message (str): message to be logged


    """
    self.message = message
    self.logger = the_logger
    self.start = 0
    self.duration = 0
    self.sub_timers = []