Kwissle

My real-time quiz battle game Kwissle.com

Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Photos

Photoalbum, both old and new.

Twitter

Follow me on Twitter

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 
Python

Python inspect module


16th of August 2004

My friend Jacob Lundqvist of Galdrion today showed me a nifty little method I did not know about in Python, namely the inspect module. It allows you to find out what the name of the method was that called the method "you're in". Allow me to show an example:

 import inspect

 def foo():
     caller_module = inspect.stack()[1][1]
     caller_method = inspect.stack()[1][3]
     print caller_module, caller_method
     return "Something"

 def bar():
     foo()

 if __name__=='__main__':
    bar()

And the result is:

 >>> 
 C:\Python23\dummy.py bar

I now use this in an internal debug method that prints all SQL statements used before being executed. Every method that needs to execute some SQL will always first send it's SQL statement to the debug method. The debug method can now also show where the SQL came from. Great!



Comment

Jacob Lundqvist - 9th August 2004  [«« Reply to this]
Credits where its due ;)
I got it in my turtn from this article:

http://www.oreillynet.com/pub/wlg/5204
Rangler - 22nd July 2011  [«« Reply to this]
Four score and seven minutes ago, I read a sweet artlice. Lol thanks
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.