Create a virtual drive in Windows

June 13th, 2007 16:06

If you need to test your code on a Windows box with multiple drives but don’t want the hassle of partitioning check out the SUBST command. For example, to create a J: that will mirror your desktop:

c:\\>subst j: "c:\\Documents and Setting\\username\\Desktop"

To remove one of these virtual drives use the /D flag:

c:\\subst /D j:

Remember, this is only a virtual drive. Changes you make in the real directory are reflected on the virtual drive and vice versa.

I Wrote Some Java

June 13th, 2007 00:59

Wow, I fell back into Java land for 15 minutes or so today. After all my Python work I made a classic mistake by forgetting to end my line with a semi-colon.

Crazy Python

May 18th, 2007 12:14

Gotta love it:

class _(object):
    def _(self):
        class _(object):
            def _(self):
                print "here now"
        return _()
_()._()._()

EC2 Firefox Extension

May 17th, 2007 23:24

I tried out EC2 today. I used the precanned Fedora Core 4 image. I used the EC2 Firefox extension for administration and it was a breeze. Next step is to create my own image.

English v U.S. Spelling

April 13th, 2007 00:25

In general APIs and languages use the U.S. spelling of the word ‘color’. The wxWidgets project uses the English spelling ‘colour’. I am obviously after getting into the habit of dropping the u when coding as I keep getting it wrong in the wxPython code I am writing.

Python Development in Eclipse

April 7th, 2007 10:17

Eclipse is a good environment for developing using Python (or Jython) thanks to the excellent PyDev and PyDev Extensions plugins.

Writing APIs on top of Django

April 5th, 2007 09:40

Django provides a very neat filter method for retrieving results from the database. This method allows you to specify matching criteria for each field in a model (and any of the models related via a foreign key). Take the following model for example:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(maxlength=30)
    last_name = models.CharField(maxlength=30)

The following call will return all of the Person instances that have ‘Joe’ as their first_name:

Person.objects.filter(first_name='Joe')

and this call will return all of the Person instances that have ‘Joe’ as their first_name and ‘Bloggs’ as their last_name:

Person.objects.filter(first_name='Joe', last_name='Bloggs')

I like to keep all database calls in one module for abstraction. For example, db.py:

from myproject.models import Person

def get_person(fname=None, lname=None):
  if fname and lname:
    return Person.objects.filter(first_name=fname, last_name=lname)
  elif fname:
    return Person.objects.filter(first_name=fname)
  elif lname:
    return Person.objects.filter(last_name=lname)
  else:
    return None

Using this approach to get all the ‘Bloggs’ records would require a call like so:

db.get_person(None, 'Bloggs)

The first parameter as None isn’t so great is it.

My solution to this style of problem is to use keyword arguments as the only parameter to my db.py functions. This allows great flexibility and uses Django’s filter much more effectively. Here’s my new db.py:

from myproject.models import Person

def get_person(**kwargs):
    return Person.objects.filter(**kwargs)

The call to retrieve all ‘Bloggs’ records now becomes:

db.get_person(last_name='Bloggs')

For this trivial example it would probably be easier to stick to the named parameters example, but when you have models with a large number of fields I find the keyword arguments approach works brilliantly.

Tip of the Day

March 29th, 2007 10:45

Courtesy of the wxPython demo:

wxPython demo tip

del.icio.us + FireBug

February 7th, 2007 12:17

I installed the latest del.icio.us Firefox extension and it is fantastic. No more quick bookmarking in Firefox to be filed later everything will be going into del.icio.us from now on.

But, there is a problem. I have Firebug installed and they don’t play well together. The del.icio.us plugin spits out heaps of errors into the Firebug console:

del.icio.us firebug console errors

I haven’t debugged yet as I don’t have the time, but if I do I’ll post an update. I’ve notified del.icio.us support about this so I will update if I hear back from them also.

XML Survival Skills

February 5th, 2007 10:50

James Pasley has posted a screencast that covers the basics for XML, XML Schema, WSDL and SOAP. Luckily they’re introduced in that order so you can skip the nightmares that are WSDL and SOAP.

Newer Stuff  Older Stuff