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.
FrontPage - Storm - New ORM for PythonStorm 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.
Head First Labs :: Head First SQLPerhaps 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: Css, Decorators, Diff, Font, Googlegears, Greasemonkey, Learning, Mssql, Offline, Orm, Python, Sql, Sqlserver, Toread, Web.Py, Wind32, Work
Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST
Comments Off
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.
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
Tags: Ai, Apache, Bayes, Bayesian, Benchmark, Businessidea, Chess, Clustering, Database, Datamining, Excel, Filter, Games, Gui, Machine_Learning, Mysql, Opensource, Optimization, Performance, Python, Science, Sql, Statistics, Utilitymill, Weka
Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST
Comments Off
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 »
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.
YouTube - Ferris Bueller’s Day Off 10What’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.
Tags: Apache, Becky, Blogging, Comments, Community, Http, Linode, Movies, Music, Mysql, Optimization, Python, Socialsoftware, Utilitymill, Vps
Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST
3 Comments »
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