Decorating a function will log how long it took to execute each function call Usage: @linetimer() def foo(): pass "Code block 'foo()' took xxx s" @linetimer(show_args=True) def foo(): pass :param show_args: When True, will print the parameters pa
(
show_args=False,
name: str = None,
silent: bool = False,
unit: str = UNIT_SECONDS,
logger_func=None,
threshold: Optional[Union[int, float]] = None
)
| 105 | |
| 106 | # function decorator style |
| 107 | def linetimer( |
| 108 | show_args=False, |
| 109 | name: str = None, |
| 110 | silent: bool = False, |
| 111 | unit: str = UNIT_SECONDS, |
| 112 | logger_func=None, |
| 113 | threshold: Optional[Union[int, float]] = None |
| 114 | ): |
| 115 | """ |
| 116 | Decorating a function will log how long it took to execute each function call |
| 117 | |
| 118 | Usage: |
| 119 | |
| 120 | @linetimer() |
| 121 | def foo(): |
| 122 | pass |
| 123 | |
| 124 | "Code block 'foo()' took xxx s" |
| 125 | |
| 126 | @linetimer(show_args=True) |
| 127 | def foo(): |
| 128 | pass |
| 129 | |
| 130 | :param show_args: When True, will print the parameters passed |
| 131 | into the decorated function |
| 132 | :param name: If None, uses the name of the function and show_args value. |
| 133 | Otherwise, same as CodeTimer. |
| 134 | :param silent: When True, does not print or log any messages |
| 135 | :param unit: Units to measure time. One of ['ns', 'us', 'ms', 's', 'm', 'h'] |
| 136 | :param logger_func: A function that takes a string parameter |
| 137 | that is called at the end of the indented block. |
| 138 | If specified, messages will not be printed to console. |
| 139 | :param threshold: A integer or float value. If time taken by code block |
| 140 | took greater than or equal value, only then log. |
| 141 | If None, will bypass this parameter. |
| 142 | :return: CodeTimer decorated function |
| 143 | """ |
| 144 | |
| 145 | def decorator(func): |
| 146 | |
| 147 | def wrapper(*args, **kwargs): |
| 148 | |
| 149 | if name is None: |
| 150 | if show_args: |
| 151 | block_name = func.__name__ + '(' |
| 152 | |
| 153 | def to_str(val): |
| 154 | return [val].__str__()[1:-1] |
| 155 | |
| 156 | # append args |
| 157 | if len(args) > 0: |
| 158 | block_name += ', '.join([to_str(arg) for arg in args]) |
| 159 | |
| 160 | if len(kwargs.keys()) > 0: |
| 161 | block_name += ', ' |
| 162 | |
| 163 | # append kwargs |
| 164 | if len(kwargs.keys()) > 0: |
nothing calls this directly
no outgoing calls
no test coverage detected