Category Archives: Python

Python: Associative Results with the MySQLdb library

This is more of a personal reminder as it’s not the default behavior as it is in most modern database libraries…

I’ve highlighted the important bits for associative results in white below

#!/usr/bin/python
# Note the MySQLdb.cursors import statement
#    and the cursorclass declaration in the connect string.
import MySQLdb
import MySQLdb.cursors

oDBConn=MySQLdb.connect(user="user",
			passwd="pass",
			db="db",
			host="localhost",
			compress=1,
			cursorclass=MySQLdb.cursors.DictCursor)
oCursor=oDBConn.cursor()
oCursor.execute("select `field1`,`field2`,`field3` from `mytable`")

while(1):
	LogString = ""
	oRow = oCursor.fetchone ()
	if oRow == None:
		break
	sField1 = oRow["field1"]
	sField2 = oRow["field2"]
	sField3 = oRow["field3"]

The <pre> tag formatting above may be a bit difficult depending on your browser… but you should be able to copy and paste the entire codeblock with no difficulty.

Python Programming: Converting my old avi files to h.264

Here’s a little script that calls HandBrake‘s CLI interface to convert a whole directory full of media in a recursive manner.

No Instructions. No warranty. No guarantees. Your mileage may vary. Yadda Yadda Yadda.
Tested on Ubuntu and MacOS 10.6

Download it here or just read it (better to download it, since my blog removes the whitespace and hence the flow of the script):

#!/usr/bin/python
# This script requires the HandBrake CLI program
# Download it at http://handbrake.fr/downloads2.php

# You’ll need to change this to fit where everything is on your system
sFromDirectory = “/home/jeff/Videos/”
sToDirectory = “/home/jeff/Desktop/converted_video_output/”
sFullPathToHandbrakeCLI = “/usr/bin/HandBrakeCLI”

# Don’t mess with anything below this…..
import os.path
import shutil

def ConvertFile(sPath, sFile):
global sToDirectory
global sFromDirectory
global sFullPathToHandbrakeCLI
sExtension = os.path.splitext( sFile )
sNewFileName = sFile.replace(sExtension[len(sExtension) – 1], “.m4v”)
sNewDirectory = sPath.replace(sFromDirectory, sToDirectory)
if sPath != “”:
sOldFullPath =  sPath + “/” + sFile
else:
sOldFullPath =  sPath + sFile

if sPath != “”:
sNewFullPath =  sNewDirectory + “/” + sNewFileName
else:
sNewFullPath =  sNewDirectory + sNewFileName

# Don’t re-encode if it’s already a m4v
if not os.path.exists(sNewFullPath):
if sExtension == “.m4v”:
# Copy it instead of re-encoding it…
shutil.copyfile(sOldFullPath, sNewFullPath)
else:
sHandbrakeCommand = sFullPathToHandbrakeCLI + ‘ -i “‘ + sOldFullPath + ‘” -o “‘ + sNewFullPath + ‘” -v -m -E aac,ac3 -e x264 -q 0.65 -x ref=3:mixed-refs:bframes=6:b-pyramid=1:weightb=1:analyse=all:8x8dct=1:subme=7:me=umh:merange=24:filter=-2,-2:trellis=1:no-fast-pskip=1:no-dct-decimate=1:direct=auto’
os.system(sHandbrakeCommand)

def callback( arg, sDirectory, aFilenames ):
global sToDirectory
global sFromDirectory
print “Checking files in ” + sDirectory
sSubPath = sDirectory.replace(sFromDirectory, “”)
for sFile in aFilenames:
if sSubPath == “”:
sOldFullPath =  sDirectory + sFile
else:
sOldFullPath =  sDirectory + “/” + sFile

if os.path.isdir(sOldFullPath):
sNewDirectory = sOldFullPath.replace(sFromDirectory, sToDirectory)
print “”
if not os.path.exists(sNewDirectory):
print “Making new directory: ” + sNewDirectory
print “”
os.mkdir(sNewDirectory)
else:
ConvertFile(sDirectory,sFile)

if not os.path.exists(sToDirectory):
os.mkdir(sToDirectory)
arglist = []
os.path.walk(sFromDirectory,callback,arglist)

Updated (September 3rd): Fixed a directory recursion issue.

Games Workshop FAQ Grabber (Python Script)

I’m nerding out on ya, sorry. Programmers and Script Monkeys only, as I’m not going to explain how to use this.

I’ve created a Python script that grabs all the latest 40K and Fantasy FAQ’s/PDF’s

Here’s the script:

#!/usr/bin/python

import urllib
import sys,os

sGWURL = "http://www.games-workshop.com"
sWarhammerFAQURL = "http://www.games-workshop.com/gws/content/article.jsp?categoryId=1000018&pIndex=1&aId=3000006&start=2"
s40KFAQURL = "http://www.games-workshop.com/gws/content/article.jsp?catId=cat440134a&categoryId=1000018&section=&pIndex=1&aId=3400019&start=2"
sLocalFantasyFAQPath = "./" # I Normally just use the full filepath to my Dropbox Folder on my Ubuntu box
sLocal40KFAQPath = "./" # I Normally just use the full filepath to my Dropbox Folder on my Ubuntu box

def GetData(sURL):
        oPage = urllib.urlopen(sURL)
        sPageData = oPage.read()
        return sPageData

def FilterOutFAQLines(sData):
        FAQs = list()
        aLines = sData.splitlines()
        for sLine in aLines:
                if sLine.lower().startswith("<a href"):
                        if sLine.lower().find(".pdf") > 0:
                                FAQs.append(sLine)
        return FAQs

def downloadFile(url,localfilename):
        webFile = urllib.urlopen(url)
        #olocalFile = open(url.split('/')[-1], 'w')
        localFile = open(localfilename, 'w')
        localFile.write(webFile.read())
        webFile.close()
        localFile.close()

def GetFile(sHREFLine, sDir):
        global sGWURL
        sURL = sGWURL + sHREFLine[sHREFLine.find('"') + 1:sHREFLine.find('"', 10)]
        sFileName = sHREFLine[sHREFLine.find('>') + 1:sHREFLine.find('<', 10)]
        downloadFile(sURL, sDir + sFileName)
        return sFileName

def DeleteFolderContents(folder):
        for the_file in os.listdir(folder):
            file_path = os.path.join(folder, the_file)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_path)
            except Exception, e:
                print e

aWarhammerFAQs = FilterOutFAQLines(GetData(sWarhammerFAQURL))
a40KFAQs =  FilterOutFAQLines(GetData(s40KFAQURL))

DeleteFolderContents(sLocalFantasyFAQPath)
for sLine in aWarhammerFAQs:
        sFile = GetFile(sLine, sLocalFantasyFAQPath)
DeleteFolderContents( sLocal40KFAQPath )
for sLine in a40KFAQs:
        sFile = GetFile(sLine, sLocal40KFAQPath )

Feel free to download it here in case your copy/paste doesn’t work.

This won’t be very useful to non-programmer types, but it might save someone an hour or so if they wanted to do the same thing.

Ok I’m being drawn to Ruby (over Python)…

Lately I’ve been dabbling (and enjoying!) Python, but as of this weekend I’m being drawn to Ruby mainly because of several reasons (over Python):

  • The ability to easily set up and run a full blown MVC web application on Apache
  • I like the MySQL database interface better (I just finished a test and it was much more elegant than having to define a cursor, etc).
  • The ability to command line script is about the same, but I’d rather marry myself to as little programming languages as I can this late in life.
  • When I do return to the Mac platform Cocoa for Ruby has been praised
  • I’ll be eventually migrating my current Python projects to Ruby to see how much different or harder or easier it is to read and maintain the code.
  • My biggest beef with Python is having to define the functions before the code is run (I like to have my user functions at the bottom of the script and the logic at the top). Update: It seems as if Ruby has this limitation too….. 🙁 So it appears that I’ll just have to work on my coping skills.
  • If Python had a module similar to Passenger (for Apache module for Ruby) I wouldn’t be so quick to look at Ruby as I really like the language.

BUT WAIT! There’s MORE!

It seems that Passenger supports Python web development as well! *rubs chin* verrry interesting…