0

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.

mvexel
  • 1,093
  • 1
  • 10
  • 25

1 Answers1

2

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.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • That works if `self.cache = {}` but not if `self.cache = tdb.TDB()` so there is something tcdb-specific that I am not getting right. – mvexel Dec 03 '11 at 20:50
  • Also, I did change the name of the variable to `typ` in the question, thanks for that. – mvexel Dec 03 '11 at 21:07