472,145 Members | 1,432 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Creating logged functions using decorators

Hi,

I m thinking about writing some code which logs the input and output
of a function/script and stores it in a database using sqlalchemy
(although I havent started on this yet). I want to do this via a
decorator ( I think this is the best way ).

def log(fn):
def newfn(*args):
print datetime.date.today()
print __file__
print fn.__name__
print args
return fn(*args)
return newfn

@log
def doAnalysis(a, b):
print a, b
return a + b

doAnalysis(3, 7)

I can access the arguments passed to the "logged" function, but is
there anyway I can capture the output of the function i.e. 10.

Many Thanks in advance,

Nathan
Jul 7 '07 #1
2 1058
On Jul 7, 1:47 pm, "Nathan Harmston" <ratchetg...@googlemail.com>
wrote:
Hi,

I m thinking about writing some code which logs the input and output
of a function/script and stores it in a database using sqlalchemy
(although I havent started on this yet). I want to do this via a
decorator ( I think this is the best way ).

def log(fn):
def newfn(*args):
print datetime.date.today()
print __file__
print fn.__name__
print args
return fn(*args)
return newfn

@log
def doAnalysis(a, b):
print a, b
return a + b

doAnalysis(3, 7)

I can access the arguments passed to the "logged" function, but is
there anyway I can capture the output of the function i.e. 10.

Many Thanks in advance,

Nathan
Yes. Capture the output of the function to a variable and log it
before you return it.

def log(fn):
def newfn(*args):
print datetime.date.today()
print __file__
print fn.__name__
print args
returnValue = fn(*args)
print returnValue
return
return newfn

Here's another idea: wrap the call to fn inside a try/except block, so
that if the function raises an exception, you can log it and then
reraise it.

-- Paul

Jul 7 '07 #2
On Sat, 2007-07-07 at 11:59 -0700, Paul McGuire wrote:
On Jul 7, 1:47 pm, "Nathan Harmston" <ratchetg...@googlemail.com>
wrote:
Hi,

I m thinking about writing some code which logs the input and output
of a function/script and stores it in a database using sqlalchemy
(although I havent started on this yet). I want to do this via a
decorator ( I think this is the best way ).
[...]
I can access the arguments passed to the "logged" function, but is
there anyway I can capture the output of the function i.e. 10.

Many Thanks in advance,

Nathan

Yes. Capture the output of the function to a variable and log it
before you return it.

def log(fn):
def newfn(*args):
print datetime.date.today()
print __file__
print fn.__name__
print args
returnValue = fn(*args)
print returnValue
return
What Paul means here is, of course, "return returnValue", not a bare
return.

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Sylvain Hellegouarch | last post: by
4 posts views Thread by Michael Sparks | last post: by
23 posts views Thread by Chance Ginger | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.