after tried unsuccessfully while, seeking miraculous website. problem: want create decorator writes elapsed execution time of function (during execution of function) logging file like:
@log_time("log.txt", 35) def some_function(...): ... return result
and
from functools import wraps def log_time(path_to_logfile, interval): ...
so log.txt
like
time elapsed: 0h 0m 35s time elapsed: 0h 1m 10s time elapsed: 0h 1m 45s
any ideas?
i'll give basic overview must in order accomplish this. following decorator accepts 2 parameters, , executes function. missing funcitonality presented comments, add them in:
def log_time(path_to_logfile, interval): def log(func): # 'wrap' puppy if needed def wrapped(*args, **kwargs): # start timing func(*args, **kwargs) # stop timing open(path_to_logfile, 'a') f: pass # functionality return wrapped return log
you can decorate functions , output going written in path_to_logfile
. so, example, decorating foo
here:
@log_time('foo.txt', 40) def foo(i, j): print(i, j) foo(1, 2)
will take foo , execute it. need time
appropriately , write contents file. should experiment decorators more , read on them, nice article on decorators exist @ python wiki.
Comments
Post a Comment