Well, I’ve finished the first half of O’Reilley’s Learning Python book and I’m impressed. The language is clean and not too hard to learn, especially given my experience with similar languages like Tcl/Tk. There are however a few things that intrigue and concern me:

  1. No Magic.Python seems really big on the “No Magic” idea, meaning that if the result of an operation is ambiguous then it simply doesn’t do it. This was particularly frustrating for me with TclTk as it was frequently difficult to determine if a string would be considered a Number (for addition) or a string (for Concatenation). This most frequently was a problem when you were trying to do string processing operations on numbers, you wound up with a string like “042″ that it started interpreting as Octal 42. That Python warns you & forces you to typecast is somewhat nice.
  2. Immutable vs Mutable: In C you can spend alot of time getting the hang of “Pass by Reference” vs “Pass by Value”. In Python it seems you spend alot of time trying to get the hang of Mutable vs Immutable. The reference is similar when it comes to function arguments, but this distinction effects the code at all levels. From what I see, it’s mostly a theoretical problem that in practice doesn’t really happen much. I mean, honestly, how often have you ever had to modify a number “in place”. I’m betting never. “2″ is “2″, and you probably want it to stay that way. Where I figure I’ll get burned is with “tuples”, the immutable form of a list.
  3. Generators/Iterators: This is just a neat gizmo here. Having functions that “yield” a value instead of “return” seems a bit awkward, but I can already see the huge benefits you can reap from this.

And, of course, there’s plenty more. I’m just getting started. VTK has Python Bindings (I believe the new Paraview 3.0 is Python-based even) so I’ve spent the last few days trying to get VTK+Python setup on my Windows Workstation at the office and on our SGI Irix system. It’s proven to be difficult, partly because of the wierd architectures and partly because of VTK, but it’s done. So I present to you, my first functional VTK+Python script.
 [tag:python][tag:source][tag:vtk][tag:script][tag:software][tag:programming]

# import VTK
from vtk import *

# Open the cquads file
file = open(r’data/cquads1′)

# Skip the first line
file.next()

dataPoints = vtkPoints()
dataPoints.Allocate(2000000,1024)
quad = vtkQuad()
grid = vtkUnstructuredGrid()
grid.Allocate(2000000,1024)
pointList = vtkIdList()
# Foreach remainignl ine
print “Reading quads”
count = 0
for line in file.xreadlines():
    count += 1
    if count % (2 ** 13) == 0:
        print “t%s …” % count,

    # Split the line into a list, by spaces
    data = line.split()
    # Extract the 4 points
    pointList.Reset()
    for start in [0, 7, 14, 21]:
        point = map(float, data[start:start+3])
        pointList.InsertNextId(apply(dataPoints.InsertNextPoint, point))

    grid.InsertNextCell(quad.GetCellType(), pointList)

grid.SetPoints(dataPoints)

print “Writing file…”
writer = vtkDataSetWriter()
writer.SetFileName(‘output.vtk’)
writer.SetFileTypeToBinary()
writer.SetInput(grid)
writer.Write()



It’s not much, I know, but I’m happy with it. It simply reads a file that contains, on each line, 4 three-dimensional points that define a 4-sided polygon (Quad). There’s some extra numbers between the points that I ignore, but I take all of it and load it into a vtkUnstructuredGrid and then write it to disk. I’m impressed at how easy it is, as I was dreading trying to get the sscanf commands to correctly parse all of this. A simple split & map(Float) and I was well on my way. This script, on our old 600MHZ Mips Processor SGI system converts the full 2.4Million quads (lines of text) in 15 minutes.