Archive for the 'python' Category

Configuring Python’s Easy Install on Windows

Tuesday, August 5th, 2008

I ran into an interesting issue with easy_install the other day. What is easy_install? Let me answer by analogy: easy_install is to Python like Gems are to Ruby. In other words, it is an apt-get like application that will automatically fetch, install and configure packages for you. So for example, you could go:

easy_install simplejson

To install a module for parsing JSON data. At least in theory the package should be fetched from some online repository, and automatically installed on my machine. Unfortunately it did not work on my windows machine. Instead I got this lovely error message:

Python was built with Visual Studio 2003; extensions must be built with a compiler than can generate compatible binaries. Visual Studio 2003 was not found on this system. If you have Cygwin installed, you can try compiling with MingW32, by passing “-c mingw32″ to setup.py.

Unfortunately I do not own Visual Studio 2003. Never had owned it, and likely I never will. I do on the other hand use Cygwin, and I do have MINGW32 installation present on my hard drive. The problem is that easy_install does not take an argument that would let me specify the compiler. I had to find another way of forcing Python to compile the packages using Mingw32 environment.

This can be done by modifying the Distutils configuration file. On Windows machines it is located in: C:\Python25\Lib\distutils\distutils.cfg. Or rather it is supposed to be located there. In my case the file did not exist in that directory so I simply created it. Inside I added the following two lines:

[build_ext]
compiler=mingw32

After this, I simply had to make sure the Mingw32 binaries were in my %Path% and I was all set. The simplejson module downloaded and installed without any issues. I’m putting this out here so that anyone having this issue can find it and resolve it the same way I did. Easy install is just to convenient not to use it on Windows.

Dynamic Method Creation in Python

Monday, June 30th, 2008

I like to use this example to totally freak out Java people. Cool dynamic languages such as Python or Ruby allow you to modify the definition of any class on the fly. Javascript let’s you do that too, but then again Javascript does not really have classes right now. It’s a prototype based language - at least until ECMAScript 4 descends from heavens in a beam of heavenly light and will bring forth much awesomeness. Here is how you do it in Python:

Observe:

>>> class Person:
...     def __init__(self, fname, lname):
...             self.fname=fname
...             self.lname=lname
...
>>> john = Person("John", "Smith")
>>>
>>> Person.whatisyourname = lambda p: "My name is " \
...     + p.fname + " " + p.lname
>>>
>>> john.whatisyourname()
'My name is John Smith'

Every new instance of Person will now have a whatisyourname() method. C and Java people are probably sneering right now, at how insecure this is, how it breaks encapsulation and etc. I used to be like that too, but I reformed. What I see here is raw power.

I’m putting this here because I was trying to do closures in Python but found out you can’t really do them. Python has lambda functions instead which are really a lisp concept. The only difference between a Python lambda function and a closure is that lambda must evaluate to something. In other words, it’s body must be an expression rather than a statement. Which makes perfect sense in Lisp because it has no statements. In Lisp everything is an expression - and frankly, that is not a bad idea. Lets you chain stuff pretty nicely.

If you need to use Python statements however, you can’t use lambda. You just have to use regular assignment:

>>> def foo(self):
...     if(self.fname < self.lname):
...             print "foo"
...     else:
...             print "bar"
...
>>> Person.foo = foo
>>> john.foo()
foo
>>> zack = Person("Zack", "Abrams")
>>> zack.foo()
bar

Of course the downside here is that this is not a closure, and that the method definition exists outside the class on its own which can contribute to clutter in your code. Then on the other hand, perhaps this isn’t so bad as it allows you to do stuff like:

>>> foo(john)
foo
>>> foo(zack)
bar
>>> foo = Person.whatisyourname
>>> foo(john)
'My name is John Smith'

I guess that’s part of Python’s charm that standalone functions can easily become instance methods and instance methods can be easily used as standalone functions.