Archive for category Python

Catching ALL exceptions in Python

When working with Python, sometimes, more than anything else, you need to know what went wrong. Quite aside from all the debate about what you should do in response, and particularly when developing, you need more than just a vague idea that your CGI bailed and that there might (or might not) be some usable indication of this in the system web logs.

Even when working in a pure command line context, you may need to catch anything and everything. If you do, the following gives you a basic model of just how to do it.

import sys try: a = 5 / 0 except Exception,e: # the Exception class provides messages print 'Exception caught, message: '+str(e) raise SystemExit # bail out (optional) except: # other exceptions e = sys.exc_info() # so we mine sys library info instead print 'Non-Exception class Exception caught, message: '+str(e) raise SystemExit # bail out (optional) else: # and, well, sometimes things work out. print 'it worked, no exception!' # whoo hoo... finally: # always happens print 'Glad THAT ordeal is over -- one way or another.' print 'And here we are. Aren't we?' # you only get here if things worked out

Try out the above with code you know will work, like a=1 immediately subordinate to the try: clause, and then with code you know won’t work, like a=5/0 and see what it does.

Hope someone finds that useful. Took me a while to dig through it all and wrap my head around even the basic idea that sometimes, you just need to know!

Tags: , , , , ,

Python, TkInter, OSX (OS X) and making it all behave

I use Python a lot. Python 2.5.1 to be specific. And inside Python is TkInter, which, with a little work, will give you a handy way to put a GUI together. But there are problems. To say that TkInter is poorly supported and poorly documented under OSX is to understate the case rather dramatically. So you’re left to Google for answers, and mostly, they aren’t to be found — or if they are, they aren’t obvious or easily found. So I’m going to provide some answers here that have taken me quite some time to collect, and hopefully keyword and title them so that a Google search will actually get you to the solution you need sooner rather than as much later as it did me!
Read the rest of this entry »

Tags: , , , , , , , , , , , , , , , , , , , ,

PD database

I’ve written a public domain (that means there are no restrictions on how you use it) database engine in Python. It is very small, about 20k, and it is a single class that will run without adding any other software, module, or technology.

There are presently two versions. The original handles ASCII and integer fields; the newest version, which I presently consider to be beta in that it has not been extensively tested, adds the ability to handle binary, extended character sets, and floating point numbers.

If this is of interest to you, you may learn more about it, and/or download it, on this page.

Tags: , , , , ,