View low bandwidth version

Archive for September, 2010

Code Kitchen: Overriding and keyword parameters

Monday, September 27th, 2010

I hinted, in the first Code Kitchen that there was a bit more to tell of the story of or recent Django refactoring.

Recall that we created a family of filter classes with the superclass doing all the work of returning what we need for our object tree-view, leaving the subclasses to redefine the filter_x() methods to define selection criteria for X, Y and Z objects.

Now, all the objects in our hierarchy have a Boolean deleted attribute, and we never want to display deleted objects. We could specify this easily using Django’s filter() method by saying X.objects.filter(deleted=False), but we’ve chosen to do something slightly more subtle. Class Filter defines our default criteria like this:

class Filter(object):
    not_deleted = {'deleted':False}
    ...

    def filter_Xs(self, **criteria ):
        criteria.update(self.not_deleted)
        return X.objects.filter(**criteria)

Python functions take two kinds of parameters: positional parameters and keyword parameters. Passing a dictionary, preceded by two asterisks tells Python that the contents of that dictionary should be used as the keyword parameters for that function. Similarly specifying a formal parameter preceded by two asterisks tells Python that we want to be given all our keyword parameters in one dictionary instead of specifying them individually. This way our function always receives a dictionary of its keyword parameters; if no actual keyword parameters are specified in the function call, the dictionary will be empty.

Filter‘s other methods call filter_Xs() (also filter_Ys() and filter_Zs()) with no actual parameters. The method uses the dictionary’s update() method to add the ‘not deleted’ criterion to the empty dictionary which then becomes the actual keyword parameter list for the call to Django’s filter() method.

Filter‘s sub-classes override filter_Xs() (also filter_Ys() and filter_Zs()) to define more specific selection criteria. Though these may be arbitrarily complex, one simple solution is to add more selection criteria to be used by the filter method. Some of our tree filters look like this:

class CountryFilter(Filter):
   country = None

   def __init__(self,country):
        self.country = country

   def filter_Xs(self,**criteria):
        return  super(CountryFilter,self).filter_Xs(country=self.country)

   ...

A CountryFilter is instantiated with a Country object to match on. Its filter methods use super to call Filter‘s implementations of filter_Xs(), etc., specifying their additional criteria with conventional keyword parameters. The superclass implementations of these functions work the same as I described above except now they are adding ‘not deleted’ to to the ‘matches country’ criterion to from the final query.

Filter‘s methods look slightly odd at firs sight. But we write them once and, in return, the implementations of many specific filter sub-classes become simple and uncluttered.

Code Kitchen: Refactoring Django views

Saturday, September 25th, 2010

We, at Aptivate, love openness, participation and learning. We’re also into International Development and, often, we write software. Code Kitchen is a new idea to help us learn to become even better software developers using openness and participation. (more…)

Solar Diaries – Day 5 & 6 – Behaviour Modification and Diet Failure

Saturday, September 18th, 2010

Day 5

A cloudy day. This is one of the interesting things about taking responsibility for my electricity production – it’s not just that I replace the electricity I used to use. The awareness I am getting is modifying my behaviour.

I generally think of myself as someone who tries not to waste energy. I turn lights off when I leave rooms etc. But trying to run my working life off an under-dimensioned solar power system is being a real education in energy wastage.

Today I’m having the usual one hour lunch break. Normally I would probably leave my computer on. Today I don’t even suspend the laptop, I shut it down completely… and unplug its 12 volt regulator. I want to see 0.00 amps flowing out of the battery when I’m not using it.

I’m also aware that I’m starting the day with a fairly empty battery and it’s cloudy – so today I’m not using the external monitor…

(time passes)

…I got through the day but around 5pm the solar charger shut down the load to protect the battery from getting too flat. So I worked the rest of the day, up to 6:30, using the internal battery of the laptop. By the end of the day I’ve got 10 minutes battery left on my laptop, about the same on the Samsung / PixelQi netbook and a shut down deep cycle battery. Hmmm. (I’m actually writing this blog entry on Day 6 because I ran out of power on Day 5)

Day 6

The forecast was for sun this morning, potentially worsening through the day. I’m starting the day with an empty solar battery and no reserves on the laptops. I have to get up early (early for me anyway, if not early by international standards) to get the panel out. At a push, the sun starts being usable from about 8am so I’ve got 2 hours to get enough charge on the battery.
PV Panel out the front of the house

Luckily, yesterday evening, the 15m extension cables for the PV panel arrived so I don’t have to lug the battery around. I start by dragging the panel upstairs to the east facing bedroom to try and get the early sun. By the time I’ve got it all plugged in the sun’s rising high enough that it’s hitting the doorstep out the front. I take it down there instead. The panel’s bigger than any of the panes in our windows so there would always be a shadow from the window frame which would seriously reduce the power output.

As the minutes tick by I’m glued to the multi-meter watching the voltage slowly climb, repeatedly running out the front, rotating the panel to face the sun. (There is some debate about whether it is better to spend money on mechanical trackers or on more static PV panels instead. However manual tracking is free… if a complete pain in the back-side.) It gets to 10am, time to start work, and the solar charger is still disconnecting the load. I use my last few minutes of laptop battery to skype in to the office for the morning meeting and then, with a sad face, I plug the laptop into the mains. I’ve broken my mains-free diet. And what’s even more irritating is it’s a gloriously sunny day. There’s probably enough power coming off the PV panel to run the laptop directly, but I’m not set up to do that… yet. Digital-Resilience #fail.

I’m hoping the battery will get enough charge over the weekend to see me through next week. Until I’ve built my office-cum-shed with the south-facing roof in the garden, I’ve got nothing to attach the PV panel to in a permanent way. We’re away for the weekend and I’m worried the panel might go for a walk if I leave it out in the garden so it’s currently inside standing vertical against a west-facing window. Knowing the PV panel is less than optimally placed is taking some of the joy out of the fabulous sunshine we’re having today. I did try and persuade the family to bring the battery and panel away with us. They think I’m obsessed. Luckily for family relations the panel is too big to fit in the boot of the car and the battery is too heavy to carry on the train.

If I make even more gridbeam I could, in true agile fashion, just make the roof of the shed.

The Solar Diaries – Day 4

Thursday, September 16th, 2010

Image of PV panel in gardenIt’s the beginning of day four of my solar experiment and off to a good start. The sun is out this morning, bright blue skies, although the forecast is for the weather to worsen over the day. So getting up early and getting the panel in place is important today.

I am using the equipment we used for the “Small Is…” festival to see if I can go “off-grid” for my work electricity requirements. This is a pilot to investigate the possibility of the whole of Aptivate being powered by renewable energy.

Because my house faces East-West, to get the morning sun means either dragging the panel through the house and putting it outside the front door or dragging it down the far end of the garden. Trying the garden this morning – it will be interesting to see how the sector of sun moves across this part of the garden.

Right now I’ve only got about 4 metres of cable to run to the PV panel which means I have to drag the 33kg battery down the garden too – made all the more difficult today because I broke my toe yesterday running into my baby’s high-chair!

Really I need to get the panel up higher… which means drilling more gridbeam.

Today was going to be a particular challenge to stay “off-grid” as I’m supposed to be pair-working out of “The Skiff” with Nate. I’ve had a last minute reprieve as Nate’s not going to be there until the afternoon – but how do I stay off grid there? The battery is too big to carry.

For pair-working we want to use a decent sized screen. We use an one of the Iiyama 12 volt 24″ monitors, which in Eco mode takes about 20 Watts. Interestingly the Samsung N220 netbook we have can drive the big screen quite happily. It would be useful to get a bigger battery for the netbook anyway, so I could conceivably run a whole working day off the netbook’s battery. That only leaves powering the monitor.

There are small lead acid batteries that I could carry to the Skiff. For an 8 hour day I would need a battery with about 20 Ah. That’s 20 Watts / 12 Volts  x 8 hours x 1.5. The 1.5 is to make sure the battery never gets below 1/3rd capacity as this would shorten its life.

A shadow has shifted and is covering one of the cells on the PV panel, and if the voltage from one cell drops it tends to pull the others down with it apparently, so be back in a minute…

…ow! Never try and cut back brambles without gloves. I didn’t realise this solar experiment would be so dangerous.

A 20 Ah lead acid battery would weigh about 6kgs – which could just be carried but is on the heavy side. The screen takes about the same power as a netbook, so I’m wondering if a lithium-ion battery would be more appropriate – they are about a third of the weight. I imagine I would need some kind of charge controller for it – the solar charge controller is specifically for sealed lead acid batteries.

…Nate and I didn’t use the large screen for pairing today so I’ve got through day 4 without mains electricity using the battery in my laptop and netbook for the afternoon.

battery voltage at the end of day 4:  12.11V

Digital Photography on Linux

Thursday, September 2nd, 2010

Many people think that digital photography on Linux is much harder than on Windows or Macintosh. This is a typical comment:

As a hobby I’m a photographer, so I do lots of graphic work as editing for invites, posters, mag covers etc. does any one know of a software that will be as great as photoshop for linux (Ubuntu), not Gimp, Gimp is very limited. I tried using WINE but it doesnt work well, very slow.

It was true for a long time that working with digital photos on Linux was difficult. The main problem was that the most popular software that users are familiar with, Photoshop and Lightroom, is simply not available for Linux (due to unfortunate business decisions by Adobe).

There have been open source alternatives to Photoshop for a while, notably Gimp, but Gimp is difficult to use and the interface is quite annoying:

I’d say the major limiting factor is the desire to slit your wrists while arguing with the unwieldy interface layout.

Luckily today I came across (thanks to Alastair Otter of GLUG) this excellent article:

Photography with Open Source / Linux

This includes detailed descriptions of the many applications available, most of which I hadn’t heard of before, and look forward to trying. I’ve been particularly impressed by Adobe Lightroom’s ease of use and powerful histogram correction features, and I was considering buying a copy even though it doesn’t run on Linux, but I will definitely try Darktable now.

For users who really, really have to have a Photoshop user interface, GimpShop may help.

And before you say “this is not work related”, I’ve often needed to edit photographs to prepare brochures, promotional materials and for website work :-)

It seems that digital photography on Linux has made excellent progress in the last year, and I’m very happy to see it.