Skip to main content

Posts

Showing posts from August, 2008

A few days of relative fame

Here's what happens when somebody posts a few things you've written about / in python to reddit.com :

A more efficient method of sorting a list of IP addresses in Python

As ben, okplus and tom pointed out in comments to my Sorting a list of IP addresses in Python blog post, my sort function is very inefficient. As implemented, each IP address gets converted to decimal a number of times which is unnecessary and wasteful. Thinking about the bigger picture - using the function in an application - it doesn't make sense to convert the IP address to decimal in the sort function. Since my goal was to extend the work of the iplib module, the solution, I think, should involve using this module more efficiently, if possible. The iplib module represents an IPv4 address using its IPv4Address class. When initializing an instance of this class, the address gets converted to decimal and stored in the _ip_dec attribute, accessible by calling its get_dec() method. So by converting the raw IP addresses to IPv4Address instances outside the sort function (which I would probably be doing anyway) and using the get_dec() method in the function should improve things. S

Playing with Google AppEngine

I've just spent the last two days playing around with Google's AppEngine , and I like it. When my language of choice was perl , I wrote plain ole CGI web applications (if you can call them that) using perl's CGI module . In the python world though, web frameworks and WSGI seem to be the way to do things, so I've known for a while that I needed to learn one of the many frameworks out there. I spent about a week getting my head around TurboGears and the concepts of ORM a year or so ago using the TurboGears book . After a week I has finished the book and has the sample app working but had no more time left to spend on the project. Obviously, not using it, I've long since forgotten all of the TurboGears specific stuff. I had through to given Django my next try but AppEngine caught my eye in the meantime. AppEngine has a nice Getting Started Guide which I used to get a simple little application up and running on the local development web server included in the SDK

Sorting a list of IP addresses in Python

As I work a lot with network data, one of my favourite python modules is iplib . It takes care of quite a few of things I want to do with IP addresses but lacks a lot of functionality of perl's Net::Netmask which I relied on extensively when perl was my favourite language. One of the iplib missing features is a method for sorting a list of IP addresses, or at the very least, a method for comparing two addresses. Luckily this is easy enough to implement yourself in python using a customised sort function. See the Sorting Mini-HOW TO for a well written document on sorting in python. Here is my attempt at a custom function for sorting IP addresses. import iplib ips = ["192.168.100.56", "192.168.0.3", "192.0.0.192", "8.0.0.255"] def ip_compare(x, y): """ Compare two IP addresses. """ # Convert IP addresses to decimal for easy comparison dec_x = int(iplib.convert(x, "dec")) dec_y = int(ipl