Python Thumbnail

Python: Associative Results with the MySQLdb library

July 8th, 2011 at 08:48 am (CST) by Jeff

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.

classic_battletech_logo

I’ve created an online BattleTech lance builder

May 20th, 2011 at 09:37 pm (CST) by Jeff

Check it out http://gauthic.com/utils/lancemaker/

A big thank you from the Solaris Skunk Werks project for their index.ssi file where I got the data for this quickie project.

No saving options, but it’s made for just quick lance (or star) creation :)

Python Thumbnail

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

September 2nd, 2010 at 04:10 pm (CST) by Jeff

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.

Thumbnail - Gauthic (New)

Tournament-Tracker.NET is in Release Candidate 1

July 30th, 2010 at 04:23 pm (CST) by Jeff

Yesterday I went ahead and versioned up Tournament-Tracker to RC1. This was after I cleaned up the Stats pages, added the ability to add a “Free Game” with your Tournament-Tracker.NET Friends, and rudimentary support for campaigning (which I am running for our Warhammer Fantasy Build up here at Galaxy Comics).

Remember, anyone who joins now will have Comp Credit Accounts for creating Tournaments & Campaigns (and whatever is added in the future!)

Cheers!

Jeff

Python Thumbnail

Games Workshop FAQ Grabber (Python Script)

July 22nd, 2010 at 02:10 pm (CST) by Jeff

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.

Thumbnail - Gauthic (New)

OpenWargaming: BBCode to TEX Converter

July 1st, 2010 at 09:43 am (CST) by Jeff


Click here to read this article


Thumbnail - Open Wargaming Society

OpenWargaming: LaTeX PDF Test

June 30th, 2010 at 09:48 am (CST) by Jeff


Click here to read this article


Thumbnail - Gauthic (New)

LaTeX for document creation

June 28th, 2010 at 10:58 am (CST) by Jeff


Click here to read this article


tn_ruby

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

June 23rd, 2010 at 10:10 am (CST) by Jeff


Click here to read this article


Thumbnail - Open Wargaming Society

OpenWargaming: Quiet on the Frontend

June 15th, 2010 at 07:57 pm (CST) by Jeff


Click here to read this article