Skip to content

Commit f92fdd2

Browse files
author
ArcHound
committed
feat: exceptions in timing
1 parent 93716ea commit f92fdd2

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

‎{{ cookiecutter.project_slug }}/{{ cookiecutter.project_slug }}.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import logging
77
import sys
88
import time
9+
import math
910
from functools import update_wrapper
1011

1112
import click
@@ -28,6 +29,7 @@
2829
)
2930
log = logging.getLogger(__name__)
3031

32+
3133
log_levels = {
3234
"DEBUG": logging.DEBUG,
3335
"INFO": logging.INFO,
@@ -36,6 +38,7 @@
3638
"CRITICAL": logging.CRITICAL,
3739
}
3840

41+
3942
def log_decorator(f):
4043
@click.pass_context
4144
def new_func(ctx, *args, **kwargs):
@@ -44,18 +47,29 @@ def new_func(ctx, *args, **kwargs):
4447
r = ctx.invoke(f, *args, **kwargs)
4548
log.info("Finishing")
4649
return r
50+
4751
return update_wrapper(new_func, f)
4852

53+
4954
def time_decorator(f):
5055
@click.pass_context
5156
def new_func(ctx, *args, **kwargs):
5257
t1 = time.perf_counter()
53-
r = ctx.invoke(f, *args, **kwargs)
54-
t2 = time.perf_counter()
55-
log.info(f"Execution in {t2-t1:0.4f} seconds")
56-
return r
58+
try:
59+
r = ctx.invoke(f, *args, **kwargs)
60+
return r
61+
except Exception as e:
62+
raise e
63+
finally:
64+
t2 = time.perf_counter()
65+
mins = math.floor(t2-t1) // 60
66+
hours = mins // 60
67+
secs = (t2-t1) - 60 * mins - 3600 * hours
68+
log.info(f"Execution in {hours:02d}:{mins:02d}:{secs:0.4f}")
69+
5770
return update_wrapper(new_func, f)
5871

72+
5973
@click.command()
6074
{%- if cookiecutter.file_input == "Yes" %}
6175
@click.option(

0 commit comments

Comments
 (0)