If it won't be simple, it simply won't be. [source code] by Miki Tebeka, CEO, 353Solutions

Thursday, May 18, 2006

Using Python Syntax in Configuration Files

As a rule it thumb, it's best to place as many configuration options in a configuration file. This way you don't need to edit/compile the code to change its behavior.

However writing a parser for configuration files takes time, and the standard ones (.ini and .xml) have their limitations. Lucky for us each Python program comes with the full interpreter bundled. We can do stuff like:

# settings.py
from sys import platform

if platform == "win32":
APP_HOME = "c:\\my_cool_app"
else:
APP_HOME = "/opt/my_cool_app"

Then using __import__ in our application we can load the configuration file and parse it without a sweat:

# my_cool_app.py
settings = __import__("settings.py")
print settings.APP_HOME

To view this approach taken to extreme, have a look at http://www.unixreview.com/documents/s=9133/ur0404e/

Wednesday, May 10, 2006

"Hard" breakpoints in Python

Sometimes, when debugging, you want a to stop at abreakpoint no matter what.
Use the following code:

from pdb import set_trace

def buggy_function():
pass
pass
set_trace() # Break here
pass
pass

if __name__ == "__main__":
buggy_function()

When you run the script with python (not with pdb), set_trace will cause the program to stop at this line and pdb console will show.

BTW: You can do the same trick on MSVC if you write __asm int 3

Here We Go

Starting a blog! How original :)

Welcome!

I plan to post a weekly tip on Python programming and programming in general, hope you'll find it useful.

Miki

Blog Archive