- Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathExample
More file actions
Latest commit
66 lines (44 loc) · 1.6 KB
/
Copy pathExample
File metadata and controls
66 lines (44 loc) · 1.6 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env python
# python-cdb 0.32 "Example"
importcdb
printcdb.__version__
## cdbmake object
fn="f.cdb"
maker=cdb.cdbmake(fn, fn+".tmp");
maker.add('flim', 'flam') # +4,4:flim->flam
maker.add('map', 'hash') # +3,4:map->hash
maker.add('spam', 'eggs') # +4,4:spam->eggs
maker.add('map', 'dictionary') # +3,10:map->dictionary
maker.add('map', 'assoc. array') # +3,12:map->assoc. array
print"Added %d records to CDB %s (fd %d)"% \
(maker.numentries, maker.fn, maker.fd)
maker.finish()
del(maker)
## cdb object
c=cdb.init(fn) # or init( file_obj.fileno() )
printlen(c) # 5
printc['flim'] # 'flam'
printc.get('spam') # 'eggs'
printc.get('map', 0) # 'hash' -- a la cdbget utility
printc.get('map', 1) # 'dictionary'
r=c.get('map') # will print:
whilerisnotNone: # 'hash'
printr# 'dictionary'
r=c.getnext() # 'assoc. array'
printc.keys() # ['flim', 'map', 'spam']
forkinc.keys(): # flim => ['flam']
printk, '=>', c.getall(k) # map => ['hash', 'dictionary', 'assoc. array']
# spam => ['eggs']
k=c.firstkey() # same as above
whilekisnotNone:
printk, "=>", c.getall(k)
k=c.nextkey()
defcdbdump(cdb_o): # as the cdbdump utility
r=cdb_o.each() # r = ('key', 'val') or None
whiler:
print"+%d,%d:%s->%s"% (
len(r[0]), len(r[1]),
r[0], r[1] )
r=cdb_o.each()
print
cdbdump(c)