2011-12-03 2 views
0

큰 키 - 값 저장소를 보유하려면 tcdb을 사용하고 있습니다. 키가 사용자 ID를 나타내는 문자열, 숫자가 형태 매장 COORD 노드, 방법 및 관계 객체를 갖는 데이터 파일에 걸쳐 순회를 충전python : tokyo cabinet store의 값을 늘리십시오.

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

특정 사용자에 연결된 각각의 dicts이다. 다음은 필드를 증가시키기위한 코드입니다.

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' 

이것은 작동하지 않습니다.

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

수율 :

... 
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} 
... 

이유는 모두 0 값이있는 테이블로 끝?

마지막 예외가 호출되지 않습니다.

편집try 블록이 코드 : 대신 원래

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

작품의

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

하지만, 나에게 정말 추한 보인다. 이 선 후

+0

'coord', 'node', 'way'또는 'relation'중 하나입니다. – mvexel

+0

테이블을 출력하는 코드를 삽입했습니다. – mvexel

+0

죄송합니다. 다른 버전에서 더 많은 키가 도입되었습니다. 수정 됨. – mvexel

답변

2

:

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

이 추가 : 그것은 같은 이름의 내장을 숨 깁니다으로

self.cache[uid][type] = 1 

또한, 변수 이름으로 type을 사용하지 마십시오.

+0

'self.cache = {}'는 작동하지만'self.cache = tdb.TDB()'가 아닌 경우 작동합니다. 그래서 tcdb에 고유 한 것이 있습니다. – mvexel

+0

또한, 나는 변수의 이름을 'typ'로 바꿨다. 그 점에 대해 감사한다. – mvexel

관련 문제