From 919ab514bcf2ae6146dfa2baff14bcc9c9843b4b Mon Sep 17 00:00:00 2001 From: Berke Alpaslan <93981165+BerkeAlpaslan@users.noreply.github.com> Date: Tue, 22 Oct 2024 00:31:08 +0300 Subject: [PATCH] Create timer_berke_alpaslan.py --- Week06/timer_berke_alpaslan.py | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Week06/timer_berke_alpaslan.py diff --git a/Week06/timer_berke_alpaslan.py b/Week06/timer_berke_alpaslan.py new file mode 100644 index 00000000..402568a6 --- /dev/null +++ b/Week06/timer_berke_alpaslan.py @@ -0,0 +1,62 @@ +import time + +class Timer: + """ + A context manager that measures the elapsed time of a code block. + + This class allows you to measure the time taken to execute + a block of code using the context management protocol. + It records the start time when entering the context and + the end time when exiting the context, allowing you to + calculate and print the elapsed time. + + Attributes: + start_time (float): The time when the timer starts. + end_time (float): The time when the timer ends. + + Example: + >>> with Timer() as timer: + ... time.sleep(2) + ... + Elapsed time: 2.0000 seconds + """ + def __init__(self): + """ + Initializes the Timer with start and end times set to None. + + Attributes: + start_time (float): The time when the timer starts (initially None). + end_time (float): The time when the timer ends (initially None). + """ + self.start_time = None + self.end_time = None + + def __enter__(self): + """ + Starts the timer by recording the current time. + + Returns: + Timer: The Timer instance, allowing access to start and end times. + """ + self.start_time = time.time() + return self + + def __exit__(self, exc_type, exc_value, traceback): + """ + Stops the timer and calculates the elapsed time. + + This method is called when exiting the context. It calculates + the elapsed time and prints it. + + Args: + exc_type: The exception type (if an exception occurred). + exc_value: The exception value (if an exception occurred). + traceback: The traceback object (if an exception occurred). + + Returns: + bool: Returns False to propagate any exceptions that occurred. + """ + self.end_time = time.time() + elapsed_time = self.end_time - self.start_time + print(f"\n\nElapsed time: {elapsed_time:.4f} seconds") + return False