You really want to do that? Easy enough, just add danceric-hangman[at]appspot.com to any wave, and start playing hangman game in text mode.

The code is on github at: http://github.com/danceric/wave-hangman

P.S.: As always, keep in mind that this was done to learn a little more bit about Google Wave and Python, so don’t expect the best game ever.

P.P.S.: I know the name danceric-hangman may sound self-centered, but it’s hard to find a free appID on appspot nowadays.

Some other projects haves slowed down my Python learning progress, but things are now geting back to normal. I’ve read a lot of documentation on Python (the official tutorial, PQR, diveintopython2-3, etc), and now it’s time to get my hands dirty.

I’ve build a simple blog (you know, ‘blog’ is the new ‘hello, world!’) using the webapp framework on Google Appengine (code/demo). Next planned steps are, in no particular order:

I’ve been working with Python and Django lately, here’s my configuration for future reference

The setup

I’ve got a test website built with Django and it’s running on a VPS (at Linode). the setup is as follow

  • Ubuntu 8.04 LTS with the Apache preform MPM package
  • python virtualenv
  • Django
  • mod_wsgi 2.4

Some explanation on the setup

As I plan to have many website on this server, I want to be able to use different version of some libraries (I don’t always have the time to update every website for each new Django release). That’s why I use Virtualenv

My setup use apache prefork because of php.net recommend it (and Passenger needs it, keep an open door for Sinatra) and mod_wsgi in deamon mode (wsgi 2.4 (branches/wsgi-2.x) from svn because of the backport addsitedir())

Setting up virtualenv

python virtualenv.py --no-site-packages pythonenv
cd pythonenv
source bin/activate

from there, path have been changed to user this virtualenv, I can install django the usual way

easy_install django 

(type deactivate to return to your standard path, read more about virtualenv)

Setting up django

Once your virtualenv is activated, you can build your Django apps the usual way

Installing mod_wsgi

mod_wsgi >= 2.4 is needed, because of the backporting from 3.0 of the reodering of the path afet a call to addsitedir()

As of today, 2.3 is the stable release, so we’ll fetch 2.4 from svn:

svn co http://modwsgi.googlecode.com/svn/branches/mod_wsgi-2.X mod_wsgi-2.x
cd !$
.configure
make
sudo make install

On my Ubuntu 8.04, the apache configuration was done automatically (you may have to run a2enmod mod-wsgi).

Setting up the Apache config for mod_wsgi

I’ll assume here that you know how to use virtual host. Next step is to create the config for mod_wsgi

in /etc/apache2/sites-available/django-test

<VirtualHost *>
    ServerName example.com

    Alias /static/ /path/to/my/django/project/static/

    # I'm new to mod_wsgi
    # throw a lot of output so I can debut and learn what's happening
    LogLevel info

    WSGIDaemonProcess example.com display-name=%{GROUP}
    WSGIProcessGroup example.com
    WSGIScriptAlias / /path/to/your/django/project/application.wsgi

    <Directory /path/to/my/django/project>
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

Some explanation on the settings

  • WSGIDaemonProcess: mod_wsgi default to embeded mode, which give you more performance, but have the downside that you need to restart apache when you want to reload your application to apply changes. Using this directive will switch it to Daemon mode (with it, you can reload your application by touching your application.wsgi
    • display-name is used to show a different name when you run ps, will show the groupname as {wsgi:example.com}
    • there’s a lot of other parameters that you can give, like threads=15, or process=2. go read the doc if needed
  • WSGIScriptAlias: the first / tell mod_wsgi to take care of every requested url that start with a slash, and use the applications.wsgi to handle it

Testing the apache/mod_wsgi setup

Just to test that your mod_wsgi is working with this config, create an application.wsgi file in your django project folder (were not involving django for now)

def application(environ, start_response):
    start_response('200 OK', [('content-Type', 'text/html')])
    return ['Hello World']

You may now restart apache (sudo apache2ctl restart (or graceful)) an nagigate the virtual host with your browser, you should see Hello World. If not, you can check the apache log file

/var/log/apache2/error.log

Setting up Django to act as a wsgi application

If everything is working, you may now create a wsgi application for your django project. Replace the file named application.wsgi in you Django project folder with the following content:

import os
import sys
import site
 
# One directory above the project, so project name will be needed for imports
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
 
# with mod_wsgi >= 2.4, this line will add this path in front of the python path
site.addsitedir(os.path.join(root_dir, 'lib/python2.5/site-packages'))
 
# add this django project
sys.path.append(root_dir)
 
os.environ['DJANGO_SETTINGS_MODULE'] = 'subyt.settings'
 
import django.core.handlers.wsgi
 
application = django.core.handlers.wsgi.WSGIHandler()

The important line here is the ‘site.addsitedir’ one. That set your virtualenv path at the top of the python path, so your library are used before those of the main python install

Setting up a web.py project with mod_wsgi

Sometime I have some simple page/apps where that would be overkill to use Django, so I also use web.py. It’s almost the same setup, but instead of calling this in your webpy-app-file.py

application = web.application(urls, globals())

just call this

application = web.application(urls, globals()).wsgifunc()

Now point you WSGIScriptAlias to this file and voila! Please note that if your using web.py with virtualenv, you’ll have to add the site.addsitedir() as in the Django example

update 2009-04-01: if you’re not using virtualenv (which I don’t recommend), you can follow some easier instructions, as Django documentation now recommend mod_wsgi for deployment

Three weeks ago, I have decided to use my free time to re-learn python. My first step was reading the book Python: Visual QuickStart Guide which is really pleasant to read, and a short one too (found the dead-tree version for only $15).

I also keep these reference close to me at all time

I’ve created a couple of easy scripts first, and then I have started to play with web frameworks. First one was web2py. It was fun, but the website looks like a java ad, which scared me. I may come back to it later. Then I went to try web.py, prototyped a website in a snap, and got it running easily.

Next thing I’ve done, is reading the “Definitive Guide to Pylons”, because the philosophy behind pylons interest me. Putting together a lot of great component that can be reused outside of Pylons is a neat idea, and it play well with the community. The book taught me a lot of thing, but it makes Pylons look overcomplicated… I feel that I’m not going to save any time by using this framework. Maybe I wasn’t ready for that. But still, I have learned about a lot of new stuff from this book: mako, sphinx, sqlalchemy, virtualenv and wsgi.

I’ve had some apprehension with Django, mainly because they seem to live in their own universe (don’t quote me on that, I may regret it later). But I have kept an open mind and tried it. Honestly, when coming from Symfony, Django is beautiful and really easy to grasp, I’ve got a Django website running on my VPS at Linode in no time.

Now before I go deeper with a selected framework, I also want to take a look at some other ones (i.e.: try their tutorial) and I want to learn/work more with PEP 333.

Ultimately I will choose a full stack framework to invest more time in it. Here’s my choice so far:

  • Django
  • Pylons
  • TurboGears
  • Web2py

I also want to find a small one to quickly test stuff, or create small web app and API (like Sinatra or Ramaze). Available choice seems to be:

  • CherryPy
  • Juno
  • web.py
  • Werkzeug

I know there is a gazillion other frameworks in python, but I want to use one with a good user base. Working alone is rarely a good idea.

to be continued…

From 1999-2002, I wasn’t doing a lot of web development, I was a Powerbuilder programmer. and on some project, I was working with QPL (http://qpl.gao.gov/). It don’t remember a lot from these glorious day, but the thing I do remember is how buggy Powerbuilder 6.0 was at the time (and how painful it was to modify a datawindow on any version up to 8.0), and how boring it was to code in QPL (things my have changes, I haven’t checked this project since 2003). To ease the coding I was parsing/modifying/creating code with a lot of Python scripts (and some Perl one). I’ve stopped programming in Python in 2003, because PHP was more trendy…

Now, after 6 years with PHP, I want to expand my horizon. I’ve tried Ruby, totally fell in love with Sinatra, the only downside of ruby is that it’s “trendy” right now. And I want to stay away from that for some time. Now that web development with Python is something realistic (was painful in the days of CGI imho), I want to get my Python skills back.

My first goal is to create a Youtube Niche engine with web.py. A small simple framework that will not get in the way as I’m re-learning Python. Then I will try both Django and Pylons to re-create my YouTube Niche engine, to see which framework feel more comfortable to me.

Having worked with the Symfony framework (in PHP land) a lot, I will probably start with Django, as it seems to have inspired a lot the development of Symfony. Then I will try Pylons, because it seem more decoupled, and that would be useful to be able to reuse stuff in non-web project.

to be continued…