473,395 Members | 1,629 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,395 software developers and data experts.

How can I add behavior to all the methods in a module?

So I have the following code, which works. I have a log function that I use to redirect standard output from a function to a file. I then change the other functions in the module so that when they are called, they will pass themselves into this logging function. So the code below results in a file called "test.log" being created which contains the lines "foo" and "bar".
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. def log_function( function, *args ):  
  4.   log = open( 'test.log', 'a' )        
  5.   sys.stdout = log
  6.  
  7.   try:  
  8.     return function( *args )
  9.   finally:    
  10.     sys.stdout = sys.__stdout__ 
  11.     log.close()
  12.  
  13.  
  14. def foo():
  15.   print "foo"
  16.  
  17. def bar():
  18.   print "bar"
  19.  
  20. foo_without_logging = foo
  21. foo = lambda: log_function( foo_without_logging )
  22.  
  23. bar_without_logging = bar
  24. bar = lambda: log_function( bar_without_logging )
  25.  
  26. foo()
  27. bar()
  28.  
What I want to do is somehow take all the functions in a given module and transform them so that they will pass themselves into the logging function. Something like this:
Expand|Select|Wrap|Line Numbers
  1. for function in current_module:
  2.   function_without_logging = function
  3.   function = lambda: log_function( function_without_logging )
  4.  
Know what I mean? It's kind of hard to explain, so let me know if I need to clarify.

Thanks.
Feb 4 '08 #1
0 839

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Robin Munn | last post by:
How is re.split supposed to work? This wasn't at all what I expected: $ python Python 2.2.2 (#1, Jan 12 2003, 12:07:20) on linux2 Type "help", "copyright", "credits" or "license" for more...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
6
by: Mayer | last post by:
Hello: Is there a way to see at the python prompt the names of all the public methods of a class or the names exported by a module? I know that GUI-based IDEs have a nifty way of displaying...
1
by: Stephen Kellett | last post by:
Hello everyone, I'm trying to do something in C calling Python and its failing. I'd be grateful if you could take a look and hopefully you have an answer. What I'm trying to do is determine...
1
by: me | last post by:
hi, i just want to ask how to access VB6 Module's methods in c#. i already converted the dll into interop assembly but i cannot invoke Module's methods thru reflection. thanks.
14
by: Joe Fallon | last post by:
I am trying to build a Data Access Layer for a SQL Server back end. My class has a number of methods in it. The constructor takes a connection string. I currently have to instantiate an object...
4
by: Paul McGuire | last post by:
I have a new enhancement to pyparsing that doubles the parse speed (using a technique called "packrat parsing"), but which is not suitable for all parsers, specifically those that have complex...
1
by: Mark Denardo | last post by:
Ok here's my problem: I have a bunch of Classes at the same namespace level say "abc.xyz". And all Classes reside in different files. abc.xyz.Class1 (in Class1.vb) abc.xyz.Class2 (in...
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.