the code above works fine
but I am wondering wheather it's possible to
write something like this
... def wrapper(*a,**kw):
... logfile.write("started at %s" % time.ctime())
... t0 = time.time()
... f(*a, **kw)
... t1 = time.time()
... logfile.write("ended at %s" % time.ctime())
... logfile.write("diff = %f %s" % (t1-t0, "sec"))
... return wrapper
... def loops(a,b,c):
... sum = 0
... for i in range(a):
... for j in range(b):
... for k in range(c):
... sum += 1
...
(1) fails to compile
is it possible to pass parameters to a decorator function?
Yes, but then the function must return the same kind of thing
a bare decorator-function name would have, which is a function
able to take a single argument of a function and return a function.
So your decorator won't take f as an argument, just the optional logfile,
and it will return a function that does the wrapping like the original decorator.
... def deco(f):
... def wrapper(*a,**kw):
... logfile.write("started at %s\n" % time.ctime())
... t0 = time.time()
... f(*a, **kw)
... t1 = time.time()
... logfile.write("ended at %s\n" % time.ctime())
... logfile.write("diff = %f %s\n" % (t1-t0, "sec"))
... return wrapper
... return deco
... ... def foo(): pass
... started at Sun Jan 08 14:13:55 2006
ended at Sun Jan 08 14:13:55 2006
diff = 0.000000 sec started at Sun Jan 08 14:14:02 2006
ended at Sun Jan 08 14:14:02 2006
diff = 0.000000 sec ... def foo(dt): time.sleep(dt)
... started at Sun Jan 08 14:14:59 2006
ended at Sun Jan 08 14:15:04 2006
diff = 5.007000 sec started at Sun Jan 08 14:15:16 2006
ended at Sun Jan 08 14:15:17 2006
diff = 0.501000 sec
Regards,
Bengt Richter