hello NG,
consider this code
[color=blue][color=green][color=darkred]
>>> def timelogger(f):[/color][/color][/color]
.... def wrapper(*a,**kw ):
.... print "started at %s" % time.ctime()
.... t0 = time.time()
.... f(*a, **kw)
.... t1 = time.time()
.... print "ended at %s" % time.ctime()
.... print "diff = ", t1-t0, "sec"
.... return wrapper
....[color=blue][color=green][color=darkred]
>>> import time
>>> @timelogger[/color][/color][/color]
.... def loops(a,b,c):
.... sum = 0
.... for i in range(a):
.... for j in range(b):
.... for k in range(c):
.... sum += 1
....[color=blue][color=green][color=darkred]
>>> loops[/color][/color][/color]
<function wrapper at 0x402eec34>[color=blue][color=green][color=darkred]
>>> loops(10,10,10)[/color][/color][/color]
started at Sun Jan 8 23:19:19 2006
ended at Sun Jan 8 23:19:19 2006
diff = 0.0003671646118 16 sec
the code above works fine
but I am wondering wheather it's possible to
write something like this
[color=blue][color=green][color=darkred]
>>> def timelogger(f, logfile=sys.std out):[/color][/color][/color]
.... 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
[color=blue][color=green][color=darkred]
>>> import time
>>> @timelogger(fil e("hierher", "a")) ### <<<<<< (1)[/color][/color][/color]
.... 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?
Regards, Daniel
consider this code
[color=blue][color=green][color=darkred]
>>> def timelogger(f):[/color][/color][/color]
.... def wrapper(*a,**kw ):
.... print "started at %s" % time.ctime()
.... t0 = time.time()
.... f(*a, **kw)
.... t1 = time.time()
.... print "ended at %s" % time.ctime()
.... print "diff = ", t1-t0, "sec"
.... return wrapper
....[color=blue][color=green][color=darkred]
>>> import time
>>> @timelogger[/color][/color][/color]
.... def loops(a,b,c):
.... sum = 0
.... for i in range(a):
.... for j in range(b):
.... for k in range(c):
.... sum += 1
....[color=blue][color=green][color=darkred]
>>> loops[/color][/color][/color]
<function wrapper at 0x402eec34>[color=blue][color=green][color=darkred]
>>> loops(10,10,10)[/color][/color][/color]
started at Sun Jan 8 23:19:19 2006
ended at Sun Jan 8 23:19:19 2006
diff = 0.0003671646118 16 sec
the code above works fine
but I am wondering wheather it's possible to
write something like this
[color=blue][color=green][color=darkred]
>>> def timelogger(f, logfile=sys.std out):[/color][/color][/color]
.... 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
[color=blue][color=green][color=darkred]
>>> import time
>>> @timelogger(fil e("hierher", "a")) ### <<<<<< (1)[/color][/color][/color]
.... 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?
Regards, Daniel
Comment