PostgreSQL & Python on Mac

I’ve been playing with Django & MySQL for a while but for my next project I wanted to integrate it with a PostgreSQL database. Everything went well until I wanted to install Psycopg as my python adapter to PostgreSQL.

After a bit of blundering about here’s what it eventually took:

  • Download and install PostgreSQL one-click installer from http://www.postgresql.org/download/macosx. Remember to read the README file before actually running the installer.
  • Download the psycopg2 source from http://initd.org/pub/software/psycopg/.
  • Edit the setup.cfg file to provide a path to the pg_config executable. On my Mac it sits in /Library/PostgreSQL/8.4/bin/pg_config and is not by default on the PATH so if you don’t put it on the PATH or in this configuration file the next step will fail in a spectacular manner.
  • Run ‘sudo easy_install .‘ in the top level psycopg2 source directory.
  • Specify postgresql_psycopg2 when you configure Django’s database layer for your project.

Tags: , , ,

Manipulating collections with lambdaj

My day-to-day work often consists of writing web applications that aggregate data from a number of sources. Corporate constraints frequently dictate that I cannot use languages that make crunching of collections easier so I am forced into an old-fashioned for-loop frenzy. Ugh.

On a recent java project my pairing buddy (Jules – thank you) suggested that we look at lambdaj. From the lamdbaj site: “lambdaj is a library that makes easier to address this issue by allowing to manipulate collections in a pseudo-functional and statically typed way”. The following examples are very contrived but I’m sure you’ve seen something like them:

Old school:

List<Customer> activeCustomers = new ArrayList<Customer>();
for (Customer customer : customers) {
  if (customer.isActive()) {
    activeCusomers.add(customer);
  }
}
List<Account> accounts = new ArrayList<Account>();
for (Customer customer : activeCustomers) {
  accounts.addAll(customer.getAccounts());
}
Map<String, List<Account>> groups = new HashMap<String, List<Account>>();
for (Account account : accounts) {
  String type = account.getType();
  List<Account> group = groups.get(type);
  if (group == null) {
    group = new ArrayList<Account>();
    groups.put(type, group);
  }
  group.add(account);
}

New hotness:

List<Customer> activeCustomers = select(customers, having(on(Customer.class).isActive(), equalTo(true)));
List<Account> accounts = flatten(extract(activeCustomers, on(Customer.class).getAccounts()));
Group<Account> groups = group(accounts, by(on(Account.class).getType()));

My verdict: lambdaj certainly lives up to its promises. Obviously the old-school java code will be faster but is it easier to write or more readable? You decide …

Tags: , ,

Test definitions for developers

One of the best descriptions of the hierarchy of tests that I have seen comes from “Growing Object-Oriented Software, Guided by Tests” by Steve Freeman and Nat Price.

1. Acceptance Tests: Does the whole system work?
2. Integration Tests: Does our code work against code we can’t change?
3. Unit Tests: Do our objects do the right thing, are they convenient to work with?

Can we now get on with some work, rather than discussing definitions ad nauseam?

Tags: , , , ,