Converting to Python

30, Mar 2010

Contents

The Conversion is Complete

Due to everything that I've been doing at work, I've completely switched my allegiance from PHP to Django and Python. I got thrown into a large project that will when completed, be managing the work flow system for the Techrangers and the student and faculty technical support for the online course-ware that the university uses. I had to quickly learn my way around Python, and then on top of that, learn the Django framework for web development. After an initial slow start, I've finally gotten the hang of it, and I can say with confidence; I no longer want to write PHP code ever again!

Why Django?

What do I like about Django that causes me to rank it so much higher over PHP? This is the first time I have been exposed to a true framework, and I am soundly impressed with it. I've come to understand the purpose of a framework is to lessen the time spent writing boring code, to allow for more time to be spent writing the fun/interesting code. The prime example of this is the Django Admin.

The Django Admin comes with the Django install, and automatically creates the entire administrative portion of your website so that you do not have to. Nearly every dynamic web site (anything using a database) will have an Administrator's section, where the CRUD (Create, Read, Update, Delete) functions will happen. For the programmer, this is boring to create, as once you've created one admin back-end, you've basically created them all. So, Django has included the Admin site which, because of the way Django works, is tied in to every App (database table) and then presents a clean and sensible way for a user to enter in data. This eliminates a major hassle, and a significant investment of development time for the programmer.

What good is an Admin site without a user system to manage who has access to it? Once again, Django comes through. The Django user system provides a full fledged system to deal with many different users, and different permissions/ranks on all of them. Again, this is included by default in a Django site.

I could continue to go on and on about the multitude of things that Django has done that makes life easier for the developer, but the last thing that I'll touch on here is the way that Django handles forms. One of the great hassles for me has always been dealing with large and complex forms. While it is by no means perfect, Django Forms helps greatly in this process. Creating, displaying, validating, and processing a form have never been easier. When you create your form Class, you tell Django what types of values are acceptable in every field, and then Django handles all the validation from there. There are a great many field types built in with their own validation rules, allowing you to create fields such as a URLField(), a EmailField(), a DateTimeField(), a BooleanField(), and so on and so forth.

There are times however, when the default behaviour of a framework isn't exactly what you had in mind; when such an event happens, it is important that you are able to easily override the defaults to create the functionality that you need. Do you need to do some additional processing whenever a certain form is saved? No problem. All you have to do is declare your own .save() method on the form to override the default function, and you can take it from there. Need to create your own custom field type? Again, no problem, just extend the Django forms or models class to create your own.

Why Python?

All that praise for Django doesn't even begin to touch on the language that it uses. Like many other people these days, the first web development language that I ever became confident in was PHP. This is to be expected, as it has become quite popular, and therefore quite proliferate. However, it was my opinion (even before I started with Django) that PHP was an ugly language, in both appearance of the code, and syntactically. PHP is inconsistent in style and at times moves in random directions. While it will call a fatal error that will take down the entire site should it find a line missing it's precious semi-colon, it is willing to forgive many other affronts to orderly programming, such as soft type casting, allowing variables to be accessed before being declared, and many more things.

I've come to realize just how much I like the flow of Python code in comparison to PHP code. I find the lack of curly braces {} everywhere to be a welcome sight. Syntactically significant indentation helps code stay in an easily scan-able/readable layout. There is a solid set of guidelines put out by the developers of Python to help guide coding styles, helpfully titled Style Guide for Python Code, or commonly referred to as PEP8. This document is to go to reference for Python styling, and is followed very closely in all serious development projects. Having this guide further improves the readability and base ability for a newcomer to understand the code.

Another excellent thing about Python is that it is entirely Object Oriented. Before I'd been exposed to Python, I dove into OOP in PHP; this site is currently running on OOP code I wrote in PHP. While building it I came to realize it's purpose, and came to the opinion that working in an OOP method can simplify certain things dramatically. As I built this site, I made classes that I could include in other areas that allowed easy access to shared functionality, plus, I was able to move nearly all the processing of the site into these classes, which simplifies the actual core pages of the site - since all they have to do is include and utilize the existing classes to run everything. Because Python is an OOP based language, many of these classes are already written, and simply need to be imported to gain access to all their functionality.

Have a Sense of Humour About It

I've always had a soft spot in my heart for the Monty Python gang, famous for their Silly Walks, Spam (Spam Spam Spam Spam), Fish Slapping Dances, Philosophical Bruces', and a great many other comedic bits of genius. Somehow it seems only fitting for me to be working in a language filled stock full of references to them: how much faster is an Unladen Swallow?

Comments

This space intentionally left blank.

Have Something to Say?

Questions? Comments? Concerns? Let me know what you’re thinking.

  • You can use Markdown formatting here.