Tuesday, June 30, 2009

Project: python graphics render package

Once upon a time, back in my grad school days, I was really into computer graphics. I haven't really done much CGI since then. So I've decided to get back into computer graphics as my hobby.

Why Python? Well because Python is da shiznit. I plan to build a renderer. It will be a learning experience so I will recreate the path I took in grad school. I will be making design decisions along the way. At this point, I don't know what to reuse and what to invent from scratch. But I do know that I want to implement most of the algorithms in Python. Maybe if this project is successful, I will rewrite the performance critical parts in C. But that is a loooooooong way off. One step at a time. :)

Monday, June 29, 2009

python graphics link-o-rama


  1. Tkinter is the standard default GUI for Python. The documentation is here.

  2. gnuplot.py as a package that interfaces to gnuplot.

  3. PyQwt is a set of bindings for the Qwt class library which is written in C++.

  4. PLplot is snother scientific plotting package. It has bindings for Python and other languages.

little link-o-rama 1

I like making lists. So here you have a list of links of useful stuff!

1. A list of 23 starter kits for web designers.

2. 53 digital photography tuotorials.

3. Who isn't on facebook these days? 20 facebook tips.

4. 10 ways to get cables under control

5. The couch potato to 5k running plan. The best little tech tip I can give you is, keep your body well maintained. :)

Thursday, June 18, 2009

vim: case insensitive search


vim/gvim has a default case sensitive search which is what I want most of the time.

There are times however when I need case insensitive searches. There are 2 ways to do this:


  1. First you can do

    :set ignorecase

    This sets the searches to ignore the case. Use :set noignorecase to revert to case sensitive search.


  2. The problem with that is if (like me) you only want to ignore case just for one search. It's a lot of keystrokes just to do this for one search. This takes care of that case. Use \c anywhere in the search pattern to ignore case. This does not touch the global setting of case.


Easy as pie. :)



Ad: Vim books

Ad: Regex books

Thursday, June 11, 2009

python sqlite trick / tidbit

I found myself doing this a lot in Python when using sqlite3:



conn = sqlite3.connect( 'somedb' )
cursor = conn.cursor()
cursor.execute( 'SELECT * from sometable' )
for row in cursor:
do something with row


I found it easier and much more fun to use list comprehensions:



conn = sqlite3.connect( 'somedb' )
for row in [x for x in \
conn..execute( 'SELECT * from sometable' )]
do something with row


We even dispense with the cursor altogether. After discovering this, I found SQL to be a lot less painful.








python: how to flatten a tuple

Basically the problem was that I wanted to create a flat tuple from a tuple and a single value like such:

val = 3
tup = ( 'a', 3.14, "zzz" )


I wanted this:

( 3, 'a', 3.14, "zzz" )

not this:

( 3, ( 'a', 3.14, "zzz" ) )


It turns out that this is exactly what the '+' operator does for tuples.

In general, adding two tuples creates a bigger flat tuple from the elements of each.
For this problem the answer then was:


(val,) + tup

Easy. :)


Note:

(val,) results in a tuple with a single element.



Ad: Python books on Amazon

opening a unicode file in vim/gvim

This was the easiest way I found:


:e ++enc=utf-16 filename

python: finding the maximum element in a list

Actually it is very easy, as I discovered. Python has a built-in function, max. So it is simply:

maximum = max( mylist )

That easy. The problem that I had to solve involved a list of tuples. And I had to find the maximum of the first element. There's a very cool way of using either generator expressions like so:

maximum = max( (x[0] for x in list_of_tuples ) )

You can also do it with list comprehensions as such:

maximum = max( [x[0] for x in list_of_tuples] )

Now the big question is, which is better? I have no clue. Stay tuned for the answer. I will try to find out.

Wednesday, June 10, 2009

python tidbits

1. How to change the working directory in python:

import os
os.chdir( )


2. How to add a directory to the module search path from within the interpreter. (Note: I like running stuff and testing new things in the interpreter. This tidbit enables me to test out modules I'm writing without having to modify the PYTHONPATH environment variable).

import sys
sys.path.append( )