Jason in a Nutshell

All about programming and whatever else comes to mind

Archive for June, 2009

IronPython in Action reviewed

Posted by Jason Baker on June 28, 2009

IronPython in Action by Michael Foord is a somewhat interesting book in that it isn’t really isn’t about IronPython.  It’s a book about programming in .Net using Python.  Although some will accuse me of being overly pedantic, there is a subtle difference.

After all, IronPython is just a piece of software.  Anytime you have a bridge between two languages and/or programming environments, there are always some “devil’s in the details” type issues that come up.  These are questions like:

  • Are IronPython strings the same thing as .net strings?
  • Can I use ASP.NET with IronPython?
  • Can you call any .Net code from IronPython?

Who it’s for

If you’re a new programmer, IronPython in action probably isn’t the book for you.  Although it does have an introduction to programming in Python, it’s very brief.

On the other hand, if you’re an experienced programmer who already knows what variables are and why classes are useful, you’ll likely find IronPython in Action a pretty good primer.  Although this book does have an introduction to both Python and .Net, I think you’d get the most benefit if you have experience with one of those technologies.

IronPython in Action in Action

IronPython in action covers three main areas:  Python, .Net, and Usage of IronPython.  I’d like to cover these sections individually.


As mentioned, the book does include a brief tutorial on Python.  If you’re a total newbie to Python, you may want to keep the official Python tutorial nearby in case it goes too fast for you.

That said, the section on Python is fairly readable.  It even has diagrams if you’re the kind of person who learns visually:

A diagram from the

A diagram from the book

…although I’m pretty sure I could have figured out what a set of parenthesis and colon is on my own, thank you very much.

I’ve always thought that there were two layers to Python:  the “normal” stuff and the black magic.  Foord doesn’t just stick to the easy stuff either.  The deepest depths of the Python language are covered all the way up to metaclasses, the arch-typical example of Python voodoo.  This isn’t what makes this part of the book shine though.  Any idiot can learn how to use a metaclass by spending some time with the Python docs.  No, the place where this book really shines is in how it teaches these concepts.  The line between Python newbie and Python wizard is in knowing when to use the magical parts, and Foord does an excellent job of giving real world examples of these.

I also found it interesting to that the book mentions some popular third party Python libraries and tools.  For instance, the section on testing includes a summary of some test-runners such as nose to help simplify running tests.  As anyone will tell you, Python’s greatest advantage is its huge standard library and its even bigger set of third-party libraries.

In all, this isn’t just a good way to learn how to use IronPython.  This is a good introduction to the Python language itself.


After that, IronPython in action delves into how .Net works and how IronPython works with it.  Just as with the Python introduction, you won’t come out of this a .Net expert if you weren’t before.  However, it’s helpful if you’re not familiar with what an assembly is or want to know how the heck you’d use a generic with a language as dynamic as Python.  As it turns out, IronPython works pretty well with .Net in all but a few cases.

The book leaves no stone unturned as far as the .Net runtime is concerned.  It goes from the basics like using .Net classes all the way up to the dirty stuff like using P/Invoke and creating dynamic objects in C#/VB.NET.  In short, I can’t think of many features of the runtime itself that this book doesn’t cover.

I was a bit disappointed that third-party .Net tools didn’t get as much coverage as Python’s did.  For instance, ADO.NET simply isn’t enough to get a full picture of how to use databases with .Net.  Since most of the Python ORMs may not have very good support as they rely on C extensions, why not cover an ORM like NHibernate or Subsonic?  Or why not show how to write tests using NUnit in addition to Python’s unittest framework?  Or why not cover interoperability with other .Net languages like F# or Boo?


Arguably the best part of this book is in the sections “Core Development Techniques” and “IronPython and advanced .Net”.  This is where you get into the real meat of using IronPython.

IronPython in Action is a very practical book.  It teaches you not only the theory of using IronPython, but the practice as well.  It teaches you how to use IronPython to do test-driven development, how to read in XML files much more easily than in Python or C#, it even goes into detail on some of the hottest .Net technologies like WPF and WMI.  Virtually any type of programmer will get a brief introduction to doing the things they want to do in IronPython.

One area that I’d like to see covered more is GTK#.  It’s not quite as cool as WPF, but it’s cross-platform and much better than WinForms.  My experience has been that PyGTK+ can be a major pain to install on some platforms, so it would be helpful to have an introduction to the .Net equivalent.


In summary, if you’re a developer wanting to work with Windows technologies using Python there’s no question:  go and buy IronPython in Action.  Right now.  If you’re wanting to develop IronPython applications for various platforms or don’t want to tie yourself to just Microsoft technology… still go and buy IronPython in Action.  There are still some holes that can be filled, but all in all, this is a pretty solid book.


Posted in Programming | 1 Comment »

Windows for Python programmers: IIS

Posted by Jason Baker on June 4, 2009

Ok, so you finally talked your employer into letting you use Python.  Good job!  But there’s a catch:  it has to run on Windows.  This actually isn’t too problematic.  Python runs very well on windows.  Unfortunately, it isn’t always terribly well documented.


Ok, now I know that you’re envious of those *nix Python programmers.  Apache running mod_wsgi is a pythonista’s dream.  And newer servers like lighttpd and nginx are a pythonista’s wet dream.  You might as well forget they exist:  Apache runs on Windows, but it just wasn’t made for it.  And forget about lighttpd and nginx.  You’re stuck with IIS.  Don’t worry, it’s really not that bad.

Options for running Python

This is the most important part for obvious reasons.  You have several options:

Classic ASP

No really, I’m being serious.  If you need a quick and dirty way to serve Python applications, classic ASP is the way to go.  You’ll need python win32, but chances are you’ll be needing that some time anyway.  I’d recommend just installing ActivePython.  Besides the obvious ease of setup, there is another benefit to using Python this way:  compatibility with other Classic ASP applications.  After all, you’re a windows shop.  You have at least one Classic ASP application lying around still, don’t you?

There is documentation here.


IIS’s FastCGI module is made and officially supported by Microsoft.  Thus, this is the best option for you if your management’s usual response to using open source software usually involves them saying something along the lines of “take a shower, hippie!”  Unfortunately, it’s a bit difficult to find documentation on (at least for Python) and a lot of Python software isn’t made to run as a daemon on Windows (django is still reliant on forking for example).  Thus, I’d recommend it as a last resort.


Of course the biggest downside of this software is that it can be a bit of a tongue-twister:  say ISAPI-WSGI five times fast.  Other than that, I’ve had pretty good luck with it.  It’s easy to set up, and the performance isn’t too shabby.  Plus, it’s open source.  Currently, this seems to be the best way to get Python running in IIS.  Now you just need to convince your boss that you do in fact take showers.

There are instructions for getting ISAPI-WSGI set up with Django and Pinax, TurboGears and CherryPy, and Pylons.


Unfortunately, I don’t know enough about this project to be able to say enough one way or another.  But it is an option.

Miscellaneous tricks

Here are a few tricks that I’ve found to be helpful:

Use Application Pools

You may be envious of mod_wsgi’s daemon mode and its ability to run applications in their own process, but IIS 6 and above actually have something better:  Application Pools.  You have the ability to restrict certain sets of applications to a particular pool of processes.  This is handy if you need to isolate certain Applications.

This is also handy because if you make any changes, you will need to restart the Python interpreter.  If you’ve got Python running on its own server, then you have nothing to worry about.  Otherwise, you’ll want a way to restart the Python interpreter without disturbing any other applications that may be running.  This is where Application Pools really shine because you can not only restart Python separately from the rest of the server, but you can also restart individual Python applications as well!

To learn more about creating Application Pools, see this technet article.  Also check out this article to get more info about how to recycle (restart) an Application Pool.

Use ActivePython

I’ve already mentioned this once, but it bears mentioning again.  Use ActivePython.  It comes pre-bundled with Python win 32, which you’ll probably be installing anyway.

Choose a good framework

I suppose I could probably write a separate blog post about choosing web frameworks, but that’s for a different time.  The good news is that I can sum it up by saying this:  if you’re not sure, choose Django.

Posted in Programming | 3 Comments »