Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc6764c

Browse files
committed
added __doc__ strings etc.
1 parent 6de668f commit cc6764c

3 files changed

Lines changed: 169 additions & 46 deletions

File tree

Lib/anydbm.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
1-
"""A generic interface to all dbm clones."""
1+
"""A generic interface to all dbm clones.
2+
3+
Instead of
4+
5+
import dbm
6+
d = dbm.open(file, 'rw', 0666)
7+
8+
use
9+
10+
import anydbm
11+
d = anydbm.open(file)
12+
13+
The returned object is a dbm, gdbm or (on the Mac) dbmac object,
14+
dependent on availability of the modules (tested in this order).
15+
16+
It has the following interface (key and data are strings):
17+
18+
d[key] = data # store data at key (may override data at
19+
# existing key)
20+
data = d[key] # retrieve data at key (raise KeyError if no
21+
# such key)
22+
del d[key] # delete data stored at key (raises KeyError
23+
# if no such key)
24+
flag = d.has_key(key) # true if the key exists
25+
list = d.keys() # return a list of all existing keys (slow!)
26+
27+
Future versions may change the order in which implementations are
28+
tested for existence, add interfaces to other db-like implementations
29+
(e.g. BSD Hash), and (in the presence of multiple implementations)
30+
decide which module to use based upon the extension or contents of an
31+
existing database file.
32+
33+
The open function has an optional second argument. This can be set to
34+
'r' to open the database for reading only. Don't pas an explicit 'w'
35+
or 'rw' to open it for writing, as the different interfaces have
36+
different interpretation of their mode argument if it isn't 'r'.
37+
"""
238

339
try:
440
import dbm
5-
def open(file, mode = 'rw'):
6-
return dbm.open(file, mode, 0666)
41+
def open(filename, mode = 'rw'):
42+
return dbm.open(filename, mode, 0666)
743
except ImportError:
8-
import dbmac
9-
open = dbmac.open
44+
try:
45+
import gdbm
46+
def open(filename, mode = 'w'):
47+
return gdbm.open(filename, mode, 0666)
48+
except ImportError:
49+
import dbmac
50+
open = dbmac.open

Lib/copy.py

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,73 @@
1-
# Generic (shallow and deep) copying operations
2-
# =============================================
3-
#
4-
# The difference between shallow and deep copying is only relevant for
5-
# compound objects (objects that contain other objects, like lists or class
6-
# instances).
7-
#
8-
# - A shallow copy constructs a new compound object and then (to the extent
9-
# possible) inserts *the same objects* into in that the original contains.
10-
#
11-
# - A deep copy constructs a new compound object and then, recursively,
12-
# inserts *copies* into it of the objects found in the original.
13-
#
14-
# Two problems often exist with deep copy operations that don't exist with
15-
# shallow copy operations:
16-
#
17-
# (a) recursive objects (compound objects that, directly or indirectly,
18-
# contain a reference to themselves) may cause a recursive loop
19-
#
20-
# (b) because deep copy copies *everything* it may copy too much, e.g.
21-
# administrative data structures that should be shared even between copies
22-
#
23-
# Python's deep copy operation avoids these problems by:
24-
#
25-
# (a) keeping a table of objects already copied during the current copying pass
26-
#
27-
# (b) letting user-defined classes override the copying operation or the set
28-
# of components copied
29-
#
30-
# This version does not copy types like module, class, function, method,
31-
# nor stack trace, stack frame, nor file, socket, window, nor array,
32-
# nor any similar types.
1+
"""\
2+
Generic (shallow and deep) copying operations
3+
=============================================
334
5+
Interface summary:
6+
7+
import copy
8+
9+
x = copy.copy(y) # make a shallow copy of y
10+
x = copy.deepcopy(y) # make a deep copy of y
11+
12+
For module specific errors, copy.Error is raised.
13+
14+
The difference between shallow and deep copying is only relevant for
15+
compound objects (objects that contain other objects, like lists or
16+
class instances).
17+
18+
- A shallow copy constructs a new compound object and then (to the
19+
extent possible) inserts *the same objects* into in that the
20+
original contains.
21+
22+
- A deep copy constructs a new compound object and then, recursively,
23+
inserts *copies* into it of the objects found in the original.
24+
25+
Two problems often exist with deep copy operations that don't exist
26+
with shallow copy operations:
27+
28+
(a) recursive objects (compound objects that, directly or indirectly,
29+
contain a reference to themselves) may cause a recursive loop
30+
31+
(b) because deep copy copies *everything* it may copy too much, e.g.
32+
administrative data structures that should be shared even between
33+
copies
34+
35+
Python's deep copy operation avoids these problems by:
36+
37+
(a) keeping a table of objects already copied during the current
38+
copying pass
39+
40+
(b) letting user-defined classes override the copying operation or the
41+
set of components copied
42+
43+
This version does not copy types like module, class, function, method,
44+
nor stack trace, stack frame, nor file, socket, window, nor array, nor
45+
any similar types.
46+
47+
Classes can use the same interfaces to control copying that they use
48+
to control pickling: they can define methods called __getinitargs__(),
49+
__getstate__() and __setstate__(). See the __doc__ string of module
50+
"pickle" for information on these methods.
51+
"""
3452

3553
import types
3654

3755
Error = 'copy.Error'
3856

3957
def copy(x):
58+
"""Shallow copy operation on arbitrary Python objects.
59+
60+
See the module's __doc__ string for more info.
61+
"""
62+
4063
try:
4164
copierfunction = _copy_dispatch[type(x)]
4265
except KeyError:
4366
try:
4467
copier = x.__copy__
4568
except AttributeError:
46-
raise Error, "un(shallow)copyable object of type %s" % type(x)
69+
raise Error, \
70+
"un(shallow)copyable object of type %s" % type(x)
4771
y = copier()
4872
else:
4973
y = copierfunction(x)
@@ -100,6 +124,11 @@ def _copy_inst(x):
100124
del d
101125

102126
def deepcopy(x, memo = None):
127+
"""Deep copy operation on arbitrary Python objects.
128+
129+
See the module's __doc__ string for more info.
130+
"""
131+
103132
if memo is None:
104133
memo = {}
105134
d = id(x)
@@ -111,7 +140,8 @@ def deepcopy(x, memo = None):
111140
try:
112141
copier = x.__deepcopy__
113142
except AttributeError:
114-
raise Error, "un-deep-copyable object of type %s" % type(x)
143+
raise Error, \
144+
"un-deep-copyable object of type %s" % type(x)
115145
y = copier(memo)
116146
else:
117147
y = copierfunction(x, memo)

Lib/shelve.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
1-
"""Manage shelves of pickled objects."""
1+
"""Manage shelves of pickled objects.
2+
3+
A "shelf" is a persistent, dictionary-like object. The difference
4+
with dbm databases is that the values (not the keys!) in a shelf can
5+
be essentially arbitrary Python objects -- anything that the "pickle"
6+
module can handle. This includes most class instances, recursive data
7+
types, and objects containing lots of shared sub-objects. The keys
8+
are ordinary strings.
9+
10+
To summarize the interface (key is a string, data is an arbitrary
11+
object):
12+
13+
import shelve
14+
d = shelve.open(filename) # open, with (g)dbm filename
15+
16+
d[key] = data # store data at key (overwrites old data if
17+
# using an existing key)
18+
data = d[key] # retrieve data at key (raise KeyError if no
19+
# such key)
20+
del d[key] # delete data stored at key (raises KeyError
21+
# if no such key)
22+
flag = d.has_key(key) # true if the key exists
23+
list = d.keys() # a list of all existing keys (slow!)
24+
25+
d.close() # close it
26+
27+
Dependent on the implementation, closing a persistent dictionary may
28+
or may not be necessary to flush changes to disk.
29+
"""
230

331
import pickle
432
import StringIO
533

34+
635
class Shelf:
36+
"""Base class for shelf implementations.
37+
38+
This is initialized with a dictionary-like object.
39+
See the module's __doc__ string for an overview of the interface.
40+
"""
741

842
def __init__(self, dict):
943
self.dict = dict
@@ -18,7 +52,8 @@ def has_key(self, key):
1852
return self.dict.has_key(key)
1953

2054
def __getitem__(self, key):
21-
return pickle.Unpickler(StringIO.StringIO(self.dict[key])).load()
55+
f = StringIO.StringIO(self.dict[key])
56+
return pickle.Unpickler(f).load()
2257

2358
def __setitem__(self, key, value):
2459
f = StringIO.StringIO()
@@ -30,14 +65,31 @@ def __delitem__(self, key):
3065
del self.dict[key]
3166

3267
def close(self):
33-
self.db.close()
68+
if hasattr(self.db, 'close'):
69+
self.db.close()
3470
self.db = None
3571

72+
def __del__(self):
73+
self.close()
74+
75+
3676
class DbShelf(Shelf):
77+
"""Shelf implementation using the "anydbm" generic dbm interface.
78+
79+
This is initialized with the filename for the dbm database.
80+
See the module's __doc__ string for an overview of the interface.
81+
"""
3782

38-
def __init__(self, file):
83+
def __init__(self, filename):
3984
import anydbm
40-
Shelf.__init__(self, anydbm.open(file))
85+
Shelf.__init__(self, anydbm.open(filename))
4186

42-
def open(file):
43-
return DbShelf(file)
87+
88+
def open(filename):
89+
"""Open a persistent dictionary for reading and writing.
90+
91+
Argument is the filename for the dbm database.
92+
See the module's __doc__ string for an overview of the interface.
93+
"""
94+
95+
return DbShelf(filename)

0 commit comments

Comments
 (0)