@@ -1359,61 +1359,53 @@ def get_filename():
1359
1359
return f ' profile- { time_str} .png '
1360
1360
```
1361
1361
1362
- #### Decorator for timing function execution :
1362
+ #### Decorator for timing functions :
1363
1363
``` python
1364
1364
from timeit import default_timer
1365
1365
from datetime import timedelta
1366
1366
1367
1367
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 ):
1370
1370
start = default_timer()
1371
- result = func(* args, ** kw )
1371
+ result = func(* args, ** kwargs )
1372
1372
delta = timedelta(seconds = (default_timer() - start))
1373
- print (f " Function { func.__name__ } finished in { delta} " )
1373
+ print (f ' Function { func.__name__ } finished in { delta} ' )
1374
1374
return result
1375
- return wrap
1375
+ return out
1376
1376
```
1377
1377
1378
1378
#### Decorator for profiling functions:
1379
1379
``` python
1380
- import cProfile
1380
+ from cProfile import Profile
1381
+ from pstats import Stats
1381
1382
1382
1383
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()
1387
1387
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' )
1391
1391
stats.print_stats(20 )
1392
1392
print (f " Profile saved as 'profile_ { func.__name__ } .txt' " )
1393
1393
return result
1394
- return wrap
1394
+ return out
1395
1395
```
1396
1396
1397
1397
#### Decorator for function tracing:
1398
1398
``` python
1399
1399
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 ):
1402
1402
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} ' )
1415
1407
return result
1416
- return traced_func
1408
+ return out
1417
1409
```
1418
1410
1419
1411
0 commit comments