Thursday, August 19, 2010

Scala wrappers for java collection


import scala.collection.JavaConversions._

System.getProperties.foreach(println)


Please note that the java method System.getProperties() returns java.util.Properties. There is no foreach defined in that class. Behind the scene, scala.collection.JavaConversions defines implicit conversions that converts java collections to scala collections.

Friday, August 6, 2010

Python titbits

I was generating sql data load scripts from excel documents using python. Here’s the code snippet
import csv

with open('cases.csv', 'rU') as f:
# r = csv.DictReader(f, dialect=csv.excel)
r = csv.reader(f, dialect=csv.excel)
cases = [row[:-3] for row in r]

Few interesting aspects in the code above

  1. I am using python’s with statement to automatically close the csv file after use.

  2. I am opening the file in ‘rU’ mode - i.e. readonly and Universal mode. ‘U’ is critical for csv module to parse excel generated csv’s. Here’s the gory details

  3. Use csv.DictReader if you want data in a dictionary keyed by the column names. DictReader exposes a
    convenient property ‘fieldnames’ that has the column names if your csv has a header row.


  4. Notice the use of python’s list comprehension to populate the cases array. Also, I am chopping
    off the last 3 columns (which was empty in the data file) using python’s slice operator.



For better clarity, I changed the code to
import csv
import collections

Case = collections.namedtuple('Case',"program, title, meeting_time, meeting_date")
with open('foo.csv', 'rU') as f:
# r = csv.DictReader(f, dialect=csv.excel)
r = csv.reader(f, dialect=csv.excel)
cases = [Case(*row[:-3]) for row in r]

namedtuple is similar to ruby’s Struct class.
This is nicely encapsulated now. The rest of the code need not worry about which column
the data was in the original data file. Also note the use * in Case(*row[:-3]). I am using * to pass the parsed array as arguments to Case. In clojure, you would use apply

Now consumers of cases can use it like so
qry = '''INSERT INTO cases (program, meeting_time, meeting_date) VALUES ('%s','%s','%s');'''
for c in cases:
print qry%(c.program, c.meeting_time, c.meeting_date)

Before I wrap up, one last thing - if you want to output this to a file instead of stdout
with open('cases.sql', 'w') as f:
qry = '''INSERT INTO cases (program, meeting_time, meeting_date) VALUES ('%s','%s','%s');'''
for c in cases:
print >>f, qry%(c.program, c.meeting_time, c.meeting_date)

Notice how I am redirecting print output to the file handle ‘f’. Here’s a blog article about it.

Sunday, July 11, 2010

Build mono-2.4.6 in Snow Leopard

Mono 2.4.6 configure scripts generates 32 bit build and requires 32 bit dependencies.

This is a problem in Snow Leopard which is all 64 bit. I had to use the following settings.


export CFLAGS="-I/Users/amitava/local/include -arch i386 -m32"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="-L/Users/amitava/local/lib"

Please note that we have to override the system defaults by the generated packages. So I append $prefix (/Users/amitava/local in my case) to the PATH environment variable

export PATH=$HOME/local/bin:$PATH

The following packages were needed
gettext-0.18.1.1
libiconv-1.13.1
glib-2.12.12

And to build mono
export PKG_CONFIG_PATH=/Users/amitava/local/lib/pkgconfig:$PKG_CONFIG_PATH
./configure --prefix=$HOME/local --with-tls=pthread

Friday, June 25, 2010

Bash one-liner to build classpath

I found this gem in clojure-contrib's clj-env-dir
EXT="$(find -H $CLOJURE_EXT -mindepth 1 -maxdepth 1 -print0 | tr \\0 \:)"
Invoke java now as
java -cp $EXT clojure.main

Thursday, June 24, 2010

Java 6 api docs in MacOSX

If you are using Firefox, you can access the built-in java 6 api docs using the following url

jar:file:///System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/docs.jar!/docs/api/index.html