cornsnake.util_time
Functions for timing operations. The start_timer
function starts a timer, end_timer
ends the timer and calculates elapsed time, and describe_elapsed_seconds
describes the elapsed time in seconds.
1""" 2Functions for timing operations. The `start_timer` function starts a timer, `end_timer` ends the timer and calculates elapsed time, and `describe_elapsed_seconds` describes the elapsed time in seconds. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_time.html) 5""" 6 7from datetime import timedelta 8import time 9 10 11def start_timer() -> float: 12 """ 13 Function to start a timer. 14 15 Returns: 16 float: The current time when the timer is started. 17 """ 18 return time.time() 19 20 21def end_timer(start: float) -> float: 22 """ 23 Function to end the timer and calculate elapsed time. 24 25 Args: 26 start (float): The start time obtained from `start_timer` function. 27 28 Returns: 29 float: The number of seconds elapsed since the timer started. 30 """ 31 end = time.time() 32 seconds_elapsed = end - start 33 return seconds_elapsed 34 35 36def describe_elapsed_seconds(seconds_elapsed: float) -> str: 37 """ 38 Function to describe the elapsed time in hours/minutes/seconds. 39 40 Args: 41 seconds_elapsed (float): The number of seconds elapsed. 42 43 Returns: 44 str: A formatted string representing the elapsed time in hours/minutes/seconds. 45 """ 46 if seconds_elapsed is None: 47 return "(unknown)" 48 return f"{timedelta(seconds=round(seconds_elapsed))}s"
def
start_timer() -> float:
12def start_timer() -> float: 13 """ 14 Function to start a timer. 15 16 Returns: 17 float: The current time when the timer is started. 18 """ 19 return time.time()
Function to start a timer.
Returns: float: The current time when the timer is started.
def
end_timer(start: float) -> float:
22def end_timer(start: float) -> float: 23 """ 24 Function to end the timer and calculate elapsed time. 25 26 Args: 27 start (float): The start time obtained from `start_timer` function. 28 29 Returns: 30 float: The number of seconds elapsed since the timer started. 31 """ 32 end = time.time() 33 seconds_elapsed = end - start 34 return seconds_elapsed
Function to end the timer and calculate elapsed time.
Args:
start (float): The start time obtained from start_timer
function.
Returns: float: The number of seconds elapsed since the timer started.
def
describe_elapsed_seconds(seconds_elapsed: float) -> str:
37def describe_elapsed_seconds(seconds_elapsed: float) -> str: 38 """ 39 Function to describe the elapsed time in hours/minutes/seconds. 40 41 Args: 42 seconds_elapsed (float): The number of seconds elapsed. 43 44 Returns: 45 str: A formatted string representing the elapsed time in hours/minutes/seconds. 46 """ 47 if seconds_elapsed is None: 48 return "(unknown)" 49 return f"{timedelta(seconds=round(seconds_elapsed))}s"
Function to describe the elapsed time in hours/minutes/seconds.
Args: seconds_elapsed (float): The number of seconds elapsed.
Returns: str: A formatted string representing the elapsed time in hours/minutes/seconds.