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!

Comments

Jacob Lundqvist

Credits where its due ;)
I got it in my turtn from this article:

http://www.oreillynet.com/pub/wlg/5204

Rangler

Four score and seven minutes ago, I read a sweet artlice. Lol thanks

Your email will never ever be published.

Previous:
Film Music by Alfred Schnittke August 15, 2004 Music
Next:
Changing the size of a textarea box August 18, 2004 Web development
Related by category:
How I run standalone Python in 2025 January 14, 2025 Python
get in JavaScript is the same as property in Python February 13, 2025 Python
How to resolve a git conflict in poetry.lock February 7, 2020 Python
Best practice with retries with requests April 19, 2017 Python
Related by keyword:
How to log ALL PostgreSQL SQL happening July 20, 2015 PostgreSQL, macOS
Speed test between django_mongokit and postgresql_psycopg2 March 9, 2010 Python, Django
CSSViewer - new promising Firefox Extension February 20, 2006 Web development
Pretty print SQL script August 6, 2004 Python