Archive for the ‘Uncategorized’ Category

Existing MySQL database structure to Web2Py table definitions

Web2Py is a nice Python framework for rapid development of web-based applications. I’ve been using it quite a bit in the past two years, and got accostumed to the ease and quickness of development with it.

Recently I’ve been tasked with the rewrite of a big PHP-based website, with an existing huge database (60+ tables, millions of rows). Since the database seemed to be well-designed, I decided that I’d base my rewrite around it.

For this, I needed the whole database’s structure as a web2py model definition. Doing this by hand would be too tedious, so I decided to write a small Python script to do the work for me.

This is of course far from a full converter, as it doesn’t include all possible variable types that a column might have, only the ones I encountered in the database I processed, but I hope that this can save some time to someone who’s trying to do the same thing. Feel free to use my script as a starting point, or unmodified. Just don’t hold me reliable for any damage that might occur. :)

It also ignores defaults and other extra info.

#!/usr/bin/python

import os, sys
import MySQLdb

#use your own settings here
conn = MySQLdb.connect (host = 'localhost',
  user = 'root',
  passwd = '',
  db = 'shop')

cur = conn.cursor ()
cur.execute ("SHOW TABLES")
tables = cur.fetchall()
for t in tables:
  print 'db.define_table ("%s",' % t
  cur.execute ("DESCRIBE %s" % t)
  cols = cur.fetchall ()
  for c in cols:
    n = c[0]
    t = "UNKNOWN"
    l = ''
  if c[1][:3] == 'int' or c[1][:7] in [ "decimal", "tinyint", "mediumi", "bigint(" ] or c[1][:8] == "smallint": t = 'integer'
  if c[1][:7] == 'varchar':
    t = 'string'
    l = c[1][8:-1]
  if c[1][:4] == 'char':
    t = 'string'
    l = c[1][5:-1]
   if c[1] in [ 'text', 'datetime', 'date', 'time' ]: t = c[1]
   if c[1] in [ "mediumtext", "longtext" ]: t = "text"
   if c[1] == 'float': t = "double"
   if l:
     print '     Field ("%s", "%s", length = %s),' % (n, t, l)
   else:
     print '     Field ("%s", "%s"),' % (n, t)
   if t == "UNKNOWN":
     print c[1] # to review unknown col types
   print "     migrate = False)"

cur.close ()

conn.close ()

IRC Uno bot for Phenny

In my free time I like to hang out on a particular IRC channel, where we occasionally play some simple games. One of our favorite was Uno, a simple mIRC script that hosted Uno games. Since it is a mIRC script, it’s difficult to keep on the channel all the time, so I decided to clone it for Phenny, the Python IRC bot.

It was a nice exercise, and I think the end result is not so bad, so I’m making the code public.

Features:
- slightly modified UNO gameplay
- unlimited number of players in a round (minimum 2)
- logging of total score, games played, won, and total time spent playing per nickname
- separated strings, so localization to other languages should be easy

How to set up:
1. Download the unobot.zip file below, and extract the unobot.py file from it.
2. Install phenny. Edit the ~/.phenny/default.py config, add the unobot.py file with path to the ‘extra’ modules. Set up the other settings as desired.
3. Edit the unobot.py file, set the channel’s name (same as in default.py), and change the score file path. Make sure that Phenny has the proper rights for writing the file.
4. All set, start phenny and enjoy playing, by typing the .uno command in the channel!

Known issues:
Note that ordinary IRC nicknames are used to identify the players. The bot cannot react to a player leaving the channel or timeouting, either the game must be stopped (if the game owner quit then someone will have to take their nick temporarly to do the .unostop action) or someone must step in with the same nickname.

Download unobot.zip

Uno Bot for Phenny is licensed under the FreeBSD license.

For more games for Phenny visit Phenny Games!

LazyClick won in the Firefox Mobile Add-on Challenge!

It was a great surprise when I got the news that my humble Firefox Mobile (aka Fennec) add-on was among the ten best add-ons submitted for the challenge, especially since I saw that there are some really great and innovative creations emerging in the AMO site.

I never won anything worth mentioning, so it will be very exciting when I get the new N900, a gadget that is every geek’s dream! It’s called Mobile Computer and not Mobile Phone for a reason, after all! :)

I’m planning some new things for LazyClick, first of all supporting the desktop version of Firefox (it will only support version 3.6+, because the addon uses some newer APIs that are not available for current versions).

The next thing will be to try to port LazyClick to the default Maemo browser, which shouldn’t be too hard since that browser also uses Mozilla technology (there’s already a bunch of add-ons that are common to the two browsers). I’ll wait with this one until I have the actual N900, it will be easier to test that way.