Jesper Noehr

Pythonista, RESTafarian, Binary Poet & Proud Bucketeer

Archive for July, 2009

Dumpster kitten

with one comment

Kitten

I found this little guy yesterday. Me and Katie were driving home from my Greek lesson and we could hear something screaming (we had the windows down.)

I pull over, and we walk towards the sound. This was at 10pm, by the way. Just by a dumpster, we see this tiny kitten, screaming his eyes out. Well, that’s a bad analogy, seeing as he’s just a couple of days old and his eyes aren’t even open yet. In fact, one of them was infected, badly.

We take him home and clean up his eye a bit. He doesn’t like that at all, but it needs to be done. We try to feed him a bit with some milk, but we have no way of getting it into his tiny mouth, and he keeps trying to suck on our fingers. Poor thing.

We drive to the vet, but he’s closed. His cell number is on the door though, so we call it. He tells us to go by the 24-hour pharmacy and pick up a syringe. Strangely, they give it to us for free. Needle and everything. Fun.

We head back home, fill it with milk and try to feed him again. Much better this time. This little guy must’ve gulped down about 25ml of milk, and in between meals he also decided to pee on me. Good sign, I think. We put some blankets in a bucket and put him in Katie’s office downstairs. It’s dark there, and he fell asleep instantly. Poor guy must’ve been terrified and exhausted. God knows how long he’s been in that dumpster.

This morning he was still sound asleep in his blankets, with the eventual “mmmm blankets comfy” moves. We haven’t woken him up yet, but we’ll go to the vet this morning. He seems to be doing a lot better.

So, to the people who put him where we found him, and to anyone ever having dumped a litter of kittens in the trash: You need to be arrested and put in jail. You’re inhuman. Animal cruelty is one of the lowest points you can reach in life. If you have a female cat and you don’t want kittens, a) don’t let her go outside and/or b) have her fixed. You need to be responsible. If you *do* end up with kittens, you’re responsible for them, too. What you did was inexcusable and I hope for your sake, that we never meet.

What’s gonna happen to this little guy? I don’t know. Someone we know might want him. If they don’t, we’ll probably adopt him. We already have a cat, but he’s currently back in The Netherlands, getting his vaccinations sorted out. Maybe the two will get along, we’ll see.

One thing’s for sure: We’ll take care of him and make sure he’s happy for as long as we need to.

Written by jespern

July 18th, 2009 at 8:48 am

Posted in Uncategorized

Making Python’s string.Template useful

with 2 comments

You know how Python has string.Template? It’s kinda useful, as it allows you to do stuff like:

from string import Template
s = Template('$who likes $what')
print s.substitute(who='tim', what='kung pao')
'tim likes kung pao'

That’s neat. But more often than not, you may want to use nested dicts, so you can write something like ‘person.name’. string.Template won’t allow you to do this, but it’s pretty easy to get around:

class TraversingDict(dict):
    def __getitem__(self, item):
        if '.' in item:
            source, path = item.split('.', 1)
            return TraversingDict(self[source])[path]
        return super(TraversingDict, self).__getitem__(item)

class InterpolTemplate(string.Template):
    idpattern = r'[_a-z][_a-z0-9\.]*'

    def render(self, dct):
        return self.safe_substitute(TraversingDict(dct))

How does it work? It uses a custom class, which subclasses ‘dict’. It’ll behave just like a normal built-in dictionary, but we’ve overriden __getitem__ to look for periods in the key name. If one is found, it splits up the key, and instantiates itself recursively. This essentially means that you can nest to any level, like ‘person.information.personal.name.first_name’.

The ‘render’ method on InterpolTemplate is not really needed, but it turns your dict into a TraversingDict, so you don’t need to mess with those at all:

Here’s the unittest I use:

def run_template_test():
    tmpl = "repository: ${repo.name}, owner: ${repo.owner}, size: ${size}"
    t = InterpolTemplate(tmpl)
    d = { 'repo': { 'name': 'foo', 'owner': 'bar' }, 'size': 42 }
    r = t.render(d)

    assert r == 'repository: foo, owner: bar, size: 42', r

Neat, eh? Makes for a nice simple substitute when you don’t want to rely on <insert template library here>.

Written by jespern

July 15th, 2009 at 9:50 am

Posted in python