Monday, January 19, 2009

pygame, pywebsite -- a website with joystick controls???!??!

Here's a song program written with a pygame style api:

import pygame
pygame.init()

counts = {}
going = True

while going:

events = pygame.event.get()

for e in events:
if e.type == HTTP:

if e.path.startswith('/song/'):
song_id = e.path[6:]

if e.method == "POST":
counts[song_id] = counts.get(song_id, 0) + 1

e.write(counts[song_id])

elif e.path == "/":
if e.method == "GET":
data = ','.join(['%s=%s' % (k, v) for k, v in counts.iteritems()])
e.write(data)

elif e.method == "DELETE":
counts.clear()


elif e.type in [QUIT, KEYDOWN, JOYBUTTON, MOUSEDOWN]:
going = False


Note this is not in pygame yet, but is just the possible API. So it can't be benchmarked since it doesn't really exist. It's Vapour-ware ™, so only good for making clouds -- not websites.

Compared to the others*1 it has:
  • less code (measured in LOC, or # of characters)
  • simpler code (no need to write functions or classes).
  • JOYSTICK control


  • Note: this is a response to this series of posts about a basic REST application written in python.

    Starting with "Eric Florenzano's deliberately minimalist WSGI application illustrating a bare metal WSGI application with RESTful behaviour has drawn out from other Python web application framework communities a number of useful comparisons"

    1. Christian Wyglendowski's entry using cherrypy
    2. Tim Parkin's restish approach
    3. Grok showing real object persistence by Martijn Faasen.
    4. A Werkzeug solution also by Tim Parkin.
    5. Carlos de la Guardia provides a solution via repoze.bfg, which, like Grok, shows how Zope3 technologies can be used.
    6. Then mike Watkins does the same thing with qp http://mikewatkins.ca/2009/01/18/resty-applications-in-qp/
    7. this post (self post) where we add Joystick control to a Vapour-ware ™ REST framework.

    3 comments:

    Michael Watkins said...

    I think you are on to something... forget REST, all web sites should be Joystick-accessible!

    Marius Gedminas said...

    Nice, but I cannot help noticing that your indentation levels are a bit messed up. The "elif e.path == '/':" bit should be aligned with "if e.path.startswith('/song'):", and not with "if e.type == HTTP".

    illume said...

    Indeed! The indentation was very broken. Thanks.