python: incrementing values in a tokyo cabinet store

153 views Asked by At

I am using tcdb to hold a large key-value store. The keys are strings representing user IDs, the values are dicts of the form

{'coord':0,'node':0,'way':0,'relation':0}

The store is filled iterating over a data file that has coord, node, way and relation objects, each linked to a specific user. Here's my code for incrementing the fields:

def increment(self,uid,typ):
    uid = str(uid)
    type = str(typ)
    try:
        self.cache[uid][typ] += 1
    except KeyError:
        try:
            self.cache[uid][typ] = 1
        except KeyError:
            try:
                print 'creating record for %s' % uid
                self.cache[uid] = {'coord':0,'node':0,'way':0,'relation':0}
            except KeyError:
                print 'something\'s messed up'

This does not work. I end up with a table that has all zero values:

def result(self):
    print 'cache is now %i records' % len(self.cache)
    for key in self.cache:
        print key + ': ' + str(self.cache[key])

yields:

...
4951: {'node': 0, 'coord': 0, 'relation': 0, 'way': 0}
409553: {'node': 0, 'coord': 0, 'relation': 0, 'way': 0}
92274: {'node': 0, 'coord': 0, 'relation': 0, 'way': 0}
259040: {'node': 0, 'coord': 0, 'relation': 0, 'way': 0}
...

Why?

The last exception is never called.

EDIT This code in the first try block:

        tempdict = self.cache[uid]
        tempdict[typ] = tempdict.get(typ,0) + 1
        self.cache[uid] = tempdict

instead of the original

        self.cache[uid][typ] += 1

works, but looks really ugly to me.

1

There are 1 answers

2
Mark Byers On

After this line:

self.cache[uid] = {'coord':0,'node':0,'way':0,'relation':0}

Add this:

self.cache[uid][type] = 1

Also, please don't use type as a variable name as it hides the built-in of the same name.