Fix bug and use __init__

This commit is contained in:
Guido van Rossum 1993-10-30 12:38:16 +00:00
parent 74b3f8a9e3
commit becec31f17

View file

@ -4,22 +4,17 @@
# correctly after being converted to a string.) # correctly after being converted to a string.)
def opendbm(filename, mode, perm):
return Dbm().init(filename, mode, perm)
class Dbm: class Dbm:
def init(self, filename, mode, perm): def __init__(self, filename, mode, perm):
import dbm import dbm
self.db = dbm.open(filename, mode, perm) self.db = dbm.open(filename, mode, perm)
return self
def __repr__(self): def __repr__(self):
s = '' s = ''
for key in self.keys(): for key in self.keys():
t = `key` + ': ' + `self[key]` t = `key` + ': ' + `self[key]`
if s: t = t + ', ' if s: t = ', ' + t
s = s + t s = s + t
return '{' + s + '}' return '{' + s + '}'
@ -46,7 +41,7 @@ class Dbm:
def test(): def test():
d = opendbm('@dbm', 'rw', 0666) d = Dbm('@dbm', 'rw', 0666)
print d print d
while 1: while 1:
try: try:
@ -54,7 +49,7 @@ def test():
if d.has_key(key): if d.has_key(key):
value = d[key] value = d[key]
print 'currently:', value print 'currently:', value
value = eval(raw_input('value: ')) value = input('value: ')
if value == None: if value == None:
del d[key] del d[key]
else: else: