Penguins and Pythons, Oh My!

Note: I have been meaning to get this post up for a while now.  However since I am swamped with work in general, everything is delayed and taking longer than expected.  Sorry about that.

As I mentioned in an early post, I am working on building up my professional experience in Python.  It has been a few months–time sure does fly when you are busy as heck–so it makes sense for me to update you on my experiences with Python so far.

Living in Large in Python-landia

I always enjoyed writing Python scripts.  Nothing quite beats Python when you need to write a script to search-and-destroy empty files or automatically converting files.  Sure you can suffer through BASH scripting or BATCH for the poor souls stuck in Windows.  You could write a one-liner Perl script that you could never ever read again.  Or you could bring in a 800 pound gorilla into the mess by coding a full-on program in Java or C++.  But why would you want to, if you can write a fully functional script in Python?

Writing scripts is one thing and writing applications is another.  An full application contains a lot of complexity, structure and code, and initially I felt uncomfortable building a large web application in Python.  Turns out it is not as big of a deal as I thought.  First of all a lot of the code that I write in Java is boilerplate or object manipulation or setter/getter fetish.  All of that goes away in Python.  Also it is easy to simply use other people’s libraries for much of that other work, so the size of a Python application is considerably smaller.  With a bit of careful organization and good code modularization, most of the problems go away by itself.

I found the last sections of Learning Python the Hard Way by Zed A. Shaw, an excellent reference as a both a refresher in Python and working with complex Python applications.  Incidentally I found the book so good, I think I will use it as a textbook when teaching programming.

As I use Python more and more, the more I like it the way it is.  Yes, I even have gotten over the whole whitespaces and tabs issue that most people get hung up about in Python.  Besides the brevity and elegance of the language, how can you not like a language that:

  • Has a Zen statement built into it.  (Hint: run import this in a Python shell.)
  • Refuses requests to be anything that it isn’t. (Hint: run from __future__ import braces)
  • Links to a xkcd comic about Python!  (Hint: run import antigravity)

Using PIP and PyPi (Or Shopping at The Cheeseshop)

One of the fun things I recently discovered in Java with Maven, Ivy and Gradle was the concept of automatically downloading and installation of dependencies.  I loved it that you could list the types of libraries that you’re application and everything automatically installed during the first build.  Also having lived with Linux package management, I got used installing all my software via a package manager.  Unfortunately many of the packages in Ubuntu are not the latest and greatest libraries that I need.  Also building a Debian deb or RPM is an exercise in yak shaving.

Fortunately all those issues are addressed in Python.  For package management you have the PIP utility, that installs packages available in a large web repository of Python libraries and applications called PyPi [http://pypi.python.org/].  This combination lets you install packages with a simple <code>sudo pip install $AWESOME_PYTHON_PACKAGE</code>.  Pretty nifty, eh?

If you are wondering about the reference to “The Cheeseshop”.  Apparently PyPi (not to confuse with the Python implementation of Python called PyPy) used to be called “The Cheeseshop” in reference to the Monty Python sketch by the same title.  Fortunately finding software that you want in PyPi is much easier than shopping for cheese at the Cheeseshop.

Charming Code with PyCharm and PyDev

Python code like I mentioned is one of those rare languages where you do not need an IDE to use, outside of a text editor and a shell/console.  I could not imagine doing the same with Java or C++.  For anything more complicated than a small function you really do need a full-blown IDE like Eclipse.  However after working with my favourite text editors Kate and Vim, I realized that I was missing better text highlighting and tab completion when writing Python code.  A good IDE really does help and not hinder when hacking in Python too.

Being familiar with Eclipse, I started using PyDev, a great Eclipse plug-in for Python with Django and Google App Engine support.  I highly recommend it, if you already use and are familiar with Eclipse.  However even when using the Eclipse plugins for Git and Syntax colour themes, I found that Python on Eclipse is still sort of a second class citizen compared to Java.  (Not sure if PHP or C++ developers on Eclipse have it any better in that case though.)

I experimented with quite a few different IDEs, while each offered something nice none of them really stood out.  Then I met the nicest Python IDE out there: PyCharm.   Of all the IDEs I’ve looked at PyCharm has the nicest tools, themes and feels much nicer than any other IDE out there.  Yes, I know that PyCharm is proprietary and that I am breaking my own rules about using libre software.  However there isn’t any IDE high quality enough that does what PyCharm does, including refactor, style checking and really good Django support.  The Git support could be nicer, but for any complex Git operation I end up using a console anyways.  Still I was impressed enough with PyCharm, that I actually bought a license for myself pretty much on the spot.

Other IDEs I’ve looked at included:

Dancing to the Sound of Django

While working with Python is pretty sweet, for me the big question is whether or not I can potentially make a living using Python.  Since most of my expertise is in web or web-powered mobile applications, I was most interested in what Python can do in that area.  Hence my interest in Django.

After using Django for a while, I can confirm that it is an amazing framework.  Django has captured my imagination of what can be done with Python.  While it takes some time to get used to the framework, it makes building web applications easy and enjoyable.  A lot of the tedious and mundane work like building administrative forms, mapping models to database tables and input validation, are dealt with for you.  Django is very modular, so with a bit of effort various underlying technologies can be swapped in and out.  Django provides smart defaults, but allows you to easily override the parts that don’t make sense.  One example is that it is fairly easy to override the default ORM SQL that is auto-generated from a model, to match the model or database better.

Documentation can make or break one’s perception of a library or framework.  Django has awesome documentation, and so do many of the community-contributed apps and systems of Django.  Some the add-on components really add a lot like Taggit and South, which provide tagging and database migration to Django.  Also there are some great books on Django:

One of the best parts of Django that I’ve encountered is the community of Django developers.  Unlike some communities, Django seems to get developers who are knowledgeable, friendly and approachable.  By hanging out on the #django IRC channel and asking the right questions, the devs there were able to help me out and answer some of my questions faster than I could of possibly been able to find the answers myself.  Of course as with any open source project community, you need to ask smart questions.  There are also local Django groups.  In fact I am looking forward to going to my first Django Toronto meetup.

Django is a really cool web framework for Python.  The designers of Django, really put a lot of effort in making a great framework.  The documentation is top notch.  The community is amazing.  To top it all, it is named after an amazing and cool French-Belgian jazz guitarist Django Reinhardt.  If you are into jazz or swing music I highly recommend checking out his music.

Using Python in Anger

 All the theory in the world, does not help if you can not put theory into practise.  Same goes with the theory of using Python in a professional setting.  Coming from the Java EE and Android worlds, I do expect certain tools.  Now since Python is interpreted on the fly, one can skip all of the compilation and packaging nonsense of Java.  (Yes, I know that Python code gets compiled into bytecode on the first run.  Why can’t Java do the same?)  As for API documentation, Python is quite a bit different than Java.  However epydoc does great job of API documentation, Sphinx does an incredible job with making in-depth documentation using the reStructured Text format.  Currently for my projects I have used epydoc mostly, and I’ve built a script to run it in Linux.  I have yet to figure out how to make a pure Python automated script for it, and I might switch to Sphinx if my documentation becomes extensive enough.
Interestingly in a company that I interviewed at recently, also uses Python and namely Pylons/Pyramid for the backend.  Also they have some Django components to if I remember correctly.  In fact a few of the people involved commercially in Python use a combination of Pyramid/Pylons, Django and some Zope, depending on the job.

Leave-A-Note Portfolio Project

While I have learned a lot about Django in the past few months, I still would hesitate on quitting my day job and becoming a Django consultant.  :P  However I have started working on a portfolio project to highlight my Django, web frontend and Android development skills.  This project is a simple device-to-device pull messaging system called  Leave-A-Note licensed under the AGPL.  While I could of easily made this application in PHP, I decided to build the server-web application in Django.  (I’m also working on another web app, but I can’t reveal much about that project yet.)  I am still in the initial phase of development, so there isn’t much to see yet.  I might build a few of the projects in the Practical Django Projects before I head off on my own.
All in all I am quite excited by Django.  Hopefully with it, I can start adding in more Python development in my professional programming repetiore, if not working with Python outright.