Archive for category PD Software

aa_counter.py — pre- and post-increment and decrement in python

Screen shot 2015-05-24 at 3.36.42 PMI like Python. A lot. But it has its limits, and short of forking a new version of Python for myself, sometimes it is just best to implement some kind of work-around. In this case, for pre- and post increment and decrement operations on counters, which Python regrettably lacks.
Read the rest of this entry »

Tags: , , , ,

aa_webpage.py — awesome and easy web pages with Python

I do a lot of generating web pages using Python. Python because I think Python 2.7 is awesome, and web pages because I do lots of work with various web sites that require dynamic results. Python is a very useful tool for me to get those dynamic results. It’s server-side (at least, the way I use it, it is, because I really try not to embed client-side resource usurping things), it is fast and efficient, and it is what I am comfortable with.

The thing is, web pages — generally speaking — aren’t all that simple if you really do them right. You can’t just throw a few tags together, test them on one browser, and hope you’re golden. Because you won’t be, I can pretty much promise you.
Read the rest of this entry »

Tags: , , , , , , , , ,

aa_dbo.py — Simplified PostGreSQL use from Python

Screen shot 2015-05-04 at 3.00.37 PMHere’s a Python import library that, working in conjunction with the PyGreSql project, makes using PostGreSQL from within Python super, super easy. Complete with examples. You need PostGreSQL installed and running (obviously, I hope), and you’ll need to install the PyGreSQL Python module as well.

Click here to download the import library.

Revision Changes
1.0.1 Initial Release
1.0.0 Internal Version

Tags: , , , , , , , ,

Amateur Radio Callsign utility

Screen shot 2015-05-02 at 11.36.46 AMWhen creating reports involving amateur radio callsigns, it is useful to be able to sort them according to region, prefix and postfix, as this is the way we are accustomed to thinking about them. In addition, padding them so that call regions align and other types of worthy formatting are applied is something I have found to be very handy.

For this purpose, I developed aa_calllib.py, a Python import library. You are welcome to use it.

Click here to download the zipped library.
Read the rest of this entry »

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

Colored reports for text and HTML in Python

One of the things that I face regularly is report generation. Often they’re free form, by which I mean not tables full of tabulated data.

Output from my htmlAnsii() class

Output from my .htmlAnsii() class

Just “is this ok, is that ok, 27 of the other happened”, that sort of thing. I like to use color — green if everything is ok, red if it isn’t and so on.

I’m often out where I want the report in a web browser. But then again, I’m often at my desk, signed in to a console and I want it there. The environments couldn’t be much more different; HTML tags on the one hand, within the wrapper of a page, and ANSII escape sequences on the other. And they’re both kind of annoying and error-prone to write out explicitly, especially when you’re doing it a lot.

What to do?
Read the rest of this entry »

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

Making Python’s sqlite3 import easy as 1 2 3

Lurking within the normal Python 2.x distribution is the sqlite3 import, which is an amazingly powerful, no-server-required, mostly SQL compatible database engine that can be used in any project without restriction.

That’s the up side. The down side is, as a fairly complete database, there are many options and varied ways it can be used, and managing actual database transactions isn’t all that simple — to do it right, even a single query takes about sixteen lines of code. And yes, if you want maximum flexibility and the ability to use every feature in sqlite3, that’s how you should do it.

But. Most database operations are very straightforward. You want to issue a single command to the database, or a query. Perhaps you want to write a bunch of data and then commit it all at once so that the database doesn’t contain part of the data from a more complex transaction. You need to know if something went wrong, and if it did, what it was. Those are by far the most common use cases for me, and I suspect that’s true for others as well.

Frankly, it’s difficult enough dealing with the SQL query language itself. Why make actually using it harder than it has to be?
Read the rest of this entry »

Tags: , , , , , ,

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.

Something to keep in mind: The ELSE clause of a TRY block only runs if execution proceeds off the end of the TRY section. So if you have two statements in the TRY section, and the first one runs but the second one does not, the ELSE clause will not execute. The EXCEPT clause will due to the exception. FINALLY always, always runs, even if the EXCEPT clause has an exit in it.

You can think of the ELSE as being functionally equivalent to just putting code right after the TRY-EXCEPT-ELSE-FINALLY sequence if you build an unavoidable exit into the EXCEPT portion. Of course, it’s nice to put related code in, because that makes the functionality and intent more obvious. And if you don’t have an exit there… then ELSE can be quite useful, as it won’t run if the TRY block fails, but the code after the entire TRY-EXCEPT-ELSE-FINALLY sequence will.

Hope someone finds this 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: , , , , ,