Skip to content

Commit 5ba1688

Browse files
committed
Claned profiling decorators
1 parent 8b3e1f4 commit 5ba1688

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

README.md

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,61 +1359,53 @@ def get_filename():
13591359
return f'profile-{time_str}.png'
13601360
```
13611361

1362-
#### Decorator for timing function execution:
1362+
#### Decorator for timing functions:
13631363
```python
13641364
from timeit import default_timer
13651365
from datetime import timedelta
13661366

13671367
def stopwatch(func):
1368-
"""Print runtime of decorated function."""
1369-
def wrap(*args, **kw):
1368+
"""Prints runtime of decorated function."""
1369+
def out(*args, **kwargs):
13701370
start = default_timer()
1371-
result = func(*args, **kw)
1371+
result = func(*args, **kwargs)
13721372
delta = timedelta(seconds=(default_timer() - start))
1373-
print(f"Function {func.__name__} finished in {delta}")
1373+
print(f'Function {func.__name__} finished in {delta}')
13741374
return result
1375-
return wrap
1375+
return out
13761376
```
13771377

13781378
#### Decorator for profiling functions:
13791379
```python
1380-
import cProfile
1380+
from cProfile import Profile
1381+
from pstats import Stats
13811382

13821383
def profiler(func):
1383-
"""Decorator.
1384-
Create a run call profile of the decorated function."""
1385-
def wrap(*args, **kwargs):
1386-
profile = cProfile.Profile()
1384+
"""Saves run call profile of the decorated function to file."""
1385+
def out(*args, **kwargs):
1386+
profile = Profile()
13871387
result = profile.runcall(func, *args, **kwargs)
1388-
with open(f"profile_{func.__name__}.txt", "w") as stream:
1389-
stats = pstats.Stats(profile, stream=stream)
1390-
stats.strip_dirs().sort_stats("tottime")
1388+
with open(f'profile_{func.__name__}.txt', 'w') as stream:
1389+
stats = Stats(profile, stream=stream)
1390+
stats.strip_dirs().sort_stats('tottime')
13911391
stats.print_stats(20)
13921392
print(f"Profile saved as 'profile_{func.__name__}.txt'")
13931393
return result
1394-
return wrap
1394+
return out
13951395
```
13961396

13971397
#### Decorator for function tracing:
13981398
```python
13991399
def tracer(func):
1400-
"""Print a trace of the input and output of a function in one line."""
1401-
def traced_func(*args, **kwargs):
1400+
"""Prints input and output of a decorated function."""
1401+
def out(*args, **kwargs):
14021402
result = func(*args, **kwargs)
1403-
if len(args) is not 0:
1404-
argslist = ", ".join(f"{x}" for x in args)
1405-
if len(kwargs) is not 0:
1406-
argslist = argslist + ", " if len(kwargs) is not 0 else ""
1407-
else:
1408-
argslist = ""
1409-
if len(kwargs) is not 0:
1410-
kwargslist = ", ".join([f"{k}={v}" for k, v in kwargs.items()])
1411-
else:
1412-
kwargslist = ""
1413-
print(
1414-
f"{func.__name__}({argslist}{kwargslist}) = {result}")
1403+
arg_list = [str(x) for x in args]
1404+
arg_list += [f'{k}={v}' for k, v in kwargs.items()]
1405+
arg_str = ', '.join(arg_list)
1406+
print(f'{func.__name__}({arg_str}) = {result}')
14151407
return result
1416-
return traced_func
1408+
return out
14171409
```
14181410

14191411

0 commit comments

Comments
 (0)