Category Archives: Python

New Milestone: My First Linux Application

TuxLast night I finished my first program that runs on Linux. If you haven’t guessed yet, its built on the PostgreSQL and Python application stack. The program is a server that checks and updates different files, while at the same time, has the ability to accept remote connections through telenet for administering the server.

I can’t go into deeper details about what the program does because of an agreement with my employers, however, I can talk about what I had to learn in order to put everything together. Later on in the week, I plan on posting a write up on Python and the Twisted Framework. While researching Twisted, it was maddening at the sparse amount of example code for some parts of the framework.  For me, it was authentication and integration of application code outside of protocols. Even now after completing everything, I’m still clueless on a whole slew of issues. I know most of the nerds out there will just say, ‘So? why don’t you just read through the source? It’s on your computer in plain sight.’ Since I’m still new to Python, reading through the source for the frameworks is like reading Chinese. As I’ve mentioned before, it all looks like a hodgepodge of pseudo code.

Still Switching to Python: Iterating through dictionary lists

Last night while developing a module, I ran into a situation I never thought I would encounter, a syntax problem. Why the surprise? Python is fairly sparse when it comes to syntax. The lack of keywords, brackets, and semicolons makes code look more like a hodgepodge of pseudo code than a programming language.

While trying to implement a simple iteration through a dictionary list, I ran into a syntax error with the following code:

for k,v in dictList.items():
print "%s=%s \n" % k,v

This resulted in the following error: ‘ValueError: need more than 1 value to unpack’.

After a bit of searching and experimenting, I figured out that I was missing the parenthesis around the ‘extraction values’. This was confusing because most of the examples I found did not cover the more familiar  iteration patterns in C like languages. To correct the error above, you would type:

for (k,v) in dictList.items():
print "%s=%s \n" % (k,v)

During my search for the solution. Most of the examples I found for iterating through lists looked more like the following:

print "\n".join(["%s=%s" % (k,v) for k,v in dictList.items()])

 
The above accomplishes the same thing, just in a slightly more succinct format. To most programmers that come from a C background, this looks downright odd – kind of like comparing English to Old English. Looks similar but not.

Switching to Python

Yesterday I looked through a bunch of things related to developing in Python and PostgreSQL. One of the things I was researching was unit testing my code. Unit testing is the testing of individual software components. Many frameworks exist for doing this. These include NUnit, JUnit, MbUnit, and MS Unit Testing. Yesterday I looked at PyUnit, a port of JUnit to Python,  as a potential candidate for testing not only code for future Python programs, but also unit testing functions in PostgreSQL. For now, I think this will be sufficient.

As I’ve started to use Python more, I’ve yearned for a better IDE than Eclipse and PyDev. So far the only commercial IDE’s I have read about are WingIDE and ActiveState’s Komodo. I plan on looking at these sometime soon.

New Article