Skip to main content.
November 22nd, 2007

Mini Searches with Answers

These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but still super useful.

CSS Font and Text Style Wizard
Neat resource to play with css fonts in realtime and see how different things look

FrontPage - Storm - New ORM for Python
Storm is an object-relational mapper (ORM) for Python developed at Canonical. The project has been in development for more than a year for use in Canonical projects such as [WWW] Launchpad, and has recently been released as an open-source product.

PythonDecoratorLibrary - PythonInfo Wiki
A collection of examples of using Python decorator syntax. What’s a decorator? it’s a way to make your source code harder to read :-)

SQL Server 2005: TableDiff.exe GUI
Compare two tables for differences in MS SQL Server. I haven’t tried it yet but it looks useful.

Google Code FAQ - GearsMonkey: Google Gears Greasemonkey to take Wikipedia offline
Use Google Gears and GreaseMonkey to make any website work offline.

Head First Labs :: Head First SQL
Perhaps this would be a good book to teach SQL with? I learned a lot from "Head First Java" and "Head First Design Patterns". Though I’ve since forgotten everything.

Tags: , , , , , , , , , , , , , , , ,

Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST

Comments Off

November 19th, 2007

Mini Searches with Answers

These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but still super useful.

Optimize MySQL for a low memory machine
May not work for MySQL 5?

Orange - Python Data Mining Suite (Weka Alternative?)
Orange is a free component-based data mining software. It includes a range of preprocessing, modelling and data exploration techniques. It is based on C++ components, that are accessed either directly, through Python scripts, or through GUI objects

MySQL AB :: MySQL 5.0 Reference Manual :: 6.2.1 Optimizing Queries with EXPLAIN
Place "EXPLAIN EXTENDED" before your select statement and MySQL will give you its executation plan. From there you can see where you need to add indices.

MySQL AB :: MySQL 5.0 Reference Manual :: 6.2.20 Other Optimization Tips
More MySQL query optimization tips

Autobench is a simple Perl script for automating the process of benchmarking a web server
Perhaps an alternative to Apache’s ab?

Shredder Computer Chess Download - Play chess online with Shredder
Online Chess game. Does it use AJAX too?

Tags: , , , , , , , , , , , , , , , , , , , , , , , ,

Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST

Comments Off

November 18th, 2007

Python - Using DBUtils

I wanted to try using the DBUtils package update my homegrown MySQL access module to pool database connections. I ended up deciding not to use it because it seemed to just hang. Perhaps I’m still not understanding how to use it properly. Since I’m already using web.py, I figure I might as well just switch over to using web.py’s db module to handle my database access instead. It already handles connection pooling.

In any case, here are some tips for getting it to DBUtils PoolDB to work at all:

TypeError: Cannot determine DB-API 2 module.

I got this error when I made my own creator function to return a db instance. The documentation claims you can do this. Maybe I was doing something wrong. To fix it, just give it the MySQL module as the creator.

Example:

import MySQLdb
from DBUtils.PooledDB import PooledDB

POOL = PooledDB(creator=MySQLdb,mincached=4,maxcached=10,maxshared=0,
    maxconnections=20,blocking=False,maxusage=100,setsession=['SET AUTOCOMMIT = 1'],
    passwd=PASSWORD,db=DATABASE,user=USERNAME,host=HOSTNAME)

...

TypeError: ‘database’ is an invalid keyword argument for this function

I was originally passing PooledDB the keyword “database”. You want to use “db” instead. See above example.

Here is the relevant part of the final code that I came up with, integrating DBUtils into my database access layer module. But alas it seems to hang when I used it. Of couse something else could have been wrong on the server and it’s not DBUtil’s fault at all, but web.py’s module should work, I won’t investigate further for now:

#! /usr/bin/env python2.5
"""
Updated MySQL database access module to use connection pooling via DBUtils.
"""
import sys
import MySQLdb #<-Definately need this (http://sourceforge.net/projects/mysql-python)
from DBUtils.PooledDB import PooledDB #http://www.webwareforpython.org/DBUtils

#-----------------------------------------------------------------------------
#Connection Info
HOSTNAME='127.0.0.1'
USERNAME='user'
PASSWORD='1234'
DATABASE='pets'
#-----------------------------------------------------------------------------

POOL = PooledDB(creator=MySQLdb,mincached=4,maxcached=10,maxshared=0,
    maxconnections=20,blocking=False,maxusage=100,setsession=['SET AUTOCOMMIT = 1'],
    passwd=PASSWORD,db=DATABASE,user=USERNAME,host=HOSTNAME)

def query(SQL_stmt,values=None):
    """You provide a valid query, I return the results
    If you provide values, I try to let the DB api build the final SQL statement.
    In this case keep in mind you shouldn't use 's around strings in your sql,
    I guess the DB api does that for you.
    """
    db=POOL.connection(0);cur=db.cursor()
    if values:
        cur.execute(SQL_stmt,values)
    else:
        cur.execute(SQL_stmt)
    result=cur.fetchall()
    cur.close(); db.close()
    return result

Here’s the original code, and what I’m currently using until I switch over to web.py’s db module. Should I have any performance issues with this that DBUtils or connection pooling in general could help?

"""
Just a really easy to use library holding common MySQL cruft so I don't have to
ever type it out again.

No connection pooling.
"""
import sys
import MySQLdb

#-----------------------------------------------------------------------------
#Connection Info
HOSTNAME='127.0.0.1'
USERNAME='user'
PASSWORD='1234'
DATABASE='pets'
#-----------------------------------------------------------------------------
global CUR
CUR=None #Singleton Thingy to hold db cursor

def _get_DB_connection():
    "Setup the database connection"
    db=MySQLdb.connect(host=HOSTNAME,user=USERNAME,passwd=PASSWORD,db=DATABASE)
    #Insert's don't seem to happen without the autocommit line below
    #see http://www.thescripts.com/forum/thread42620.html
    db.autocommit(True)
    cursor=db.cursor()
    return cursor

def exec_w_reload(cur,*args,**kwargs):
    """FastCGI leaves connection open forever, so it times out, this catches that
    error was OperationalError: (2006, 'MySQL server has gone away')"""
    if not CUR:CUR=_get_DB_connection()
    try:
        CUR.execute(*args,**kwargs)
    except MySQLdb.OperationalError, Err:
        CUR=_get_DB_connection()
        CUR.execute(*args,**kwargs)
    return db

def query(SQL_stmt,values=None):
    """You provide a valid query, I return the results
    If you provide values, I try to let the DB api build the final SQL statement.
    In this case keep in mind you shouldn't use 's around strings in your sql,
    I guess the DB api does that for you.
    """
    global CUR
    if values:
        CUR=exec_w_reload(CUR,SQL_stmt,values)
    else:
        CUR=exec_w_reload(CUR,SQL_stmt)
    return CUR.fetchall()

Posted by Greg Pinero (Primary Searcher) as Python, SQL at 4:46 PM MST

1 Comment »

November 13th, 2007

Mini Searches with Answers

These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but still super useful.

What to tune in MySQL Server after installation | MySQL Performance Blog
Even though you can tune quite a lot of variables in MySQL Servers only few of them are really important for most common workload. After you get these settings right other changes will most commonly offer only incremental performance improvements.

How to Suggest a Default File Name from CGI Output - HTTP/1.1: Appendices
Is your webpage generating say a PDF file, but the browser prompts you to save it as mywebpage.html? To fix that you can specify a filename in the header like this:

Content-Disposition: attachment; filename="fname.ext"

Disqus | Comment Management for Blogs and Websites
This looks like an excellent comment system. I’m considering using it for Utility Mill instead of coding comments myself. Any thoughts?

Ferris Bueller’s Day Off (1986) - Soundtracks
eh, random search

YouTube - Ferris Bueller’s Day Off 10
What’s that song that plays when Ferris Bueller is running home at the end? It was also in Family guy when Stewie runs to the pool. The song is called march of the Swivelheads by the english Beat. Go to time 5:13 on this video to hear the best part.

Great Utility Mill Review Yay!
(Doesn’t really belong in Mini-searches but it’s such a nice review ;-)

Tags: , , , , , , , , , , , , , ,

Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST

3 Comments »

November 8th, 2007

How To Raise a “File Download” Dialog Box for a Known MIME Type

Have you even clicked on a link and instead of going to the page your browser asks you what program you want to use to view an HTML file? Or any other file format you browser should normally handle without prompting you.

Well, for a short period of time, I actually wanted this effect for my own website.

After some searching it turns out the answer is to simply send “Content-Disposition: attachment” in your HTTP headers.

Posted by Greg Pinero (Primary Searcher) as Other at 1:11 AM MST

Comments Off

« Previous Entries