Calculating TAS

Chargeable income

MSD MAP definition of the above

Salaries, wages, WFF, benefits, AS, DA, everything that's a plus in the account, pretty much.

Henceforth known as chargeable_income.

Allowable costs

MAP

Most of your ongoing regular living expenses (NOT FOOD) some odd ones, sad ones, and some fairly generous provisions IMO. Essential televisions? More policy bound, so read it.

This is allowable_costs, I'll keep going like this.

Disposable income

It's your cash monies, it's your luxury items. It's also your food, your smokes, your dog food and your car (unless you argued for an allowable cost for your car, in which case, nevermind). This is where your food fits in, in the formula. Unless you have diabetes or other special food needing diets (please see Disability Allowance).

  chargeable_income - allowable_costs = disposable_income

You'll wanna write disposable income down.

Standard costs

Copy and paste from: MAP. Read it! Horse's mouth and all that.

Standard costs are the proportion of living costs that the client is expected to meet from their benefit or income.

Standard costs for a client receiving a main benefit

70% of the Net rate of main benefit (before any income abatement or other deduction) AND 70% of Family Tax Credit. See above link! (NoTE: Not yet sure if FTC includes the In Work Payment (IWP). I suspect not, so make your numbers worst case and don't include it.

Standard costs for a non-beneficiary

70% of the Net rate of Unemployment Benefit (before any income abatement or other deduction) AND 70% of Family Tax Credit. So, in the below calculation, use the UB rate for main_benefit_rate. Same caveat as above for FTC.

Which means

 (main_benefit_rate \* 0.70) + (family_tax_credit \* 0.70) = standard_costs

Write this down too.

Deficiency

How broke you are. Opposite of a surplus. WINZ works it out so that the deficiency is expressed as a positive number. That is, if you are $50 short this week, you have a deficiency of +$50. Likewise, if you have $25 extra in your wallet, you have a deficiency of -$25. That is, you don't have a deficiency. I guess it makes the maths easier.

So anyway, remember: positive deficiency means that you have less money than you need to cover your costs.

  standard_costs - disposable_income = deficiency

Write it down, and remember, a negative deficiency means you don't need TAS (yay!).

Upper limit

The upper limit is the maximum amount that can be paid for Temporary Additional Support unless the client is eligible for a disability exception amount. I'm ignoring the disability exception amount for now. It's in the link for the curious.

Upper limit for a client receiving a main benefit

30% of the Net rate of main benefit (before any income abatement or other deduction).

Upper limit for a client not receiving a main benefit

30% of the Net rate of Unemployment Benefit (before any income abatement or other deduction).

Which means

  main_benefit_rate \* 0.30 = upper_limit

TAS eligibility

If deficiency > upper_limit, then the amount eligible is upper_limit, otherwise, it's the deficiency.

That's it.

DISCRETIONARY SUPPLEMENTS

TAS eligiblity is determined by a case manager, not a website. Don't trust this, read the links to MAP. Here's another one, bookmark it. Talk to lawyers or beneficiary unions and/or advocates for proper advice.

Published: 2nd June, 2010

Microsoft's documentation is very wrong about CSV.

I've fired up Visual Studio again, to work on a pet project of mine, and I searched the latest and greatest MSDN Library for "CSV" and found two articles explaining how to use it with LINQ. Ah, thought I, this will no doubt demonstrate how to use .NET's CSV parsing library with LINQ...

The article on Reordering the fields of a delimited file helpfully explains:

By using the Split method to separate the fields, it is very easy to query and manipulate CSV files by using LINQ.

It even has a code sample:

// Create the query. Put field 2 first, then
// reverse and combine fields 0 and 1 from the old field
IEnumerable<string> query =
    from line in lines
    let x = line.Split(',')
    orderby x[2]
    select x[2] + ", " + (x[1] + " " + x[0]);

The article on how to compute column values in a csv text file also uses a string split on comma to separate the fields, although it also gives the helpful to split on tab if it's tab delimited.

Now, I'm aware that these are just examples of how to use LINQ more than examples of how to parse CSV, but they are the only remotely relevant results I can find for "is there a standard library in .NET that handles CSV" (No, it would seem), and as such people might take their advice ad verbatim - at the very least add a disclaimer saying "This will do hilarious things in production."

If you want an illustration of why splitting on comma is incredibly useless and downright wrong for anything beyond simple problems with data 100% guaranteed to arrive in the precise format you expect, use LINQ and string split to work with this valid CSV data:

Name,Age,Favourite Ungulate
"James, Earl of Stratford, Defender of the Heath, Occasional Plumber",24,Oryx

There's a reason that there are CSV parsers.

Although admittedly I haven't yet figured out which one on the .NET platform is best, although this one looks promising. You can also use OleDbConnection to handle CSV; though like a lot of things Microsoftish it seems to get needlessly complex fast, as it can require modifying registry keys to handle mixed data types. For someone who learnt to code in Python, I find this lack of batteries in .NET disturbing. CSV is a relatively common file format.

TL;DR - Microsoft documentation demonstrate the absolute worst way to handle CSV - and I have a hunch that a lot of MSDN code samples end up copied and pasted into production codebases.

Published: 23rd January, 2009

Tramping the Edwards - Hawdon

So a mate and I went for a walk in the hills. We did the Edwards - Hawdon trip, where you travel up the Edwards Valley to Taruahuna Pass, cross Tarn Col and drop down into the east branch of the Otehake River, and then cross Walker Pass into the Hawdon Valley and then out. It was awesome. Brutal at times, cold at other times, but awesome. I learnt a lot about my limitations and hubris, which is usual for the mountains - they don't really care about your pretensions.

So, here's the story and pictures. Oh, and a Google Maps map with mentioned mountains, rivers, huts etc. marked. Blue is landmarks/passes, green is mountains, wavy lines are rivers, and yellow is huts.

By the way, true left and true right are the left and right of a river when looking downstream. So when you're walking upstream on the true left, it's your right. Also, clicking on all photos will open a larger version.

Starting at Greyneys Corner, about 6 km before Arthur's Pass village, you have to cross two rivers, the Bealey and the Mingha straight off the bat. This photo is after the two crossings, looking up the Mingha to Mt Oates. The Mingha River is one half of the mountain run section of the Coast to Coast, they run up the Deception River, across Goat Pass and down the Mingha. Shortly after this photo, we found that the river had changed course and come up hard against a bluff, so we had to recross the Mingha another two times to get to the track around the lower gorge on the Edwards River. This is typical of tramping in Arthur's Pass, as DOC has decided that bridges are for pansies. Rivers were running high (AP had had 150mm of rain two days earlier), but crossings were okay if you picked your spot carefully.

This photo is looking towards where the Edwards joins the Mingha. The Edwards Valley is the low bit to the right. The spiky mountain right at the front is called The Spike. The higher peak behind it is Mt Williams.

We picked up the track into the Edwards from the Mingha - if the river's low you can walk up the lower gorge, but as they weren't, we got to grunt up a small hill. ACTION SHOT:

In the Edwards, looking back the way we'd come. That's Mt Bealey in the distance, which is a day walk from Arthur's Pass village. Has a fun scree off the low summit followed by a bush-bash/boulder-hop out down Rough Creek. Highly recommended if you're not afraid of a bit of a challenge, unlike these guys:

Two male 60yr+ overseas tourists climbed Mt Bealey via the normal track route. Whilst on descent they noted that another party was descending the "Bealey Scree" from the Low Peak (this experienced party included children), they decided to follow having no idea of the route ahead. When they reached Rough Creek they discovered the normal entanglement of trees and river crossings, and decided this route was beyond their ability. They returned to the base of the scree and used a cellphone (at 1830hrs) to notify police of their predicament stating they were unable to ascend back to the normal route, and were too exhausted to be led down. Westpac Air Rescue from Christchurch was dispatched to recover them.

I feel a bit mean laughing, but it's a valuable (and humorous) lesson.

/tangent

This is looking up the Edwards River towards the confluence of the East Edwards, it's the valley coming in on the right. From where the photo is taken, it was a saunter up some nice river flats on the true left to the confluence, (the true right was not a good side of the valley to walk on.) Our biggest challenge was the heat (if you can see the heat haze), but there was ample shade for the most part.

Avoiding the true right was due to rockfall issues, there's a notorious area called Gunshot Alley, where stuff likes to fall off Mt Williams into the Edwards all the time. And some of it is huge. House sized. This is before Gunshot Alley, and that rock is only twice as tall as me:

A little further on, looking upstream, you can see that the valley curves away to the left as it enters its upper gorge, which is impassable without pro rock skills. There is a bypass track through the forested slope in the centre of the picture. If you look up at the top of the ridge, you can see several rock ribs coming down - they make the bypass track a bit of a heart breaker, but more on that later. The scree at the left of the forested area is the start of the upper valley.

We arrived at the confluence, and sat down and had a munch, refilled our water (can't beat fresh mountain river water), and prepared for... the grunt. We also saw a blue duck fly past, which was nifty.

How to describe the next bit. So basically, the upper gorge in the Edwards river is, for all intents and purposes, impassable. So deer cullers in the 30s cut a track around it in order to access the upper valley, where there were large populations of deer at the time (no longer, sadly, seems to have been shot out.) Now, those deer cullers were hard men. So they cut a track that does not mess around. As I mentioned earlier, the slope has several rock ribs running through it. So the track has to sidle around them. And it usually does this by going up... and then down... and then up... and then down. Repeat that for three hours and you get the idea. It's slow going, and it's a bit tough on bodies honed by years of playing Counterstrike and eating greasies. Anyway, enough self-pity. This is a shot of the opposite hillside, these bluffs are under Mt Williams and they reminded me of those shots of Vietnam you always see.

About half-way through the track (according to my map), we stopped for lunch as we were getting tired, dehydrated and a bit demoralized. It was a good place to stop, as it was on a bluff above one of the waterfalls in the gorge. Some good sandwiches followed by a luxury item (stuffed olives) revitalized us and we moved on.

Ego shot:

An hour or so on, we stopped at a lovely wee stream located at the end of one bluff (that the track descended so steeply from that DOC had provided a chain as a hand-hold). This first photo is looking downstream and very down hill, and it's a nice optical illusion because it looks like the water is flowing towards you, when it's actually flowing away from you. It's just a really steep hillside with the trees growing on a funny angle. Also, the grassy stuff is snowgrass. It's important later on. The second photo is looking upstream. It was really a nice wee spot, as the water cooled the air around it, and well, it was just a tiny wee oasis of calm, quiet, and above all, about 1.5m of flat track. After several hours walking up and down slopes, you really begin to appreciate flat.

After one last uphill slog over a bluff (and it was the longest too, did I mention that the deer cullers were hardcore?), we reached the final downhill that brought us to the upper valley above the gorge, where we walked through a mix of mountain beech and subalpine herbs.

Here's another shot, showing the back of Mt Oates better (the mountain from the first photo) - it's the rocky/snowy bit at the back of the ridge:

Half an hour later, we reached Edwards Hut, which is a cosy wee one, just on the edge of the bushline.

After we rested up, and cooked some spag bol for tea (the extra weight of the mince and sauce was worth it, IMO), we went for a walk upstream to see if we could see any blue ducks/whio, as the last time I'd been up here I'd spotted a family of them. We walked fruitlessly for half an hour, and as we were about to turn back, we decided to go to the next corner of the river, and bingo, we spotted a male whio. No pictures of him unfortunately, as I hadn't brought my camera. Cue face palm. So I've filched someone else's photo to illustrate. But it was really cool. When the duck saw us, he whistled a few times, which they do to warn their partners and any ducklings, so we know he had at least a partner (they mate for life) if not some kids. We stayed stock still to see what would happen, and he started trying to fake us out - he'd turn around and pretend to preen and then spin around real fast to see if we'd try to sneak up on him, he did this a few times and then decided we weren't a threat, so went back to feeding/playing.

And man, it was amazing. He'd go under water, swim up a rapid, and then pop out on a rock above it, and then jump back in, swim down the rapid underwater, pop back up. So stoked to see it. Here's a video of blue ducks feeding. Was also stoked to see that DOC had been laying lots of possum bait and stoat traps up the valley to protect the blue ducks and great spotted kiwis/roroa in the area. There are kiwi living around Edwards Hut, but they only call on moonlit nights, and the moon set before dark, sadly.

Our next day's journey would see us heading upstream to Taruahuna Pass under Falling Mountain. Here's the view of Falling Mountain from the hut's main room:

The next day, we woke up to a less than stellar forecast via the radio from DOC headquarters. Rain in the west, winds reaching gale force around 2000m. The wind was a concern for the ascent of Tarn Col as it was quite exposed, although we'd only be ascending to just under 1400m, so it was not impossible. The rain was going to be annoying because Taruahuna Pass is on the Main Divide - that is, when you cross Taruahuna Pass, you end up on the West Coast. Which means that we were going to get some of the West Coast weather. We weren't expecting too much, as if you look at a map, the Edwards valley runs north/south, but I prefer no rain at all, makes stuff a lot more pleasant. Oh well. We headed off, swung past the river to see if we could spot the blue duck whanau again, now that we had a camera, but no luck. We continued upstream on the river flats, trying to avoid the "river flats made out of big boulders with scrub on top of them", and made pretty good time. We occasionally got strong blasts of wind coming down valley, and the clouds at the head of the valley looked a little unfriendly, but not life threatening. This photo was taken at morning smoko, looking towards the pass. Apologies for the underexposed photos, I'm a photo newbie.

We continued onwards until we reached the confluence of a small side-stream off Amber Col and the Edwards, and there was all these awesome waterfalls coming down the hills. Here's a bad photo I took of one:

At the confluence we sat down out of the wind (which was having a bit of a howling fit at this point), and stocked up on energy for the crossing of Taruahuna Pass, as it's not a place you really want to stop. I took this photo looking back down the Edwards. I've haxed this photo due to yet another underexposure to make it look a bit more like it actually did. The pyramid shaped mountain to the right is Mt Williams, and Edwards Hut is at the bottom of this face of it, to illustrate how much distance we'd covered.

As we came around the corner, Taruahuna Pass came into view. Here's an aerial photo of it:

This is what it looks like coming up the valley:

Another shot, showing more of Mt Franklin on the left:

As we were about to leave the Edwards River behind, we said goodbye, and took some glamour shots. Here's me doing valiant-explorer-who-happens-to-be-dressed-like-a-clown, I'm pointing down valley:

And then off to Taruahuna Pass. I didn't take any photos while crossing it, as we were quite exposed to the wind, and we were warmed up and going well, so didn't want to break the rhythm. But once we got to the bottom of Tarn Col, I took this photo looking back:

We readied ourselves to gain Tarn Col. Tarn Col is the obvious saddle in the ridge in this photo:

Here's what the ascent route looks like from the bottom. You go straight up the gully.

At the point in the photo where the gully narrows dramatically is a series of small waterfalls. In the dry, it'd be fine to ascend up the waterfall, but as this has been a somewhat wet summer, it was coated in a nice layer of slime. So no safe ascent was possible that way. So we had to climb onto the rocky rib to the right of it (as it's a stream, I suppose I could say true left, although to be honest, it felt like more of a lazy waterfall), and that was pants-fillingly scary. It dramatically increased our vertical exposure, and the rain came in while we were on it. Which quickly wet the snowgrass slope we were on. If you know snowgrass, then you'll know that it's lethally slippery when wet. And anyway, that's what we were using for handholds, snowgrass. And footholds. Slipping would've likely causing an incapacitating injury. Needless to say, neither of us particularly enjoyed that bit, and we got up it in record time.

Normally, the top of Tarn Col is a lovely place to rest, but the wind and rain were howling through, so we kept going. You descend from Tarn Col via a stream that goes through a canyon. We were heading into the canyon when we met a family of seven from Wellington coming up. They were apparently going to "camp in the Otehake and then head out down the river to the road." They had two children with them. This is what the route guide for the Otehake says:


Experience: Suitable for well-equipped and experienced back-country trampers only.

Otehake Wilderness Area

Travel down the Otehake is much slower than the distance on the map seems to indicate. The river itself cannot be travelled and should only be used where the track leads to it. Parties intending to travel out to the main road in one day down the Otehake should be fit and leave early. The route out through the Edwards is considerably quicker and easier.

While the route out down the Otehake does not necessitate crossing the Otehake itself, there are several side streams that can rise rapidly and often cannot be crossed, and one place (at the start of the track bypassing the last gorge above the footbridge) where it is necessary to enter the Otehake to round a bluff to pick up the start of the track. Consider the weather, state of the river and fitness of the party before venturing off down the Otehake.

Here's what the Arthur's Pass website says:


Not recommended unless you're looking for a long sidle through thick bush by a track which is overgrown and fallen away. Each kilometre may take you 1-2 hours through this wilderness area. This route is the hardest tramp in Arthur's Pass National Park!

Nice place to drag some kids eh? These people either had done no research on the route that they were intending to follow out, or they hated their children a lot. We suggested they consider exiting via the Edwards, and as I haven't heard of anyone needing rescuing from the Otehake, I presume they went that way. Either way, a real face palm moment.

/self-righteous soapbox

Anyway, so we dropped into the canyon off Tarn Col and followed it down, I've always enjoyed the canyon section, travel is actually reasonably easy due to chamois/human tracks worn into the sides. Here's the canyon:

As I said, I've always enjoyed the canyon section. The bit after that... not so much. The stream leaves the canyon and some river flats develop. But they're all heavily scrubbed at best, and comprised of large boulders with massive gaping holes to break your leg in between them and heavily scrubbed at worst, so travel gets really slow and arduous. The best way is to work down the actual river, but it really needs warm weather for that, and unfortunately, the warm weather was 15km due east. We were still on the West Coast baby. The westerly was coming up the Otehake east branch and funnelling cloud up the steep valley we were descending, and as it rose and hit the mountains, it started to rain on us. And as we descended further, it became apparent that it had been raining for quite a while. The waist - chest deep scrub we were navigating through was thoroughly wet, so we became thoroughly wet from the chest down. Our travel slowed down dramatically. We were burning a lot of energy, and were very wet and starting to get cold. These are major risk factors for hypothermia. But in the narrow valley we were in, we couldn't find any viable shelter, as the wind was entering it from the west and the east. So, we kept on going. We were still okay, just feeling a bit sorry for ourselves.

We finally got out of the valley, crossed a large bog above the confluence with the Otehake and then crossed the Otehake East branch. We found a large bluff that had about two square metres where there was no wind or rain. Finally, after all the suffering bluffs had brought us, one of them redeemed all bluffery in my mind.

This is my mate in the Otehake East branch. Look cold?

We sat down in the tiny dry bit and made the most awkward lunch ever. As in, trying to get things out of our packs without upsetting the stove in the little space we had was highly awkward. But after a feed of noodles, and a cup of tea, both of us were feeling a lot more capable, a lot warmer, and most importantly, a lot happier. Morale is really important in these things IMO.

Here's the photo of Bluffy, my new best friend in the whole Otehake catchment, and where we were sitting (that's my pack):

Okay, so it's a useless photo, but words cannot express how much I loved that tiny patch of dry warmth. But I learnt a very salient lesson - if you're going out in marginal conditions, be really prepared. I thought I was prepared, after all, I had heaps of warm clothing, water-proof jackets etc. etc. but I was wrong. If I'd had some kind of tent, we could've set up shelter where ever we pleased, and thus had a lot more pleasant time - even if we just stuck up the tent for a cup of tea. Frankly, it was only providence that we found what shelter we did. I don't think we would've ended up with hypothermia, as from that bluff, it was only an hour's walk to get out of the rain and most of the wind, but when you're cold, tired and demoralized, mistakes are a lot easier to make, and in that country, mistakes are usually bad, and help is a long way away. So. A bit galling to make that mistake. Might have to rethink my "I am never wrong" stance.

From that point on, I didn't take anymore photos as it was getting late in the day, and to be honest, the West Coast was getting old. So we walked upstream for half an hour, and ascended onto Walker Pass. Walker Pass is absolutely lovely on a sunny day, it has a nice wee lake that you can swim in to demonstrate your cajones (and what they look like when they're really small), and is easily accessible from Hawdon Hut, which is itself easily accessible from the road end, so if you get a chance, have a look. From the lake we picked up the poled route that DOC had put in - last time I did this, the entire thing was nearly entirely unmarked, it seemed that they've had a few idiots get lost or something. "What, go upstream? Man, what does that mean? I might go downstream instead." - and we followed the track to the edge of the plateau. The track along Walker Pass is great fun, criss-crossing quite a charming wee stream (Twin Falls stream), again, really awesome in warm weather.

We gained the track off the plateau that bypassed the two waterfalls and big cliffs on Twin Fall Stream, and after a bit of a knee jarring descent, found ourselves in the Hawdon valley. Ten minutes downstream, and we arrived at Hawdon Hut:

It was built in 2007 to replace the old hut which burnt down, and man, it's pretty swish. Awesome fire, two toilets (always important in a twenty man hut when everyone's got to go for that morning wizz), and not only that, but some kind of fancy long drop that doesn't feature a large pile of fly infested faeces, and a great view. Here's the view from the hut, it looks up to the headwaters of the Hawdon:

The next day is quite a short one, and we were being picked up at 4pm, so we lazed in bed until about 11am, and then left at 12pm. Travel was pretty uneventful, we followed a track down to just above the confluence with the East Hawdon, and crossed the river, and then crossed the East Hawdon. We followed the river flats down on the true left for a bit, and this is where I took this photo looking upstream:

And to demonstrate how the rain shadow of the Alps works, I turned around, and took a photo downstream:

We crossed to the true right, and followed an old unmarked path along the terraces in the hope of seeing some parakeets, but no luck. We rested at a nice wee spot, and then headed out.

Published: 14th January, 2009

Syntax highlighting colour schemes

Tohtml.com, convenient as it is, doesn't do Scala, so I'm now trialling Andre Simon's syntax highlighter and seeing how the various default colour schemes fit.

Bright



class A(val x: String) extends B(x) {

  val y = x.trim()

  def yUp(): String = {
      var z = y.upper()
      z
  }
}

object A {
  val companionClass = true
  var z = (new B).yDown() match {
            case Some(lower) => lower
            case Nil => ""
          }
}




Orion



class A(val x: String) extends B(x) {

  val y = x.trim()

  def yUp(): String = {
      var z = y.upper()
      z
  }
}

object A {
  val companionClass = true
  var z = (new B).yDown() match {
            case Some(lower) => lower
            case Nil => ""
          }
}




Rand01



class A(val x: String) extends B(x) {

  val y = x.trim()

  def yUp(): String = {
      var z = y.upper()
      z
  }
}

object A {
  val companionClass = true
  var z = (new B).yDown() match {
            case Some(lower) => lower
            case Nil => ""
          }
}




Golden



class A(val x: String) extends B(x) {

  val y = x.trim()

  def yUp(): String = {
      var z = y.upper()
      z
  }
}

object A {
  val companionClass = true
  var z = (new B).yDown() match {
            case Some(lower) => lower
            case Nil => ""
          }
}




ide-anjuta



class A(val x: String) extends B(x) {

  val y = x.trim()

  def yUp(): String = {
      var z = y.upper()
      z
  }
}

object A {
  val companionClass = true
  var z = (new B).yDown() match {
            case Some(lower) => lower
            case Nil => ""
          }
}

Hmmm.

Published: 3rd January, 2009

markdown2 vs python-markdown

I'm using markdown for formatting my blog, and was having real issues with code samples generated by tohtml.com being formatted incorrectly by the library django.contrib.markup uses for markdown, python-markdown. So I thought I'd trial markdown2.

Here's the difference...

python-markdown

Code formatted incorrectly

Not only does it insert paragraphs inside <pre> blocks, and then create inline <pre> blocks, but for some reason, it messes with my indentation as well, never a good look when posting Python samples.

markdown2

Code formatted correctly

Kudos to Trent Mick, the author of markdown2. I've wrapped markdown2 in a Django template filter that works pretty much like the markdown filter, I'll make it available as soon as I write the capacity for file downloads into the blog.

Published: 3rd January, 2009

Python - you can't use property() with a classmethod

Not that it's a particularly common use case, I suppose, but still, it's worth noting. I was trying to use a Scala paradigm (computed values) in Python, so I'm not surprised. The code that failed:

class Category(models.Model):
    title = models.CharField(max_length = 50, unique = True)
uncategorizedTitle = "Uncategorised"
@classmethod def getUncategorised(cls): uncategorizedList = Category.objects.all().filter(title=cls.uncategorizedTitle) if uncategorizedList: return uncategorizedList[0] else: uncategorized = Category(title=Category.uncategorizedTitle) uncategorized.save() return uncategorized
uncategorised = property(fset=getUncategorised)
class BlogEntry(models.Model):
category = models.ForeignKey(Category, blank=True, null = True)
def save(self): if not self.category: self.category = Category.uncategorised
super(BlogEntry, self).save()

Removing the property and just calling the method directly works as expected. Admittedly, doing it as a property was entirely unnecessary syntactic sugar, no doubt stemming from the semantic connotations that I associate with the Enum.SpecificSubType syntax.

For an explanation of what I mean by a computed value, here's a pseudo-Scala transliteration of the Category code (without the pretty colours, unfortunately):

class Category extends models.Model {
    var title = new models.CharField(50, True)
}

object Category {
    val uncategorisedTitle = "Uncategorised"
let uncategorised = { Category.objects.all().filter(title == uncategorisedTitle) match { Nil => { uncategorised = new Category(Map("title" -> "uncategorisedTitle")) uncategorised.save() uncategorised } first :: _ => { first } } } }
Published: 2nd January, 2009

Django's ORM and auto incrementing non-primary key fields

I'm writing a to-do list, which will be ultimately become draggable and droppable. I'll need some way to maintain the ordering resulting, and an autoincrementing integer would be the simplest way of creating this. Space the increments far enough apart, and then you can reorder items a reasonable amount of times before having to update more than one row to do so.

(There's probably some algorithm to determine the optimal spacing for this kinda thing for N items being reordered X times... wish I knew enough about algorithms to figure out which one, I imagine it operates along the same lines as algorithms minimizing hash collisions).

Django's AutoField

Django has an AutoField, which seems like the simple approach. Of course, if it was, I wouldn't be writing this. AutoField itself presents two problems. The first is this:

class AutoField(Field):
    empty_strings_allowed = False
    def __init__(self, *args, **kwargs):
        assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__
        kwargs['blank'] = True
        Field.__init__(self, *args, **kwargs)

So firstly, AutoField's constructor requires that it be passed the argument to create it as the primary key. Then there's this, another method of AutoField:

def contribute_to_class(self, cls, name):
        assert not cls._meta.has_auto_field, "A model can't have more than one AutoField."
        super(AutoField, self).contribute_to_class(cls, name)
        cls._meta.has_auto_field = True
        cls._meta.auto_field = self

Which means that a model is only allowed one AutoField. These restrictions seem to relate to keeping Django working on all RDBMS backends - has_auto_field and auto_field seem to be used mainly in the lower levels of the ORM, in functions like sql_model_create (in django.core.management.sql).

Custom models.Field

This is another option - I think... I envisage writing an autoincrementing column with a specifiable increment for SQLite would involve a hefty amount of triggers. But at this stage, I honestly don't have the knowledge of Django's ORM to do this, or the time to develop that knowledge.

Amending the table schema directly

This would be the simplest way to fix it, create a model.IntegerField in Django and then fire up the MySQL client and alter the column to autoincrement, but it's not portable. For starters, I develop on SQLite locally, so it wouldn't (easily) work on my dev machine, and secondly, every time I reset the app, I'd have to change the schema again - I tend to reset the app quite often when changing the model while playing, as it's simpler than doing the alter tables, as the data being stored is usually gibberish.

Do it in the model itself

So ultimately, I decided on overriding the save method of the model that needed the ordering field, and handling it there to keep my model portable, and the simplest way to do this was to use one of Django's new abstract base classes:

class OrderedEntity(models.Model):
    orderingField = models.IntegerField()
    orderingIncrement = 5
    def save(self):
        #Only need to populate orderingField if entity hasn't been saved yet
        #If it's been saved, it'll have a primary key.
        if not self.pk:
            try:
                max = self.__class__.objects.order_by("-orderingField")[0].orderingField
                self.orderingField = max + self.orderingIncrement
            except IndexError:
                #No objects yet exist
                self.orderingField = 0 
        #Call actual save method to finish populating DB
        super(OrderedEntity, self).save()

class Meta: abstract = True

Which can be used as follows - note that I'm overriding the ordering increment:

class ListItem(OrderedEntity):
    description = models.CharField(max_length = 100)
    completionDate = models.DateField(null = True, blank = True)
    todoList = models.ForeignKey(TodoList)
orderingIncrement = 20

Of course, this is just the model representation, the interesting logic will come when it's time to reorder the items, but it'll be useful any other time I need an autoincrementing field in Django that isn't a primary key.

Published: 4th July, 2008

Javascript and the pitfalls of weak typing

've had a bit of free time of late, due to the sleepless nights that my newborn son is assisting, and for some reason or another, I decided to write an extension for KomodoEdit that worked along the same lines as an addin for Visual Studio called CopySourceAsHtml.

The problem

Anyhoo, I had a bug that I couldn't figure out - in my HTML writing function, all my line delimiters were being converted to &nbsp;. That is, whenever \r\n or \n were encountered, the function was supposed to close any open spans and insert a break element, but a &nbsp; was being inserted instead.

The code responsible:

var c = text[i];
var s = styles[i]

if (s == 0) {
    //Is whitespace of some sort
    if (s == " ") {
        sb.push("&nbsp;");
    }
    else if (c == "\t") {
        sb.push("&nbsp;&nbsp;&nbsp;&nbsp;"); //tab
    }
    else if (c == "\r") {
        continue;
    }
    else {
        // \n
        if (innerSpanOpen == true) {
            sb.push("</span>"); //close inner span
            innerSpanOpen = false;
        }
sb.push("</span>"); //close line newRow = true; } }

You're (as in me) doing it wrong

The ultimate root of the error was me using undescriptive variable names, as it made confusion far too easy, so c has now been refactored to character and s to style. But my lack of understanding of Javascript also played a key part. The error stems from this line in particular:

if (s == " ") {

This is a typo/logical error. It was 4am in the morning when I was writing this, so probably the latter. I of course, wanted to check if the character was a space, not the style, which is an integer, but the reason that it was always returning true is an interesting one for me, because it was always returning true when s/style was 0, when compared to a string consisting of a space.

Dynamic types, weakly held

So, it turns out that in Javascript 0 == " ". This confused me, as I couldn't see how that worked (did I mention my background is 99% strongly typed languages?), so after some digging through the Mozilla Javascript reference I came across this:

When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal.

The Javascript reference shed no further light on how a mathematical value was derived, so I downloaded ECMA-262, the ECMAScript spec, and presto:

The conversion of a string to a number value is similar overall to the determination of the number value for a numeric literal (see 7.8.3), but some of the details are different, so the process for converting a string numeric literal to a value of Number type is given here in full. This value is determined in two steps: first, a mathematical value (MV) is derived from the string numeric literal; second, this mathematical value is rounded as described below.

The MV of StringNumericLiteral ::: [empty] is 0. The MV of StringNumericLiteral ::: StrWhiteSpace is 0.

So there we go. 0 == " " in Javascript because it does. On the plus side, I hadn't even realised that Javascript had a === operator, so I'll be using that a bit more often.

It's worth noting that this code would've caused mysterious errors in most type systems. In a strongly typed language like Python, the error would have manifested as a lack of generation of non-breaking spaces, as the condition would've always evaluated to False.

Published: 30th June, 2008