File: kcdbex2.py

package info (click to toggle)
python-kyotocabinet 1.23-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 828 kB
  • sloc: cpp: 2,967; python: 1,241; javascript: 249; makefile: 85
file content (34 lines) | stat: -rw-r--r-- 1,047 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from kyotocabinet import *
import sys

# create the database object
db = DB()

# open the database
if not db.open("casket.kch", DB.OREADER):
    print("open error: " + str(db.error()), file=sys.stderr)

# define the visitor
class VisitorImpl(Visitor):
    # call back function for an existing record
    def visit_full(self, key, value):
        print("{}:{}".format(key.decode(), value.decode()))
        return self.NOP
    # call back function for an empty record space
    def visit_empty(self, key):
        print("{} is missing".format(key.decode()), file=sys.stderr)
        return self.NOP
visitor = VisitorImpl()

# retrieve a record with visitor
if not db.accept("foo", visitor, False) or \
        not db.accept("dummy", visitor, False):
    print("accept error: " + str(db.error()), file=sys.stderr)

# traverse records with visitor
if not db.iterate(visitor, False):
    print("iterate error: " + str(db.error()), file=sys.stderr)

# close the database
if not db.close():
    print("close error: " + str(db.error()), file=sys.stderr)