librarybook-data-storage.pdf

(91 KB) Pobierz
Python Standard Library: Data Storage
10-1
Data Storage
"Unlike mainstream component programming, scripts usually do not
introduce new components but simply "wire" existing ones. Scripts can
be seen as introducing behavior but no new state. /.../ Of course, there
is nothing to stop a "scripting" language from introducing persistent
state — it then simply turns into a normal programming language"
Clemens Szyperski, in Component Software
Overview
Python comes with drivers for a number of very similar database managers, all modeled after Unix's
dbm
library. These databases behaves like ordinary dictionaries, with the exception that you can only
use strings for keys and values (the
shelve
module can handle any kind of value).
Python Standard Library
Copyright (c) 1999-2003 by Fredrik Lundh. All rights reserved.
Python Standard Library: Data Storage
10-2
The anydbm module
This module provides a unified interface to the simple database drivers supported by Python.
The first time it is imported, the
anydbm
module looks for a suitable database driver, testing for
dbhash, gdbm, dbm,
or
dumbdbm,
in that order. If no such module is found, it raises an
ImportError
exception.
The
open
function is used to open or create a database, using the chosen database handler.
Example: Using the anydbm module
# File:
anydbm-example-1.py
import anydbm
db = anydbm.open("database", "c")
db["1"] = "one"
db["2"] = "two"
db["3"] = "three"
db.close()
db = anydbm.open("database", "r")
for key in db.keys():
print repr(key), repr(db[key])
'2' 'two'
'3' 'three'
'1' 'one'
Python Standard Library: Data Storage
10-3
The whichdb module
This module can be used to figure out which database handler that was used for a given database file.
Example: Using the whichdb module
# File:
whichdb-example-1.py
import whichdb
filename = "database"
result = whichdb.whichdb(filename)
if result:
print "file created by", result
handler = __import__(result)
db = handler.open(filename, "r")
print db.keys()
else:
# cannot identify data base
if result is None:
print "cannot read database file", filename
else:
print "cannot identify database file", filename
db = None
This example used the
__import__
function to import a module with the given name.
Python Standard Library: Data Storage
10-4
The shelve module
This module uses the database handlers to implement persistent dictionaries. A shelve object uses
string keys, but the value can be of any data type, as long as it can be handled by the
pickle
module.
Example: Using the shelve module
# File:
shelve-example-1.py
import shelve
db = shelve.open("database", "c")
db["one"] = 1
db["two"] = 2
db["three"] = 3
db.close()
db = shelve.open("database", "r")
for key in db.keys():
print repr(key), repr(db[key])
'one' 1
'three' 3
'two' 2
The following example shows how to use the
shelve
module with a given database driver.
Example: Using the shelve module with a given database
# File:
shelve-example-3.py
import shelve
import gdbm
def gdbm_shelve(filename, flag="c"):
return shelve.Shelf(gdbm.open(filename, flag))
db = gdbm_shelve("dbfile")
Python Standard Library: Data Storage
10-5
The dbhash module
(Optional). This module provides a
dbm-compatible
interface to the
bsddb
database handler.
Example: Using the dbhash module
# File:
dbhash-example-1.py
import dbhash
db = dbhash.open("dbhash", "c")
db["one"] = "the foot"
db["two"] = "the shoulder"
db["three"] = "the other foot"
db["four"] = "the bridge of the nose"
db["five"] = "the naughty bits"
db["six"] = "just above the elbow"
db["seven"] = "two inches to the right of a very naughty bit indeed"
db["eight"] = "the kneecap"
db.close()
db = dbhash.open("dbhash", "r")
for key in db.keys():
print repr(key), repr(db[key])
Zgłoś jeśli naruszono regulamin