Posts RSS Comments RSSTwitter 16 Posts and 13 Comments till now
This wordpress theme is downloaded from wordpress themes website.

Our days are numbered

Whenever I learn a new word in any language, I often find myself comparing that word with equivalent words in other languages. I was recently thinking about words for days of the week in various languages. For related languages, not only are there similarities in the words themselves, but the origins of those words also fall into a small number of distinct categories.

I chose three groups of related languages, partly for reasons of familiarity and interest, and partly to allow me to compare words within and across groups:

  • English and German (Germanic)
  • French and Spanish (Romance)
  • Arabic and Hebrew (Semitic)

Interestingly, the origins of the words for days of the week can almost all be placed into the categories Planetary, Pagan GodsReligious and Numeric.  The following table lists the words for days of the week in each language together with their meaning / origin:

The origins of words for days of the week

The origins of words for days of the week

What struck me immediately is that this table is strongly reminiscent of the Periodic Table.  This probably isn’t surprising considering that related languages have been placed adjacent to each other.

Head over to this Wikipedia page for a more complete study of the origins of the words for days of the week (but without the colourful table).

Be good to your colon

Programmers spend more time reading code than writing it (a fact well known by most programmers who tend not to publicise this to their employers).  It therefore stands to reason that (most?) programming languages should be designed as much for human consumption as for machine consumption and should be as readable as possible.

Python is a very readable language (a fact which contributes to its popularity) and has been termed “executable pseudocode” on account of its readability.  An aspect of Python which makes it readable is its avoidance of syntactic fluff, extraneous words and symbols which add nothing to the code’s meaning but serve to detract from it.

In the past I’ve felt somewhat negative about Python’s terminal colon “:”, the symbol used to terminate if, while, def and class statements and to signify the start of a new block of indented code.  For example:

if a == 1:
    b = do_something_cool()

def do_something_cool():
    return 'Doing something cool'

Even without the colon, it’s quite clear that we’re starting a new block of indented code because (a) the statement starts with the keyword if, while, def or class and (b) the next line of code is indented. For comparison, Ruby gets on just fine without the colon after its def statement. So why the need for a colon in Python? Is it syntactic fluff?

The Python FAQ explains that the colon enhances readability and helps editors with syntax highlighting and code indentation. Lets face it, any self respecting editor should be capable of parsing a line beginning with an if, while, def or class, so the “helps editors” argument is bogus. I do however buy the argument that the code is visibly more readable. But how does it enhance readability?

I’ve already mentioned that a programmer spends more time reading than writing code. What I haven’t yet suggested is that a programmer will often reread and scan the same code repeatedly to form a mental picture of a larger codebase. It’s what the eyes do when they’re scanning code that’s key to the importance of the colon. There is some evidence to suggest that the eyes linger at the beginning and at the end of a sentence when reading text and draw especially from visual cues at those locations. Let’s assume for the moment that this holds true for a line of code. So the visual cue heralding an indented block of code is clear at the beginning of a line of code, namely an if, while, def or class followed by an indented line. The only visual cue at the end of a line of Python code is the colon, and without the colon there would be no cue. So even though the colon is not strictly necessary, there is an argument that its existence is there for human consumption and aids readability.

When all’s said and done, the advantage of the colon is probably slight at best, and then probably only for a newcomer to the language. (This sort of advantage possibly completely vanishes for experienced users of any language). Never-the-less, on balance, I’m now happy it’s there!

Django JavaScript Integration: AJAX and jQuery

Django JavaScript Integration: AJAX and jQuery is a book about the building of Ajax-enabled web applications using Django and jQuery.  Django has rapidly shot to fame as the most popular web development framework for the Python programming language.  Similarly, jQuery has taken the Javascript world by storm as a client-side Javascript framework making the development of sophisticated browser based clients both easier and even more pleasurable than using Javascript alone.  The strapline to this book is: “Develop AJAX applications using Django and jQuery” and I would suggest that this describes the aim of the book more accurately than its title.

There’s a wealth of both online and dead-tree texts covering Django and jQuery, however by comparison, there’s far less information covering the integration of both technologies so the arrival of this book is timely.  I’m also always happy to see new books aimed at the more experienced Python programmer in a time when the rapid (and very welcome) growth in the adoption of Python has led to the recent publication of a large number of beginners’ books.

To get the most out of this book, a knowledge of Python is expected and a working knowledge of Javascript and Django highly recommended.  The author also makes occasional (and perhaps inevitable) comparisons between Javascript and the Java language in the first couple of chapters, however a working knowledge of Java is definitely not needed.

The first chapter covers Python and Javascript.  As a Django/jQuery developer you’ll be using both languages and the author provides some interesting comparisons between the two.  The author is also quite candid and realistic about the weaknesses of Javascript and its cross-browser incompatibilities whilst carefully highlighting its strengths:  “If you can figure out why Python is a good language, you can figure out why JavaScript is a good language.”

The second chapter gets stuck into the basics of jQuery and the constructs which simplify the implementation of Ajax.  The third chapter then dives into Django with a tour of Django validation and a detailed discussion of validation in general.  The remainder of the book builds a reasonably large web application with each chapter pulling together a good number of disparate features you’d want to provide in any self-respecting Web 2.0 application.  Autocompletion, form validation, server-side validation, client-side and server-side search and login handling are all described and integrated into the application.  Even the creation of a “favicon.ico” is mentioned to put a company logo on your users’ web browser tabs and make them look distinctive.

It quickly became apparent that this book  is not a regurgitation of “the same old stuff”, rather it makes the effort not only to show you what to do, but also to discuss why you do something in a particular way and how you can improve on it, leaving the reader with a deeper understanding.  For example, the book is quite happy to extend the provided Django classes where they fall short, and show validation of more unusual types such as GPS coordinates not natively supported by Django.  Another example is the book’s excellent treatment of validation discussing cultural awareness and the suggestion that a “less is more” approach to validation can sometimes make sense.

Apart from a couple of typos here and there (which are possibly restricted to my electronic copy), a minor annoyance is what I felt to be a rather unorthodox Javascript formatting style.  For example:

set: function(newValue)
   {
   var value = parseInt(newValue.toString());
   if (isNaN(value))
       {
       return false;
       }
   else
       {
       field = value;
       return true;
       }
   }

It’s quite possible again that this is a formatting issue restricted to my electronic copy (and I’ll investigate and update this review accordingly).  I also acknowledge that you can never please everyone with your coding style and layout!

The book stops short of helping you organise the inevitable growing mass of Javascript code, a difficult but increasingly important topic.  A little information around the modularisation of Javascript files or strategies and libraries for implementing MVC in client side code would have gone a long way.  Another aspect of the book which is notably glossed over is the topic of testing.  Testing can be hard, and testing web applications can be very hard, particularly those which rely on a lot of Javascript.  Admittedly this isn’t a book about testing, but implementing tests is a very important part of a developer’s life and a section or chapter setting the reader on the right path would have been welcome.

There are several parts of the book which deserve a special mention, however Chapter 11 particularly stands out.  The topic of usability is one often brushed over in technical books in favour of delivering more how-to’s and code examples.  The author devoted an entire chapter to usability, a chapter which I can only hope the authors of many web applications I’ve used might one day read.

I find it hard to characterise the author’s style of writing but I’d probably describe it as intellectual bordering on philosophical with a colourful vocabulary, a style which I enjoy but might not be to everyone’s taste.  An amusing example of the intellectual nature of the book can be found in Chapter 2: “Prototypal inheritance is more like the evolutionary picture of single-celled organisms that can mutate, reproduce asexually by cell division, and pass on (accumulated) mutations when they divide.”  I actually found this an interesting and useful analogy however it’s probably a little hard to relate to unless you remember your school biology!

In summary, I like this book.  I like the the fact that it’s filled with gems of information you won’t easily find online.  I like the colourful language and the interesting discussion around the concepts the author is conveying.  Most importantly, this book is written by someone who has clearly developed real web applications.  If you’re someone merely looking to get cracking on a project using Django and jQuery in the shortest time possible, then this book might disappoint.  But then again, the online tutorials and references are there to get you started and this book can take over where they leave off.

Finally, the author strikes me as someone both interesting and accomplished and I look forward to reading other books he might have in the works.

Pro Python – Book Review

A recent thread on the Python Northwest mailing list asked for opinions on Marty Alchin‘s book Pro Python.  I thought I’d reproduce the answer I gave and expand on it a little.

I’ve owned Marty Alchin’s first book, Pro Django, for some time and was very happy with that purchase.  Based on that, I decided to buy his Pro Python book last year.  Pro Python is targeted at readers who are proficient with basic Python but are looking to push their skills further.  Quite naturally there’s a large number of beginners’ Python books out there but a shortage of more advanced books so it was nice to see this published.

Marty Alchin starts his book with a refreshing approach.  Rather than regurgitating Python facts to the reader, he takes a step by step tour of The Zen of Python discussing how it’s philosophy can be practically applied to make your programming more Pythonic.  He then delves into traditional topics such as classes, objects and strings as well as development topics such as packaging and testing.

I like Marty Alchin’s style of writing and find it to be clear and concise.  Even if you’re reasonably knowledgeable about the advanced topics he covers such as metaclasses, descriptors, introspection and multiple inheritance, I think the book benefits from the fact that these topics are backed up with good examples of how they work, and just as importantly, how they might usefully be used in ways you might not have seen before.  In fact, Chapter 11 walks through the building of a real world Python library which can be found on PyPI (try pip install Sheets) using the principles outlined in the previous chapters.

The other aspect of the book I find very useful is the fact that it is based on Python 3, however all examples are annotated and compared with the “legacy” Python 2 equivalent where relevant.  I’ve gotten a lot more comfortable with Python 3 by reading this book and better understand the improvements in the language from Python 2 to Python 3.

This isn’t a book aimed at newcomers to Python, even if you have a lot of programming experience, as it expects a reasonable amount of basic Python proficiency.  It’s also a “thin” book in the sense that it gives each topic a light treatment rather than aiming to be a complete reference.  This may or may not suit your needs, however there’s plenty of reference material elsewhere both online (e.g. the official Python documentation) and in print.

By comparison, the other advanced Python book I’ve read (and reread!) is Python In a Nutshell by Alex Martelli.  It’s based on Python 2.5 and getting a bit out of date, but much of it is still very relevant for all Python 2.x versions.  (I think a Python 3 version might be in the works).  It’s a much heftier and more detailed book and acts as much a reference text as well as being a book you’d enjoy reading from cover to cover.

In summary, I’d recommend Pro Python to any intermediate level Python programmer who’d like to advance their Python skills with a clear and concise text.

N.B. I am in no way associated with Pro Python, Apress or Marty Alchin … except of course for owning the book!

Cracks on the surface

The recent freeze and thaw of the canal beside where I live has produced a beautiful natural phenomenon.  Cracks in the surface ice reminiscent of neurons with pronounced dendrites have appeared in random locations.  Stars in the night sky also spring to mind:

Neurons

Dendrites

Next Page »