Entries Tagged 'Python' ↓

The future of programming

This is an interesting time to be in the world of computers. The web is becoming more and more to everyday computing for the average user. Applications are moving off the PC and onto the web. Hardware is getting faster and faster (check this out, 80 cores on 1 chip for less that 100 watts!), and things like Operating Systems are beginning to take a back seat in the grand scheme of things.

With all of these changes coming around, I’m thinking that in the near future we programmers are going to have to start changing how we write our programs. We can no longer depend on gigahertz increases to drive the software faster. A point has been reached where the software needs to step up and be more efficient and utilize the new hardware more effectively.

Along those lines, I ran across this very cool article today: Functional Programming For The Rest of Us

Its a really good overview of functional programming and its many advantages (concurrency, etc.). With a lot of attention being placed on “functional” features in languages like Ruby, Haskell, Python, etc. this article is a good primer on what functional programming can do for you.

Loose code and other projects

I’ve got a bunch of little programs running around, and I decided to put them up all in one place for everyone who’s interested. (Some of them used to be on my old Geocities page, some I’ve just had hanging around on my heard drive.) They are mostly python scripts, and they do a lot of different things. The project page is located here at the Iron Bound Software code repository.

There’s a quick summary of what’s in the SVN repository after the jump. This is the same description that is on the front page. Thanks to Google for the awesome project space!

Continue reading →

Threaded data harvesting in Python

Before it slips my mind (again), I wanted to drop this link to this awesome example of using python and threads to do interesting work. The article discusses using the threads to go out on the internet and gather data (i.e. RSS feeds), parse them, and store the results in a database.

The “pattern” that is shown in the article is really good for any language that uses threads. I personally haven’t had a chance/need to use python’s thread capability, but I do use them in Java occasionally. Using a the work queues is a great way of making sure that the worker threads are fed (say that three times fast), and the idea of having the output work of the threads feed into a database thread (to help make sure that the database doesn’t get slammed, but the workers can keep going) is really great. I haven’t run into that situation yet, but this is one of those cool ideas to take note of and keep in your back pocket. You never know when you are going to need that ace in the hole.

A sale on O’Reilly books: 3 books for the price of 2

I’m a big fan of O’Reilly books. They are a great reference when tackling pretty much any programming topic. I own a ton of their books.

A few weeks ago I was searching for something (I can’t even remember what it was) and I stumbled across a new feature on their site. They are now selling short (around 50 pages) PDF’s called “Short Cuts” that are basically short papers on some topic that they don’t have a book for yet. Its a pretty neat idea, the PDFs range in price from around $5 to around $10 or so.

And the coolest thing is that there is a promotion going on where you “Buy 3 books for the price of 2″. With the short cuts I was hooked because there were a couple that looked interesting and the price was hard to beat. The whole process was pretty painless and within minutes I had my new PDFs.

Fast forward to today: Again I’m searching for something (RTF documentation to be exact) and a hit turns up on O’Reilly’s site. This time its for one of their pocket reference guides. I was bummed because it looked like the book had the answers I was looking for, but I knew the odds of finding it locally were low, and I didn’t feel like waiting for it to show up in the mail…

And right around then I noticed the book was available as a PDF for *half* price. It turns out that O’Reilly is now offering a lot of the pocket guides as PDFs for immediate download, and they are pretty much half the price of the dead tree book. Isn’t that awesome? the best part is the 3 for the price of 2 sale is still going on.
So, I wound up spending $10 and now I’ve got 3 more PDF of pocket guides. :) Go check it out if you are in the market for any of the pocket guides. Also, several of their newer books are available in PDF form also. Sadly, their older books aren’t in PDF form yet, which bummed me out because there’s a couple that look really interesting. This is the long tail at work.

An ordered HashMap in Java

HashMaps are pretty cool, they allow you to access you information using a “named” index. They aren’t as good/useful as Python’s dictionaries, but they are still really useful. (In Java, the key and the value have to Objects, so using primitives like int are out. Which is a bummer because while you can create an Integer out of an int, it leads to more code which looks less streamlined. Python doesn’t have this problem because everything is an Object.)

It seems like I’m always using HashMaps to organize my data. The other day I ran into a situation where I needed to pull the keys from the HashMap in the same order that I put them in. I’d never run into this particular scenario before, so I didn’t really give it much thought until I kept noticing that my keys were coming out in a crazy order (i.e. not in the insert order, and not in alphabetical order either).

My first thought when I need something in an ordered collection in Java is to run to the TreeMap (or TreeSet depending on the data situation). TreeMap will keep the keys sorted, so I thought all was well. When I tried it out I found out that because I was using Strings as the key, it alphabetized them, which is not what I was looking for.

The data structure I was looking for is the LinkedHashMap. It keeps track of the keys based on insertion order, so that when you do a call to getKeySet(), you will get the keys back in the order you put them in. Once I started using LinkedHashMap, everything started working perfectly.

Sometimes it pays to look around the API a little bit and see what all is out there. I’ve been running around the java.util package for a while now, but until this requirement came up, I’d never even given LinkedHashMap a second look.

And since LinkedHashMap derives from HashMap, you can use it in the same places as you would a regular HashMap (i.e. use the Map interface in your method calls) and no one would notice the difference, other than the sorting of the keys.

The Netflix prize

Everyone is talking about the offer put up by Netflix yesterday. Basically if you can improve their movie recommendations, you win a million big ones.

This is a really cool idea. Its a hard problem (otherwise everyone and their brother would already have done it), that looks like it has no solution. But just because it looks hard doesn’t mean there isn’t a solution!

To help out Netflix has supplied a data set of user recommendations. Looks like its time to fire up Eclipse, break out the python, and put on my Data Miner hat, and start reading those AI books. ;)

Unintended consequences

Yesterday I was raving about wxPython and how it was letting me do “cool” things. Today I discovered it was letting me do a lot of things, some of which aren’t so cool…

It turns out that my call to wx.Window() wasn’t 100% correct. As in I was passing bad/incorrect data to the constructor. Yet, the hardy little wx.Window() class kept on a rollin‘. I don’t like that.

Python is pretty loose when it comes to variables and data types, that’s one of it strengths. In this case it is a problem. In my call to the class I was passing in a wx.Frame object, a int, and a string. And it took this, and everything seemed to run.

Calls to getClientSize() kept returning (20,20) which is kinda odd, I was specifying 1024×768. But after running a method the called a bunch of dc related methods, the getClientSize() method began returning (1024,768). Odd, no?

Perusing though the documentation told me that the default window size is (-1,-1). If the code detects this, it will set it to  (20, 20) as sort of a “indicator” that something is not right. As opposed to say printing something out to standard error, or something useful like that.

Anyways, I had a flash that I  might be passing in bad data. In any other strongly typed language passing data this bad would wreak all kinds of havoc. In this library it just keeps on trucking. I changed my call to the class to match what the API says it should be, and lo-and-behold getClientSize() began returning the correct size every time.

So, the lesson here is if you pass bad/incorrect data in python/wxpython, that is no guarantee that you will be warned. It seems like it will try its best to keep on going. I get run time errors in python all the time for things like this, but this is the first time I’ve seen it let something that seems so wrong just slip by.
Which can make debugging such a pain.

wxPython

…Or, I love it when a plan comes together. :)
A while ago I wrote a small GUI app in python using Tk (Tkinter to be specific). It worked pretty good on Windows machines, which is where I was running it at the time. About a year later I tried to run it on a Mac, and the results were a little surprising. It did not look good. It seems that there are some differences between the Tk stuff on different platforms.

Every since then I’ve been wondering if there was a more platform independent way of making Python GUI apps. I’d heard a lot about wxPython, so I decided to give it a shot. The documentation is pretty good and the demos that come with it helped me get up to speed pretty quickly. (Plus the API reminded me of some work I did in the Win32 world a few years ago, and surprisingly, that helped me get things “working” faster than I think I would have otherwise.)

Anyways, I built a little app to visualize an idea I’ve been having for a few weeks ago. Again, on Windows machines it looks great. Today I remembered that I hadn’t run it on a Mac yet. With a little bit of trepidation, I grabbed the code and hit run. Lo-and-behold it runs great on the Mac. A little bit slow on the re-draw, which probably has more to do with how I wrote the event handlers, but it ran and looked just like it does on the Windows machines.

Thus I have concluded: wxPython rocks.

Now I remember why I like Java and Python

I just had to whip up a quick little program in C++ for a class I’m taking. I used to do a lot of C/C++ coding, but that was a while ago and it shows. My first pass at writing the program compiled great, but once I started running it I began to notice that under certain conditions it would crash.

Since its been a while since I’ve written any C++, it means its been a while since I’ve debugged any C++. So I spent about a half hour trying to figure out what EXC_BAD_ACCESS meant. It meant that I had forgotten about the joy of pointers. Then once I realized that, I spent another half hour trying to find out how do a dynamic multi-dimensional array in C++. The kinds of things that are second nature to do in Java and Python (which is where I spend most of my days).

The major lesson learned? A friend at work likes this quote from Martin Fowler: “Experience and knowledge are expiring assets.” In other words: Use it or loose it.

Plowing on

In yesterday’s post I was “contemplating” the experience of coding up something to work with XML. Specifically, I was kinda stuck because the results I was getting weren’t really helping me move forward on creating something to translate XML into Python objects.

As is usually the case I made the whole situation harder than it needed to be. I was trying to write a solution that involved a recursive function and it wasn’t working the way I expected it too. The neat thing about recursive functions is that they can be re-written as a form of a loop, so I used that approach to help debug the errors I was getting.

Having a good debugger was essential also. Without the pydev plugin for eclipse, it would have taken forever to figure out what DOM objects I was looking at. Being able to set a breakpoint and look at a live object was priceless. It turns out that the DOM object had more things in it in different places than what I was expecting. Having to guess and do a million print’s would have taken what little bit of sanity I have left. :)
Your tip for the day is that Python’s setattr() and getattr() functions rock. They allow you to inspect and modify member attributes of a class. After reading though Dave Mertz’s articles (here and here), it became very clear how I could use these functions in my own code to build objects from XML. Now I can move forward and start using that loaded data to actually do something interesting…