Python, Django – The Learning Saga

Saga – a heroic narrative :)

I first coded in python in October 2009. I had an idea for a chess website, and decided to try out Django. I had coded a fair bit in Ruby, and explored rails too. So, the dynamic aspect of python and magical qualities of Django did not overwhelm me. It took me just 7 days to launch the website. The users could solve chess combinations. So, I consider it a small success. But, then I did not take the site any further. Now, in the beginning of 2011, I came up with the idea of adding a new feature – Solitaire Chess. This time around, I had to dig deeper in python. And, these are some of my learnings -

  • TypeError: ‘module’ object is not callable

    I needed a class which would generate FEN string, based on the position on the chessboard. Now, FENgenerator.py had class FENGenerator. The module name is FENgenerator(the name of the file itself). I imported “FENgenerator”, and tried
    to create an object, FEN = FENGenerator(), and got the above “TypeError”.

    In python, everything is an object, including a module.The above import statement, does not import names in the module. So, to create an object, I had to do – FEN = FENGenerator.FENGenerator

  • The scope of variable “pleasant shock” :)
  • The code –

    def printmatter(self,matter):
    if matter == "Urgent":
    givep = "yes
    else:
    givep = "low"
    print givep

    I was able to access a variable declared inside “if” block”, outside the “if” block
    More here,
    stackoverflow
    scope

  • __init__.py required for your module to be seen
  • Session variables can be used in templates (Django)
  • easy_install is equivalent of rubygems
    easy_install django-extensions -> downloads all the packages from an online repository
  • I used TDD approach to develop FEN generation logic. My first exposure to tests in python. Each test method has to start with “test_everything_is_ok”.