Skip to content

Commit 6c4d90e

Browse files
committed
decorador clock
1 parent eed298a commit 6c4d90e

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

decorator/clockdeco.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# clockdeco.py
2+
3+
import time
4+
5+
6+
def clock(func):
7+
def clocked(*args):
8+
t0 = time.time()
9+
result = func(*args)
10+
elapsed = time.time() - t0
11+
name = func.__name__
12+
arg_str = ', '.join(repr(arg) for arg in args)
13+
print('[%0.8fs] %s(%s) -> %r' % (elapsed, name, arg_str, result))
14+
return result
15+
return clocked

decorator/clockdeco_cls.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# clockdeco_param.py
2+
3+
"""
4+
>>> snooze(.1) # doctest: +ELLIPSIS
5+
[0.101...s] snooze(0.1) -> None
6+
>>> clock('{name}: {elapsed}')(time.sleep)(.2) # doctest: +ELLIPSIS
7+
sleep: 0.20...
8+
>>> clock('{name}({args}) dt={elapsed:0.3f}s')(time.sleep)(.2)
9+
sleep(0.2) dt=0.201s
10+
"""
11+
12+
# BEGIN CLOCKDECO_CLS
13+
import time
14+
15+
DEFAULT_FMT = '[{elapsed:0.8f}s] {name}({args}) -> {result}'
16+
17+
class clock:
18+
19+
def __init__(self, fmt=DEFAULT_FMT):
20+
self.fmt = fmt
21+
22+
def __call__(self, func):
23+
def clocked(*_args):
24+
t0 = time.time()
25+
_result = func(*_args)
26+
elapsed = time.time() - t0
27+
name = func.__name__
28+
args = ', '.join(repr(arg) for arg in _args)
29+
result = repr(_result)
30+
print(self.fmt.format(**locals()))
31+
return _result
32+
return clocked
33+
34+
if __name__ == '__main__':
35+
36+
@clock()
37+
def snooze(seconds):
38+
time.sleep(seconds)
39+
40+
for i in range(3):
41+
snooze(.123)
42+
43+
# END CLOCKDECO_CLS

decorator/clockdeco_demo.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# clockdeco_demo.py
2+
3+
import time
4+
from clockdeco import clock
5+
6+
@clock
7+
def snooze(seconds):
8+
time.sleep(seconds)
9+
10+
@clock
11+
def factorial(n):
12+
return 1 if n < 2 else n*factorial(n-1)
13+
14+
if __name__=='__main__':
15+
print('*' * 40, 'Calling snooze(.123)')
16+
snooze(.123)
17+
print('*' * 40, 'Calling factorial(6)')
18+
print('6! =', factorial(6))

decorator/clockdeco_param.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# clockdeco_param.py
2+
3+
"""
4+
>>> snooze(.1) # doctest: +ELLIPSIS
5+
[0.101...s] snooze(0.1) -> None
6+
>>> clock('{name}: {elapsed}')(time.sleep)(.2) # doctest: +ELLIPSIS
7+
sleep: 0.20...
8+
>>> clock('{name}({args}) dt={elapsed:0.3f}s')(time.sleep)(.2)
9+
sleep(0.2) dt=0.201s
10+
"""
11+
12+
# BEGIN CLOCKDECO_PARAM
13+
import time
14+
15+
DEFAULT_FMT = '[{elapsed:0.8f}s] {name}({args}) -> {result}'
16+
17+
def clock(fmt=DEFAULT_FMT): # <1>
18+
def decorate(func): # <2>
19+
def clocked(*_args): # <3>
20+
t0 = time.time()
21+
_result = func(*_args) # <4>
22+
elapsed = time.time() - t0
23+
name = func.__name__
24+
args = ', '.join(repr(arg) for arg in _args) # <5>
25+
result = repr(_result) # <6>
26+
print(fmt.format(**locals())) # <7>
27+
return _result # <8>
28+
return clocked # <9>
29+
return decorate # <10>
30+
31+
if __name__ == '__main__':
32+
33+
@clock() # <11>
34+
def snooze(seconds):
35+
time.sleep(seconds)
36+
37+
for i in range(3):
38+
snooze(.123)
39+
40+
# END CLOCKDECO_PARAM

decorator/clockdeco_param_demo1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import time
2+
from clockdeco_param import clock
3+
4+
@clock('{name}: {elapsed}s')
5+
def snooze(seconds):
6+
time.sleep(seconds)
7+
8+
for i in range(3):
9+
snooze(.123)

decorator/clockdeco_param_demo2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import time
2+
from clockdeco_param import clock
3+
4+
@clock('{name}({args}) dt={elapsed:0.3f}s')
5+
def snooze(seconds):
6+
time.sleep(seconds)
7+
8+
for i in range(3):
9+
snooze(.123)

0 commit comments

Comments
 (0)