Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

task_builder

""" functions for create single tasks from a distance matrix and a cast matrix

 (0 = not present; 1 = positive train; 2 = negative train; 3 = positive test; 4 = negative test).

        PTr NTr                 PTe NTe
       ---------               ---------
  PTr  | 1 | 2 | PTr       PTr | 1 | 2 | PTr
       ---------               ---------
  NTr  | 3 | 4 | NTr       NTr | 3 | 4 | NTr
       ---------               ---------
        PTr NTr                 PTe NTe        
"""

from numpy import *
from scipy import *

def gettaskID(cm, nfam = int(0)):
    '''seleziono le IDs corrispendonti alla colonna (task) nfam'''
    ids1=[]
    ids2=[]
    ids3=[]
    ids4=[]
    for j in range(0,131):
#        print cm[j][nfam]=="1"
#        print "cm[",j,"][0]=",cm[j][nfam]               
        if(cm[j][nfam]==1):
            ids1.extend([j])
        if(cm[j][nfam]==2):
            ids2.extend([j])
        if(cm[j][nfam]==3):
            ids3.extend([j])
        if(cm[j][nfam]==4):
            ids4.extend([j])
    return ids1,ids2,ids3,ids4

def get_labels_old(pos_threshold = 11, lnum = 74):
    labels = []
    for i in range(0,74):
        if(i<=pos_threshold):
            labels.append(1)
            i = i + 1
        else:
            labels.append(0)
    return labels

def get_labels_train(cm, nfam = int(0)):
    labels = []
    ptrain = 0
    ntrain = 0
    ptest = 0
    ntest = 0
    #for j in range(0,131):
    for j in range(0,len(cm)):
        if(cm[j][nfam]==1):
            ptrain = ptrain + 1
        if(cm[j][nfam]==2):
            ntrain = ntrain + 1
        if(cm[j][nfam]==3):
            ptest = ptest + 1
        if(cm[j][nfam]==4):
            ptest = ptest + 1
    for i in range(0,ptrain + ntrain):
        if(i<=ptrain):
            labels.append(1)
            i = i + 1
        else:
            labels.append(0)
    return labels

def get_labels_test(cm, nfam = int(0)):
    labels = []
    ptest = 0
    ntest = 0
    for j in range(0,len(cm)):
        if(cm[j][nfam]==3):
            ptest = ptest + 1
        if(cm[j][nfam]==4):
            ntest = ntest + 1
    for i in range(0,ptest + ntest):
        if(i<=ptest):
            labels.append(1)
            i = i + 1
        else:
            labels.append(0)
    return labels

def train_builder(dm, ids):
    ''' Creo il training set a partire dagli IDs nella cm '''
    train1 = []
    train2 = []
    train3 = []
    train4 = []
    # tr1 ids = (0,0)
    for i in ids[0]:
        for j in ids[0]:
            train1.extend([dm[i][j]])
            #print "dm[",i,"][",j,"]=",dm[i][j]
    atrain1 = array(train1)
    atrain1 = atrain1.reshape((len(ids[0]),len(ids[0])))
    #print atrain1.shape
    # tr2 ids  = (0,1)
    for i in ids[0]:
        for j in ids[1]:
            train2.extend([dm[i][j]])            
            #print "dm[",i,"][",j+1,"]=",dm[i][j]
    atrain2 = array(train2)
    atrain2 = atrain2.reshape((len(ids[0]),len(ids[1])))
    #print atrain2.shape
    # tr3 ids = (1,0)
    for i in ids[1]:
        for j in ids[0]:
            train3.extend([dm[i][j]])
            #print "dm[",i,"][",j+1,"]=",dm[i][j]
    atrain3 = array(train3)
    atrain3 = atrain3.reshape((len(ids[1]),len(ids[0])))
    #print atrain3.shape
    # tr4 ids = (1,1)
    for i in ids[1]:
        for j in ids[1]:
            train4.extend([dm[i][j]])
            #print "dm[",i,"][",j+1,"]=",dm[i][j]
    atrain4 = array(train4)
    atrain4 = atrain4.reshape((len(ids[1]),len(ids[1])))
    #print atrain4.shape  
    # Stacking together different arrays - Questo fa la magia
    atrain12 = hstack((atrain1,atrain2))
    atrain34 = hstack((atrain3,atrain4))
    atrain = vstack((atrain12,atrain34))                  
    return atrain

def test_builder(dm, ids):
    ''' Creo il test set a partire dagli IDs nella cm '''
    test1 = []
    test2 = []
    test3 = []
    test4 = []
    # test1 ids = (0,2)
    for i in ids[0]:
        for j in ids[2]:
            test1.extend([dm[i][j]])
    atest1 = array(test1)
    atest1 = atest1.reshape((len(ids[0]),len(ids[2])))
    #print atest1.shape
    # test2 ids  = (0,3)
    for i in ids[0]:
        for j in ids[3]:
            test2.extend([dm[i][j]])            
    atest2 = array(test2)
    atest2 = atest2.reshape((len(ids[0]),len(ids[3])))
    #print atest2.shape
    # test3 ids = (1,2)
    for i in ids[1]:
        for j in ids[2]:
            test3.extend([dm[i][j]])
    atest3 = array(test3)
    atest3 = atest3.reshape((len(ids[1]),len(ids[2])))
    #print atest3.shape
    # test4 ids = (1,3)
    for i in ids[1]:
        for j in ids[3]:
            test4.extend([dm[i][j]])
    atest4 = array(test4)
    atest4 = atest4.reshape((len(ids[1]),len(ids[3])))
    #print atest4.shape   
    # Stacking together different arrays
    atest12 = hstack((atest1,atest2))
    #print size(atest12)
    atest34 = hstack((atest3,atest4))
    #print size(atest34)
    atest = vstack((atest12,atest34))
    #print size(atest)
#    print atest12.shape
#    print atest34.shape
#    print atest.shape          
                       
    return atest

# ------------------------------------------------------------------------------
''' reading the files in as arrays - 2 way '''

dm_filename = ('../data/3PGK_DNA_BLAST_nolabels.txt')
cm_filename = ("../data/3PGK_30_nolabels.txt")
# 1. Using ScyPy
scipydati = io.array_import.read_array(dm_filename)
# 2. Using array
def read_array(filename):
    f = open(filename,'rb')
    dati = []
    for line in f.readlines():
        numbers = map(int, line.split())
        dati.append(numbers)
    f.close()
    return dati  
# --------------------------------------------------------------
''' Task Creation for task = NFAM'''

NFAM = 0
cm = read_array(cm_filename)
IDs = gettaskID(cm, nfam = NFAM)
dm = read_array(dm_filename)
trainset = train_builder(scipydati, ids=IDs)
testset = test_builder(scipydati, ids=IDs)
trainlabels = get_labels_train(cm, nfam = NFAM)
testlabels = get_labels_test(cm, nfam = NFAM)
#trainset = train_builder(dm, ids=IDs)
#testset = test_builder(dm, ids=IDs)

# --------------------------------------------------------------
''' write the trainset and the testset to a csv file'''
import csv

flag = 0
if (flag==1):    
    # last row contains class labels
    trainsetplus = vstack((trainset, trainlabels))
    testsetplus = vstack((testset, testlabels))
    trainsetplus_tr = transpose(trainsetplus)
    testsetplus_tr = transpose(testsetplus)
    writer = csv.writer(open("trainset_"+str(NFAM)+".csv", "wb"))
    writer.writerows(trainsetplus_tr)
    writer = csv.writer(open("testset_"+str(NFAM)+".csv", "wb"))
    writer.writerows(testsetplus_tr)
else: print"non salvo nulla"

# --------------------------------------------------------------

from svm import *

problem = svm_problem(trainlabels,trainset)
#param_linear_10 = svm_parameter(kernel_type = LINEAR, C = 10, svm_type = C_SVC)
param_rbf_10 = svm_parameter(kernel_type = RBF, C = 10, svm_type = C_SVC)
#m_lin = svm_model(prob, param_linear_10)
m_rbf = svm_model(problem, param_rbf_10)

#from cross_validation import *
#do_cross_validation(trainset, trainlabels, param_rbf_10, 10)
#do_cross_validation(trainset, trainlabels, param_linear_10, 10)

size = len(trainset)
kernels = [LINEAR, POLY, RBF]
kname = ['linear','polynomial','rbf']

param = svm_parameter(C = 10,svm_type = C_SVC)
for k in kernels:
    param.kernel_type = k;
    model = svm_model(problem,param)
    errors = 0
    for i in range(size):
        prediction = model.predict(trainset[i])
        probability = model.predict_probability
        if (trainlabels[i] != prediction):
            errors = errors + 1
    print "##########################################"
    print " kernel %s: error rate = %d / %d" % (kname[param.kernel_type], errors, size)
    print "##########################################"

Python script to stitch together image tiles

// python script for image tile stiching

#! /usr/bin/env python

# A tile stitching program.
from glob import glob
from cgi import parse_qs
import os.path
from pprint import pprint

# load all the files in this directory to be processed
filenames = glob('*.png')
try:
    filenames.remove('output.png')
except ValueError:
    pass

canvas = {}

for name in filenames:
    qs = os.path.splitext(name)[0]
    params = parse_qs(name)
    x = params['x'][0]
    y = params['y'][0]
    if canvas.has_key(x):
        canvas[x][y] = name
    else:
        canvas[x] = { y : name }

#pprint(canvas)

# calculate the dimensions of the output
tile_size = 256
temp = canvas.popitem()
canvas[temp[0]] = temp[1]
x1 = len(canvas)
y1 = len(temp[1])

xdim = x1 * tile_size
ydim = y1 * tile_size

# create the full size blank image
from PIL import Image
mode = 'RGB'
im = Image.new(mode, (xdim,ydim))

# walk the array
position = (0,0)
for row in sorted(canvas):
    for column in sorted(canvas[row]):
        current_tile = canvas[row][column]
        temp = Image.open(current_tile)
        im.paste(temp, position)
        position = (position[0], position[1] + 256)
    position = (position[0] + 256, 0)

im.save('output.png')

Python - Processing Large Text Files One Line At A Time

// process some very large text files one line at a time

fh = open('foo.txt', 'r')
line = fh.readline()
while line:
    # do something here
    line = fh.readline()


// Update

for line in open('foo.txt', 'r'):
    # do something here

PHAWN Interpreter

// description of your code here

import sys
import time

debug = 0
source = []
stack = []
px = 0
py = 0
pd = 1
mode = 0
register = 0

def Initialize():
    
    global source
    global px
    global py
    
    path = raw_input("PHAWN source file: ")
    
    try: file = open(path, "r")
    except IOError:
        print "Failed to open file!"
        return 0
    else: print "File opened successfully!"
    
    for line in file:
        source.append(line.strip("\n"))
        try: px = line.index("$")
        except: continue
        else: py = source.index(line.strip("\n"))
        
    return 1

def Run():
    
    global debug
    global source
    global stack
    global px
    global py
    global pd
    global mode
    global register
    
    try: instruction = source[py][px]
    except:
        print "--- Pointer moved into an unknowned region."
        return 0
    
    if debug == 1: print "Instr:", instruction, " Pos:(", px, ",", py, ")", " Dir:", pd
    
    oy = py
    
    if instruction == "#":
        return 0
    elif instruction == ">":
        if pd > 0:
            stack.append(register)
        elif len(stack) > 0:
            register = stack.pop()
    elif instruction == "<":
        if pd < 0:
            stack.append(register)
        elif len(stack) > 0:
                register = stack.pop()
    elif instruction == "}":
        if pd > 0:
            Input()
        else:
            Output()
    elif instruction == "{":
        if pd < 0:
            Input()
        else:
            Output()
    elif instruction == "/":
        if pd > 0:
            px += 1
            while (source[py][px].isdigit() and source[py][px+1].isdigit()):
                px += 1
    elif instruction == "\\":
        if pd < 0:
            px -= 1
            while (source[py][px].isdigit() and source[py][px-1].isdigit()):
                px -= 1
    elif instruction == "^":
        py -= 1
    elif instruction == "_":
        py += 1
    elif instruction == "~":
        if len(stack) > 1:
            if stack[len(stack) - 1] > stack[len(stack) - 2]:
                py -= 1
            else:
                py += 1
    elif instruction == "+":
        if len(stack) > 1:
            val1 = stack.pop()
            val2 = stack.pop()
            stack.append(val1 + val2)
    elif instruction == "-":
        if len(stack) > 1:
            val1 = stack.pop()
            val2 = stack.pop()
            stack.append(val2 - val1)
    elif instruction == "&":
        mode = 1 - mode
    elif instruction == "%":
        if len(stack) > 1:
            val1 = stack.pop()
            val2 = stack.pop()
            stack.append(val1)
            stack.append(val2)
    elif instruction.isdigit() == True:
        ReadInteger()      
        
    if pd > 0 and px + pd > len(source[py]) - 1:
        pd = -1
    if pd < 0 and px + pd < 0:
        pd = 1
    
    if oy == py:
        px += pd
    
    return 1
    
def Input():
    
    global stack
    
    data = raw_input("Input: ")
    if data.isdigit() == False:
        stack.append(ord(data[0]))
    else:
        stack.append((int(data)))
        
def Output():
    
    global mode
    
    if len(stack) > 0:
        data = stack.pop()
        if mode == 0:
            print data
        else:
            print chr(data)
            
def ReadInteger():
    
    global source
    global stack
    global px
    global py
    global pd
    global register
    
    data = source[py][px]
    
    if pd > 0:
        while (source[py][px + 1].isdigit() == True):
            try:
                px += 1
                data += source[py][px]
            except: break
    else:
        while (source[py][px - 1].isdigit() == True):
            try:
                px -= 1
                data = source[py][px] + data
            except: break
            
    register = int(data)
            
    if px > len(source[py]) - 1:
        px = len(source[py]) - 1
    if px < 0:
        px = 0
        
debug = raw_input("Debug Mode? (1 = Yes / 0 = No) ")
if debug.isdigit() == True: debug = int(debug)

if Initialize() == 1:
    start = time.clock()
    while(Run() == 1):
        if debug == 1:
            print "Stack: ", stack
            raw_input("Pressed <Enter> To Continue.")
        else: continue
    print "Executed in ", time.clock() - start, " seconds."
else: print "--- Exited without execution."    
raw_input("Press <Enter> to exit.") 

Simple asx decoder

asx decoder: associate it to MIME: video/x-ms-asf in your browser.

#! /usr/bin/env python

from xml.dom.minidom import parse;
from sys import argv
from os import system

if __name__ == '__main__':
    if len(argv)>1:
#	print >> file('/tmp/asp.log', 'w'), argv 
        d = parse(file(argv[1]));
        links = [ ref.getAttribute('HREF')
                  for ref in d.getElementsByTagName('REF')
                  if ref.hasAttribute('HREF') ]
        for link in links:
            system('mplayer ' + link)


Vigenere Subsitution


Just for fun, the Vigenere Cipher, in just a few lines of Python. For more info, see:
http://en.wikipedia.org/wiki/Vigenère_cipher
import string

def vigenere(c,k,e=1):
# e=1 to encrypt, e=-1 to decrypt
    wk=[string.ascii_uppercase.find(ch) for ch in k.upper()]
    wc=[string.ascii_uppercase.find(ch) for ch in c.upper()]


    wc = [ (x[0]+(e*x[1]))%26 for x in zip(wc,wk*(len(wc)/len(wk)+1))]

    print string.join([string.ascii_uppercase[x] for x in wc],"")

>>> vigenere('thiscryptosystemisnotsecure', 'cipher')
VPXZGIAXIVWPUBTTMJPWIZITWZT
>>> vigenere('VPXZGIAXIVWPUBTTMJPWIZITWZT', 'cipher', -1)
THISCRYPTOSYSTEMISNOTSECURE

CircularStack

This class implements a sorta circular stack. If you get the top item, it will pop off but be added to the bottom.

class CircularStack(list):

    def gettop(self):
        item = self.pop()
        self.append(item)
        return item

LightTPD subdomain rewrite, Backpack/Basecamp style.

Rewrites test.example.com/example/path/ to example.com/test/example/path/

This example show the values being passed to FCGI for use by Django.


$HTTP["host"] =~ "([^.]+)\.example\.com" {
  server.document-root = "/users/home/username/domains/example.com/web/public"
  server.errorlog = "/users/home/username/var/log/lighttpd.example_com.error.log" 
  accesslog.filename = "/users/home/username/var/log/lighttpd.example_com.access.log" 

  fastcgi.server = (
    "/main.fcgi" => (
      "main" => (
        "socket" => "/users/home/username/tmp/django/example_com.socket"
      )
    )
  )

  url.rewrite-once = ( "^(/.*)$" => "/main.fcgi/%1/$1" )
  server.error-handler-404 = "/main.fcgi" 
}

Python Towers of Hanoi

Use the following as a template, replace the print line with any work for moving the discs using your data structure. The code below just prints the necessary moves.

def hanoi(n, a='A', b='B', c='C'):
    """
    move n discs from a to c using b as middle
    """
    if n == 0:
        return
    hanoi(n-1, a, c, b)
    print a, '->', c
    hanoi(n-1, b, a, c)

hanoi(3)

Win32 Shortcuts (.lnk files) with com in python

Class and general usage for accessing windows shortcuts in Python.

import pythoncom

class Win32Shortcut:
    def __init__(self, lnkname):
        self.shortcut = pythoncom.CoCreateInstance(
            shell.CLSID_ShellLink, None,
            pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
        self.shortcut.QueryInterface(pythoncom.IID_IPersistFile).Load(lnkname)

    def __getattr__(self, name):
        return getattr(self.shortcut, name)

s = Win32Shortcut(path)
iconPath = s.GetIconLocation()[0]
itemPath = s.GetPath(0)[0]