-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanydbm_example.py
More file actions
49 lines (33 loc) · 954 Bytes
/
anydbm_example.py
File metadata and controls
49 lines (33 loc) · 954 Bytes
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
#!/usr/bin/env python
"""
Example of how to save data using the anydbm package
"""
import anydbm
outfilename = "add_book_data.dbm"
# get the data from the py file
# csv format really only holds flat data well.
from add_book_data_flat import AddressBook
## note that dbm files are really only good for simple key-value storage
## so let's just do one record:
person = AddressBook[0]
# create a dbm file writing object
db = anydbm.open(outfilename, 'n')
#write the data:
for key, value in person.items():
db[key] = value
#close the file
db.close()
#### see if it can be re-loaded.
#
# open an existing dbm file
db = anydbm.open(outfilename, 'r')
#read the data:
person = {}
for key, value in db.items():
person[key] = value
#Check if they are the same
if person == AddressBook[0]:
print "db version is the same as the original"
### Storing multiple people:
## bulding up a key
# left as an exercise for the reader....