Mercurial > cpython-lru_cache-weakref
annotate Demo/classes/Dbm.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 01a7e66525c2 |
| children |
| rev | line source |
|---|---|
| 945 | 1 # A wrapper around the (optional) built-in class dbm, supporting keys |
| 2 # and values of almost any type instead of just string. | |
| 3 # (Actually, this works only for keys and values that can be read back | |
| 4 # correctly after being converted to a string.) | |
| 5 | |
| 6 | |
| 7 class Dbm: | |
| 8 | |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
9 def __init__(self, filename, mode, perm): |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
10 import dbm |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
11 self.db = dbm.open(filename, mode, perm) |
| 945 | 12 |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
13 def __repr__(self): |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
14 s = '' |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
15 for key in self.keys(): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28788
diff
changeset
|
16 t = repr(key) + ': ' + repr(self[key]) |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
17 if s: t = ', ' + t |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
18 s = s + t |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
19 return '{' + s + '}' |
| 945 | 20 |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
21 def __len__(self): |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
22 return len(self.db) |
| 945 | 23 |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
24 def __getitem__(self, key): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28788
diff
changeset
|
25 return eval(self.db[repr(key)]) |
| 945 | 26 |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
27 def __setitem__(self, key, value): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28788
diff
changeset
|
28 self.db[repr(key)] = repr(value) |
| 945 | 29 |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
30 def __delitem__(self, key): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28788
diff
changeset
|
31 del self.db[repr(key)] |
| 945 | 32 |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
33 def keys(self): |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
34 res = [] |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
35 for key in self.db.keys(): |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
36 res.append(eval(key)) |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
37 return res |
| 945 | 38 |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
39 def has_key(self, key): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28788
diff
changeset
|
40 return self.db.has_key(repr(key)) |
| 945 | 41 |
| 42 | |
| 43 def test(): | |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
44 d = Dbm('@dbm', 'rw', 0600) |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
45 print d |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
46 while 1: |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
47 try: |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
48 key = input('key: ') |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
49 if d.has_key(key): |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
50 value = d[key] |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
51 print 'currently:', value |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
52 value = input('value: ') |
|
46197
01a7e66525c2
#2503 make singletons compared with "is" not == or !=
Benjamin Peterson <benjamin@python.org>
parents:
31515
diff
changeset
|
53 if value is None: |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
54 del d[key] |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
55 else: |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
56 d[key] = value |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
57 except KeyboardInterrupt: |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
58 print '' |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
59 print d |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
60 except EOFError: |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
61 print '[eof]' |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
62 break |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
1427
diff
changeset
|
63 print d |
| 945 | 64 |
| 65 | |
| 66 test() |
