Python samples

From OriWiki

Jump to: navigation, search

Samples

tet.py

Drag the mouse and use the wheel to rotate a tetrahedron

from Tkinter import *
from math import *

# note: matrix is represented by a list of lines:
# [ [l1] [l2] .. [l_m]].
# m = len(mat), n = len(mat[0]), mat(i,j) = mat[i][j]

# The Tetrahedron is represented by a 3X4 matrix. Each column represents a 3d vertex

# todo: deal with the global variables .. maybe convert wrap in a class


def createEmptyMat(m,n):
    return [[0 for j in range(n)] for i in range(m)]

def matMul(mat1, mat2):
    m = len(mat1)
    n = len(mat2[0])
    common = len(mat2)
   
    ret = createEmptyMat(m,n)
    for i in range(m):
        for j in range(n):
            for k in range(common):
                ret[i][j] += mat1[i][k] * mat2[k][j]
    return ret

def matTrans(mat):
    m = len(mat[0])
    n = len(mat)

    ret = createEmptyMat(m,n)
    for i in range(m):
        for j in range(n):
            ret[i][j] = mat[j][i]
    return ret
   
def drawTet(tet,col):
    for p in range(4):
        for p1 in range(p+1,4):
            canvas.create_line(tet[0][p]+200,tet[1][p]+200,tet[0][p1]+200,tet[1][p1]+200, fill = col)
           

EPS = pi/300
ID = [[1,0,0],[0,1,0],[0,0,1]]

ROT_X = matTrans([ [1,0,0], [0,cos(EPS),sin(EPS)], [0,-sin(EPS),cos(EPS)] ])
IROT_X = matTrans([ [1,0,0], [0,cos(EPS),-sin(EPS)], [0,sin(EPS),cos(EPS)] ])

ROT_Y = matTrans([[cos(EPS),0,sin(EPS)], [0,1,0], [-sin(EPS),0,cos(EPS)]])
IROT_Y = matTrans([[cos(EPS),0,-sin(EPS)], [0,1,0], [sin(EPS),0,cos(EPS)]])

ROT_Z = matTrans([[cos(EPS),sin(EPS),0], [-sin(EPS),cos(EPS),0], [0,0,1]])
IROT_Z = matTrans([[cos(EPS),-sin(EPS),0], [sin(EPS),cos(EPS),0], [0,0,1]])

lastX = 0 
lastY = 0

def cbClicked(event):
    global lastX
    global lastY
   
    lastX = event.x
    lastY = event.y

def cbMottion(event):
    global lastX
    global lastY
    global tet

    dx = event.y - lastY

    if dx > 0:
        rotx = IROT_X
    else:
        rotx = ROT_X
       
    RX = ID
    for i in range(abs(dx)):
        RX = matMul(rotx,RX);


    dy = event.x - lastX
    if dy > 0:
        roty = IROT_Y
    else:
        roty = ROT_Y

    RY = ID
    for i in range(abs(dy)):
        RY = matMul(roty,RY);
   
    drawTet(tet,'white')
    tet = matMul(RX,tet)
    tet = matMul(RY,tet)
    drawTet(tet,'black')
   
    lastX = event.x
    lastY = event.y

def wheelUp(event):
    global tet
    drawTet(tet,'white')
    tet = matMul(ROT_Z,tet)
    drawTet(tet,'black')

def wheelDown(event):
    global tet
    drawTet(tet,'white')
    tet = matMul(IROT_Z,tet)
    drawTet(tet,'black')

                
# init
canvas = Canvas(width=400, height=400, bg='white') # try to delete the bg specification 
canvas.pack(expand=YES, fill=BOTH)               
canvas.bind("<Button-1>", cbClicked)
canvas.bind("<B1-Motion>", cbMottion)
canvas.bind('<Button-4>', wheelUp)
canvas.bind('<Button-5>', wheelDown)


tet = matTrans([[0,-100,0],[-100,100,0],[100,100,0],[0,0,200]])

drawTet(tet,'black')

mainloop()

blame

Colorful blame that uses svn (like the tkcvs one)

#!/usr/bin/python
import Tkinter
import sys
import subprocess


if len(sys.argv) != 2:
    print "usage:\n %s <filename>" % sys.argv[0]
    exit(1)
    
# get the output of an svn blame command 
command = ["svn", "blame", sys.argv[1]]
lines = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
lines = lines.split('\n')

# tk text widget and scroll bar creation and attachment 
scrollbar = Tkinter.Scrollbar()
scrollbar.pack(side=Tkinter.RIGHT, fill=Tkinter.Y)

text = Tkinter.Text(width=100, height=25 , bg='white')
text.pack()

text.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=text.yview)

# how many edit each user did: user-> number of lines added by her 
occurs = {}

annotated_lines = [] 
for line in lines:
    if not line: continue
    words = line.split()
    user = words[1]
    annotated_lines += [(user,line)] 
    occurs[user] = occurs.get(user,0)+1

users = occurs.keys()

# sort user by occurences 
# users.sort(key=lambda x:occurs[x])

# attach color tag for each user  
for user in users:
    # todo: improve this: 
    color = "#%02x%02x%02x" % ((occurs[user]*40)%255,(-occurs[user]*70)%255,0)
    text.tag_config(user, background=color)

# insert the colored lines 
for pair in annotated_lines:
    user,lineStr = pair[0],pair[1]
    text.insert(Tkinter.END, lineStr+'\n',user)

Tkinter.mainloop()

## todo:
#   * improve the coloring function
#   * fix the windows+scollbar expamsion
#   * fix tab and spaces display 



## lefovers ---

#canvas = Tkinter.Canvas(width=400, height=400, bg='white')
#canvas.pack(expand = Tkinter.YES, fill = Tkinter.BOTH)               

#canvas.create_text(xpos, ypos, text = lineStr, fill = color)