python


I got a copy of Computer Programming for Kids and other Beginners for review, I was interested because I have always thought that someday I would like to write a programming book for kids — I would have given my right arm to have this book as a kid! Here are some things I have always considered and how this book taught them:

How to get “out of the dos window” ?
Whenever my husband sees me typing at the console, he calls it the “dos window!” and he thinks we could move past that! I explain that you can’t point and click your way through life. But, visual is the way people think of UI’s now and it makes the program seem “real” if it has a popup box rather than a prompt on the console. Right off the bat, this book starts out introducing EasyGUI. one example of usage is

user_response = easygui.msgbox(”Hello there!”)

Nice huh? easy and straight forward. I think a new programmer seeing progress like this is pretty cool and probably very encouraging!

Looping
Whenever I have tried to teach looping, i have started out with arrays and indexes. Starting from 0 to “count” has always been confusing. This book uses ranges to loop. Starts at one and keeps it simple. Great idea!

Game
There is a chapter on PyGame and has a simple ski game. Reading through the code in the book gives you an idea of what a game is like and it annotates the sections of code to help you follow. Putting this in the middle of the starting section is cool, it keeps the reader from being all about syntax and seeing real code help them keep the end goal in mind.

More Syntax
The book continues with functions, objects, modules … but then another game chapter. This goes through some simple examples of graphics. Its pretty good I think, i think its easy for beginners to look sight of what they are making…syntax really isn’t all that fun. But i think this book is going to keep someone interested.

More on GUI
PyCard is another GUI library to help. I haven’t heard of this, but it looks pretty cool. Looks pretty easy to use.

String Formatting and File IO
This is something that is always at the beginning of a book. How interesting that it is last. I think because its not very interesting! Really how interesting is it to format a number. This chapter ends with a hangman game where the words are stored in files. Thats cool, something useful with files.

Infinity and Beyond!
The book ends with a few chapters to inspire the new programmer, computer simulations and hints on where to go next.

Overall an excellent book to inspire and keep a new programmer interested. Young and old alike. This book has fun stuff and tools to make it easy to learn (PyGame and PyCard). I’m actually wanting to go try some of this now…

Alright, ok! I am setting my ruby and rails down for a bit to give python and django a spin. I’ve been bored with programming lately and need to try something new. So what the heck! Lets see how this goes.

Since I am not a guy — I read the instructions first! (haha). I open the README file and it says to read install.txt then proceed to the tutorials in the docs section. Sweet! Being as I an off-the-grid commuter, I have to rememember to download all documentation BEFORE I leave the house in the morning.

First thing I had to do was install Django. I already had Python 2.5.1 installed on my mac. You have to be connected to the net to install Django, unless you already have the python utility “setuptools”. Oh crap! Hopefully I have it! I did a search and found it was already installed. Whew! I untar the Django tarball and type “python setup.py install” … rock and roll!

Now, proceeding in orderly fashion to tutorial01.txt I see the command to create a django site:

django_admin.py startproject samplesite

BURN!!!!

-bash: django_admin.py: command not found

HUH! :( … ok.. so maybe django didn’t install right. I do a search and I found it installed in Python/2.5/site-packages/django/bin .. oh, doh! I used an _ (underscore) instead of a - (dash).

*smack forehead* … as often happens with a new thing, I get way to excited and forget the small details. The devil is in the details!

Lets try again:

django-admin.py startproject samplesite

HEY! It works, imagine that. Using the right command works. :) yay

Hmmm.. contrast the starting struture of an new Django site with Rails:

Django:

-rw-r–r– 1 nstowe 499 0 Apr 14 08:10 __init__.py
-rw-r–r– 1 nstowe 499 153 Apr 14 08:12 __init__.pyc
-rw-r–r– 1 nstowe 499 546 Apr 14 08:10 manage.py
-rw-r–r– 1 nstowe 499 2812 Apr 14 08:10 settings.py
-rw-r–r– 1 nstowe 499 1763 Apr 14 08:12 settings.pyc
-rw-r–r– 1 nstowe 499 233 Apr 14 08:10 urls.py
-rw-r–r– 1 nstowe 499 253 Apr 14 08:13 urls.pyc

Rails:

Interesting… Moving on! Next part of the tutorial is starting the server:

python manage.py runserver

Yay, the server starts right up.

I edit the settings.py file and tell my site to use sqlite3 and dbfile of samplesite_development.db (wondering, does django use different dbs like rails? dunno, but I am used to this convention now!)

The last section of the settings file has a list of installed apps (seems like the Rails equivalent of a plugin):

INSTALLED_APPS = (
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.sites’,
)

According to the tutorials these are included for convenience and each of them have at least one table. So we need to create the tables –

python manage.py syncdb

This creates all the missing tables for anything in the INSTALLED_APPS. When I did it, it asked for an admin name, password and email. Thats pretty cool, I don’t recall any interactive propmpts for any rails plugins

Quote from the docs:
” Django comes with a
utility that automatically generates the basic directory structure of an app,
so you can focus on writing code rather than creating directories.”

Great, I hate creating directories. Actually, I believe one of the benefits of having a tool create a basic site structure is consistancy. Another Django developer can walk in and go “oh yeah, I know where stuff is.”

As I read the tutorial01.txt I realize that i just created a django project — not an app. Now to create my app, a voting application.

It creates a directory containing the following:

-rw-r–r– 1 nstowe 499 0 Apr 14 17:05 __init__.py
-rw-r–r– 1 nstowe 499 57 Apr 14 17:05 models.py
-rw-r–r– 1 nstowe 499 26 Apr 14 17:05 views.py

Interesting, a different concept then rails. In Rails when you do

rails my_awesome_site

It create a webapp structure or one site. Not very easy to combine two Rails apps into one.. In fact, as I know it, damn near impossible. Best way to do that is subdomains as I see it.

Moving right along.

Time to create model files.

To define a model for a pol in Django:

class Poll(models.Model):
question = models.CharField(maxlength=200)
pub_date = models.DateTimeField(’date published’)

A migration file in Rails

create table as |t|
t.column :question, :string
t.column :pub_date, :datetimeo
end

and then the model file simply is:

class Poll
end

Rails, as you probably know, uses instrospection to figure out the field names…contrary to the approach that Django takes.

One of the differences is that with this one file, Django has the creation of teh table AND the model defination, where was Rails has two files. Interesting.

Ah, here we go. Since we created this application Polls we need to “add” it to our projects, you know where we saw those default “plugins” I called them.

INSTALLED_APPS = (
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.sites’,
’samplesite.polls’,
)

There is our poll app.

Now we need to generate the sql:

python manage.py sql poll

Thats the command that shows you what the SQL will look like, you need to run this command to actually execute that code — sync up the db!

python settings.py syncdb

In Rails, to run a database migration you would do:

rake db:migrate

In Rails all the database migration have a version number, making it easy for you to go “up” a version and “down” a version. This is a nice concept, but its a nightmare with a team more than 2 people! So this is no longer a novel concept to me. I think I’d rather let subversion do my version control.