473,378 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 1104
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Sylvain Hellegouarch | last post by:
Hi, A bit off topic. I just wondered what was your feeling when you were coding with Python. I have beebn coding with different languages and the only that has given me the will to invent or...
4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
11
by: rayala | last post by:
Hi all, I am having very weird problem in my Outlook I am running my web application from with in Outlook.I found a strange problem that it is creating different sessionId if i open a new...
23
by: Chance Ginger | last post by:
If I define a decorator like: def t(x) : def I(x) : return x return I and use it like: @t(X) def foo(a) :
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.