May 17th, 2008
First obtain a cursor
import MySQLdb
conn = MySQLdb.connect (host = “localhost”, user = “root”, passwd = “”, db = “dbname”)
cursor = conn.cursor()
Now execute mysql sql query
sql = “select COLA, COLB from T1 where COLC=%”
colc_value = “something”
cursor.execute(sql, [colc_value])
Now iterate over results
while(1):
data = cursor.fetchone()
if data == None: break
desc = cursor.description
dict = {}
for (name, value) in zip(desc, data):
dict[str(name[0])] = value
print dict
Tags: mysql, python
Posted in python | No Comments »
May 17th, 2008
import socket
host = socket.gethostname()
print host
Tags: python
Posted in cookbook, python | No Comments »
May 4th, 2008
Sometimes we want to iterate over a dictionary in sorted order of value. Here is the code:
d = {”a”:1, “b”:5, “c”:2, “be”:30}
for k in sorted(d.keys(), lambda x,y:d[x]-d[y]):
print “%s=%s” % (k, d[k])
# output is:
a=1
c=2
b=5
be=30
Tags: python
Posted in cookbook, python | No Comments »
April 25th, 2008
Here are some cool python one liners.
- Parse apache log using python: print 5th field
python -c “import csv,sys;f=csv.reader(sys.stdin, delimiter=’ ‘); print ‘\n’.join([r[5] for r in f])
- Generate frequency histogram from a text files of terms
cat file.txt | sort | uniq -c | sort -rn
- print 2nd column of a tab separated file
awk -F”\t” ‘{print $2}’
- perl search replace from files
perl -i -p -e ’s/old/new/g’
Tags: apache, cheat sheet, python
Posted in web technology | No Comments »
April 24th, 2008
These are some quick apache tips to improve the performance of your web site:
- compress your html/css/js content by making these changes to your httpd.conf
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css application/x-javascript
To see it working, install livehttpheaders addon in firefox, open Tools->Live HTTP headers, and fetch http://www.technologydb.com/. In case some headers are missing, clear your firefox cache. Here are the screenshots:


- cache images, css and js
ExpiresByType text/css “access plus 240 hours”
ExpiresByType image/jpeg “access plus 24 hours”
ExpiresByType image/jpg “access plus 24 hours”
ExpiresByType image/gif “access plus 24 hours”
ExpiresByType application/x-javascript “access plus 24 hours”
This will avoid browser making an http call to server for these content types.
- Avoid using too many domain names. Each domain require a DNS lookup which is expensive.
- Keep limited number of css and javascripts files. Browsers generally limit parallel number of fetches.
For a full list of advanced tips on website performance you can view this excellent page maintained by Yahoo Exceptional Performance (http://developer.yahoo.com/performance/)
Tags: apache, web
Posted in web technology | No Comments »
April 23rd, 2008
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
Posted in Uncategorized | 1 Comment »