mafw.timer
Module implements a simple timer to measure the execution duration.
Basic usage:
from mafw import timer
with Timer() as timer:
do_long_lasting_operation()
When exiting from the context manager, a message with the duration of the process is printed.
Functions
|
Return a formatted version of the duration with increased human readability. |
|
Utility function to replace a substring in a given string a certain number of times starting from the right-most one. |
Classes
|
The timer class. |
- class mafw.timer.Timer(suppress_message: bool = False)[source]
Bases:
objectThe timer class.
Constructor parameter:
- Parameters:
suppress_message (bool) – A boolean flag to mute the timer
- format_duration() str[source]
Nicely format the timer duration.
- Returns:
A string with the timer duration in a human-readable formatted string
- Return type:
str
- property duration: float
The elapsed time of the timer.
- Returns:
Elapsed time in seconds.
- mafw.timer.pretty_format_duration(duration_s: float, n_digits: int = 1) str[source]
Return a formatted version of the duration with increased human readability.
- Parameters:
duration_s (float) – The duration to be printed in seconds. If negative, a ValueError exception is raised.
n_digits (int, Optional) – The number of decimal digits to show. Defaults to 1. If negative, a ValueError exception is raised.
- Returns:
The formatted string.
- Return type:
str
- Raises:
ValueError – if a negative duration or a negative number of digits is provided
- mafw.timer.rreplace(inp_string: str, old_string: str, new_string: str, counts: int) str[source]
Utility function to replace a substring in a given string a certain number of times starting from the right-most one.
This function is mimicking the behavior of the string.replace method, but instead of replacing from the left, it is replacing from the right.
- Parameters:
inp_string (str) – The input string
old_string (str) – The old substring to be matched. If empty, a ValueError is raised.
new_string (str) – The new substring to be replaced
counts (int) – The number of times the old substring has to be replaced.
- Returns:
The modified string
- Return type:
str
- Raises:
ValueError – if old_string is empty.