Python
September 17, 2011
I love Python
import glob, os, re, string
def rename(dir, pattern):
for path_filename in glob.glob(os.path.join(dir, pattern)):
title, ext = os.path.splitext(os.path.basename(path_filename))
new_title = '''%s''' %(re.sub(r'[\W]+','-',title))
print new_title.lower()
os.rename(path_filename, os.path.join(dir, new_title.lower() + ext))
rename(r'/home/phollan2/crc_papers_2011',r'*.pdf')
Posted by pj at 01:28 PM | Comments (0)
September 03, 2011
Cosign with Django
Django | Authentication using REMOTE_USER | Django documentation
Posted by pj at 06:53 PM | Comments (0)
October 09, 2009
PostgreSQL version of do_sql.py
import sys
import postgresql
from read_config import read_config
import time
import string
import re
def parse_insert_sql(sql):
response = {}
da_list = re.split("[\s\(\)]+", sql.lower().strip())
print(da_list)
ma_table = da_list[2]
if da_list[0] == 'insert':
response['is_insert'] = 1
if "returning %s_id" % (ma_table) in da_list:
response['sql'] = sql
else:
response['sql'] = sql + " returning %s_id as insert_id" % (ma_table)
else:
response['is_insert'] = 0
response['sql'] = sql
return response
def do_sql_query(db_config_file, sql, debug):
response = {}
results = ()
"""
Takes config file name, your SQL and a debug parameter.
Returns a dictionary with a boolean, an error and a warning string and
a list of results dictionaries.
"""
connection_map = read_config('%s.ini' % db_config_file, debug)
(host, user, passwd, db) = (connection_map['connection_parameters']['host'],
connection_map['connection_parameters']['user'],
connection_map['connection_parameters']['password'],
connection_map['connection_parameters']['db'])
connection = postgresql.open("pq://%s:%s@%s/%s" % (user, passwd, host, db))
insert_response = parse_insert_sql(sql)
if insert_response['is_insert'] == 1:
sql = insert_response['sql']
try:
results = connection.prepare(sql)
response['results'] = results
response['status'] = 1
if insert_response['is_insert'] == 1:
response['insert_id'] = results.first()
return(response)
except:
if debug == 1:
print ("\nPostgreSQL error: %s\n" % (sql))
response['status'] = 0
response['error'] = "\nPostgreSQL error: %s\n" % (sql)
response['results'] = results
return(response)
if __name__ == "__main__":
response = do_sql_query('waf_common', "insert into stuff(stuff_id, nonsense) values(nextval('stuff_seq'::regclass), 'Glug')", 0)
print (response['insert_id'])
response = do_sql_query('waf_common', "select * from staff limit 10", 0)
print (response['results'].first())
for record in response['results']:
for column in record.column_names:
print("%s : %s" % (column, record[column]))
print("\n ----------- \n")
Posted by pj at 05:00 PM | Comments (0)
March 29, 2009
Getting MySQL-python-1.2.2 to work with XAMPP
I'm trying to get the Python MySQLdb library to talk to my XAMMP MySQL. Here's how:
1. Before you build the db adaptor, change the site.cfg file to point to XAMMP's mysql_config
# The path to mysql_config. # Only use this if mysql_config is not on your PATH, or you have some weird # setup that requires it. mysql_config = /Applications/xampp/xamppfiles/bin/mysql_config
2. Link the dylib
cp /Applications/xampp/xamppfiles/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/mysql/libmysqlclient_r.15.dylib mkdir /Applications/xampp/xamppfiles/include ln -s /usr/local/mysql-5.1.32-osx10.5-powerpc/include /Applications/xampp/xamppfiles/include/mysql
3. You have to point your script to the localhost using the machines acutally IP address as if it was a remote server.
Posted by pj at 11:59 AM | Comments (0)
CherryPy HTMLTemplate
HTMLTemplate - CherryPy Tools - Trac
Posted by pj at 02:15 AM | Comments (0)
March 26, 2009
Python script for producing svn diffs
import os
my_file = open('changed_files.txt','r')
lines = my_file.readlines()
for line in lines:
plode = line.strip()
els = plode.split('/')
os.popen("svn diff -r 455 " + line.strip() + " > " + "_".join(els) + ".diff")
print "svn diff -r 455 " + line.strip() + " > " + els[-1] + ".diff"
Posted by pj at 09:31 PM | Comments (0)
March 24, 2009
CherryPy does ZPT
ChoosingATemplatingLanguage - CherryPy - Trac
Posted by pj at 11:14 AM | Comments (0)
April 15, 2008
Python script for getting changed files
The following Python script gets you a list of files changed between two subversion revisions:
import sys
import os
import re
lines = []
for counter in range(int(sys.argv[1]) - 1, int(sys.argv[2]) + 1):
lines.append(os.popen('/usr/local/bin/svn log -vv -r ' + str(counter)));
tally = {}
for results in lines:
for line in results:
reg = re.compile("svn-repository")
if reg.search(line):
tally[line] = 1
final = tally.keys()
final.sort()
print " " + " ".join(final)
Posted by pj at 05:06 PM | Comments (0)
December 14, 2007
pysvn Programmer's Guide
pysvn: pysvn Programmer's Guide
Posted by pj at 09:03 PM | Comments (0)
August 11, 2006
More about the Python in PHP project
Posted by pj at 10:06 AM | Comments (0)
Python interpreter embedded in PHP
Posted by pj at 10:03 AM | Comments (0)
August 03, 2006
Parsing mutliple date formats in Python
try:
date_time = datetime.datetime(*time.strptime(this_val, "%d/%m/%Y")[0:5])
except:
pass
try:
date_time = datetime.datetime(*time.strptime(this_val, "%B %Y")[0:5])
except:
pass
try:
date_time = datetime.datetime(*time.strptime(this_val, "%Y")[0:5])
except:
pass
print "<!--" + str(date_time) + "-->"
Posted by pj at 02:58 PM | Comments (0)
Very cunning Python based Universal Feed Parser
Posted by pj at 12:51 PM | Comments (0)
May 22, 2006
Notes for using Python urllib2
Posted by pj at 12:43 PM | Comments (0)
January 16, 2006
Python charting interface
PyGDChart2 - http://www.nullcube.com/software/pygdchart2/doc/index.html
Posted by pj at 08:32 PM
November 16, 2005
Parsing dates in Python
Posted by pj at 03:42 PM
September 14, 2005
Parsing binary data with Python
4.3 struct -- Interpret strings as packed binary data
Another alternative library is also available:
- http://www.nightmare.com/software.html
Posted by pj at 11:49 AM
August 09, 2005
mysql_robot2.py site indexing script
I've uploaded a copy of my mysql_robot2.py site indexing script for safe keeping.
And here's the DB structure:
Posted by pj at 10:40 AM
July 17, 2005
Yet another Python Web Framework
Django | The Web framework for perfectionists with deadlines
Posted by pj at 02:38 PM
May 27, 2005
A web crawler written in Python
http://www.newton.cx/~peter/software/crawler.py
Posted by pj at 01:20 PM
Example of how to go from Unicode to HTML
Unicode to HTML - tiddly-pom.com
Posted by pj at 12:54 PM
Information about the Python urlparse module
The urlparse module ::: The Standard Python Library (2005) ::: www.effbot.org
Posted by pj at 12:46 PM
A Python based HTML parser which handles tag soup and does tidying automaticamente
Beautiful Soup: We called him Tortoise because he taught us.
Posted by pj at 11:59 AM
May 26, 2005
Rare urllib2 example for handling response codes
ASPN : Python Cookbook : urllib2 for actions depending on http response codes
Posted by pj at 12:11 PM
May 23, 2005
Handling Unicode encoding in XML with Python
Posted by pj at 10:02 PM
April 20, 2005
Testing counters to see if they are odd or even
The following is a simple Python script to test whether a number is odd or even:
test_figure = float(test_figure) half_of_it = float(test_figure/2) #print str(half_of_it) if int(half_of_it) == float(half_of_it): return 'even' else: return 'odd'
It relies on the fact that casting a floating point number to an integer will round it up or down, and that dividing an odd number by two always gives a float, whereas dividing an even number gives an integer.
Posted by pj at 11:14 AM
February 15, 2005
Running JavaScript in Python
But why?
Posted by pj at 02:43 PM
October 30, 2004
Blogging iTunes playlists - RSS feeds
Found this posting by Kimbro Staken about a Python script he's written which posts his iTunes playlist to his blog using in-line AppleScript and the MT XML-RPC.
< http://www.xmldatabases.org/movabletype/archives/000159.html >
Should be straight forward to retask it to post 'now playing' titles too?
Posted by pj at 10:58 AM