classification
Title: dbm documentation example doesn't work (iteritems())
Type: Stage: needs patch
Components: Documentation Versions: Python 3.1, Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.araujo Nosy List: docs@python, eric.araujo, georg.brandl, sandro.tosi
Priority: normal Keywords:

Created on 2010-12-02 20:52 by sandro.tosi, last changed 2010-12-28 11:53 by georg.brandl. This issue is now closed.

Messages (7)
msg123113 - (view) Author: Sandro Tosi (sandro.tosi) * (Python committer) Date: 2010-12-02 20:52
Following http://mail.python.org/pipermail/docs/2010-December/002356.html a possible solution is:


diff -r 3b07f7bb0289 Doc/library/dbm.rst
--- a/Doc/library/dbm.rst       Thu Dec 02 19:29:18 2010 +0100
+++ b/Doc/library/dbm.rst       Thu Dec 02 21:51:06 2010 +0100
@@ -88,7 +88,7 @@
 
    # Loop through contents.  Other dictionary methods
    # such as .keys(), .values() also work.
-   for k, v in db.iteritems():
+   for k, v in dict(db).items():
        print(k, '\t', v)
 
    # Storing a non-string key or value will raise an exception (most

How much ugly is this? alternatively, we can:


>>> for k in db.keys():
...     print(k, '\t', db.get(k))

What would be the best solution? (anyhow the comments above the for loop has to be changed).

Regards,
Sandro
msg123148 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-03 00:16
Isn’t s/iteritems/items/ enough?
msg123191 - (view) Author: Sandro Tosi (sandro.tosi) * (Python committer) Date: 2010-12-03 06:07
Hi Eric,
on and up-to-date py3k I got this:


>>> for k, v in db.items():
...     print(k, '\t', v)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_dbm.dbm' object has no attribute 'items'
>>> 'items' in dir(db)
False

(I tried to use items() before proposing that code ;)).
msg123356 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-04 15:16
Arg, the internal classes returned by dbm.*.open have keys but not necessarily items.  See #9523, #6045 and #5736.

The docs should be fixed independently of that, with the less non-idiomatic code that we can find.  Do you want to check the dbm docs for other similar broken examples?  I’ll review the patch.
msg124101 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-16 01:28
IMO, wrapping db in a dict defeats the purpose of dbm implementing a mapping interface.  I would use the most natural mapping idioms:

-   for k, v in db.iteritems():
-       print(k, '\t', v)
+   for k in db:
+       print(k, '\t', db[k])

The downside of this example is that it does not explicitely call methods, making the comment about “Other dictionary methods” strange.
msg124218 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-12-17 15:53
Why not replace it with an example that uses get() or setdefault() then?
msg124788 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-12-28 11:53
Fixed in r87536.
History
Date User Action Args
2010-12-28 11:53:33georg.brandlsetstatus: open -> closed

messages: + msg124788
resolution: fixed
nosy: georg.brandl, eric.araujo, sandro.tosi, docs@python
2010-12-17 15:53:30georg.brandlsetnosy: + georg.brandl
messages: + msg124218
2010-12-16 01:28:35eric.araujosetnosy: eric.araujo, sandro.tosi, docs@python
messages: + msg124101
2010-12-04 15:16:12eric.araujosetassignee: docs@python -> eric.araujo
messages: + msg123356
2010-12-03 06:07:12sandro.tosisetmessages: + msg123191
2010-12-03 00:18:27eric.araujosetversions: + Python 3.1, Python 3.2
nosy: + docs@python

assignee: docs@python
components: + Documentation
stage: needs patch
2010-12-03 00:16:48eric.araujosetnosy: + eric.araujo
messages: + msg123148
2010-12-02 20:52:01sandro.tosicreate