Skip to content

Commit 5bc2f7a

Browse files
author
Matthieu Ancellin
committed
Add time decorator.
1 parent 78dadb8 commit 5bc2f7a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

labelled_functions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
from .pipeline import pipeline, compose, let, show, relabel
88
from .maps import pandas_map
99
from .cartesian_products import full_parametric_study
10+
from .decorators import time

labelled_functions/decorators.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
"""Some higher-order functions that make useful tools."""
4+
5+
from labelled_functions.labels import label
6+
7+
8+
# API
9+
10+
def time(func):
11+
func = label(func)
12+
13+
def timed_func(*args, **kwargs):
14+
from time import perf_counter
15+
start = perf_counter()
16+
result = func._output_as_dict(func(*args, **kwargs))
17+
end = perf_counter()
18+
result[f"{func.name}_execution_time"] = end - start
19+
return result
20+
21+
timed_f = label(
22+
timed_func,
23+
_input_names=func._input_names,
24+
output_names=func.output_names + [f"{func.name}_execution_time"],
25+
default_values=func.default_values,
26+
hidden_inputs=func.hidden_inputs,
27+
)
28+
29+
return timed_f

0 commit comments

Comments
 (0)