Showing posts with label doug-hellmann. Show all posts
Showing posts with label doug-hellmann. Show all posts

Wednesday, April 10, 2013

Using sys._current_frames() and the Python traceback module for debugging

By Vasudev Ram

Python's sys._current_frames() function/method and the traceback module of Python can be useful for debugging your Python code.

In the example below, I've used py, the Python Launcher for Windows. It comes with Python 3.3. If you're on Python 2, you can download the py launcher for Python 2 here. Use either of the versions (32-bit or 64-bit, as appropriate) called launcher*, not launchwin*, for the commands below.

The example below works on both Python 2 and Python 3.

#--------------------------------------------------------
# test_current_frames.py 

import sys, traceback

def foo():

    for thread, frame in sys._current_frames().items():
        print('Thread 0x%x' % thread)
        traceback.print_stack(frame)

def bar():
    foo()

def baz():
    bar()

baz()
#--------------------------------------------------------

Run the program with any of the following 3 commands:
py test_current_frames.py

or

py -2 test_current_frames.py

or

py -3 test_current_frames.py
You should get output similar to this:
Thread 0x17dc
  File "test_current_frames.py", line 17, in 
    baz()
  File "test_current_frames.py", line 15, in baz
    bar()
  File "test_current_frames.py", line 12, in bar
    foo()
  File "test_current_frames.py", line 9, in foo
    traceback.print_stack(frame)

Also read more about the traceback module on Doug Hellmann's Python Module Of The Week (PyMOTW) site, a.k.a. PyMOTW.

- Vasudev Ram - Dancing Bison Enterprises

Sunday, July 3, 2011

Book: The Python Standard Library By Example, by Doug Hellmann

By Vasudev Ram - dancingbison.com | @vasudevram | jugad2.blogspot.com

Just got to know about this new Python book published by Addison-Wesley Professional:

The Python Standard Library By Example: by Doug Hellmann:

http://www.doughellmann.com/books/byexample/index.html

Excerpt about the book:

[ The Python Standard Library contains hundreds of modules that provide tools for interacting with the operating system, interpreter, and Internet—all of them tested and ready to be used to jump-start the development of your applications. This book presents examples demonstrating how to use the most commonly used features of the modules that give Python its "batteries included" slogan, taken from the popular Python Module of the Week (PyMOTW) blog series. ]

Doug Hellmann is a veteran Pythonista and the Communications Director of the Python Software Foundation, and is also heavily involved in Python in many other ways. I've read many of his blog posts from the PyMOTW series, and they are good.

The above web page has:

- a description of the book, and who the target audience is - the intermediate Python programmer - but even if you are a newbie Python programmer but experienced in other language(s), you can probably derive a lot of value from both the blog series and the book

- a link to a free PDF containing the table of contents, foreword, introduction, index, and chapter 2 ("Data Structures") from the final version of the manuscript

- a link to a free e-book containing the table of contents, foreword, introduction, index, and chapter 1 ("Text"), available through the iTunes book store

Posted via email
- Vasudev Ram - dancingbison.com