classification
Title: Add repr to shelve.Shelf
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Claudiu.Popa, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-06-05 17:26 by Claudiu.Popa, last changed 2014-06-24 20:07 by berker.peksag. This issue is now closed.

Files
File name Uploaded Description Edit
shelve.patch Claudiu.Popa, 2014-06-05 17:26 review
Messages (7)
msg219827 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-05 17:26
Hello!

Working with Shelf instances in the interactive console is cumbersome because you can't have an instant feedback when running the following:

>>> from shelve import Shelf
>>> s = Shelf({})
>>> s['a'] = 1
>>> s
<shelve.Shelf object at 0x033D0AF0>

This patch adds an useful repr to Shelf objects.

>>> s
Shelf({'a': 1})

Thanks!
msg221340 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-06-23 04:44
This may not be the right repr for the usual way of creating shelves:

    d = shelve.open('tmp.shl')
    print(repr(d))             # I would expect the repr to show 
                               # the underlying db instead of all the
                               # key/value pairs which can be passed
                               # in the constructors
msg221342 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-23 05:12
The problem is that the repr of the underlying db is not very helpful either, as seen for the dbm backend.

>>> import dbm
>>> dbm.open("test", "c")
<dbm.dumb._Database object at 0x03091FC0>
>>> f=_
>>> f[b"2"] = b"a"
>>> f
<dbm.dumb._Database object at 0x03091FC0>
>>>

But it shows the content of the underlying database, not the key / value pairs passed in the constructor:

>>> shelve.open("test1")
DbfilenameShelf({})
>>> f=_
>>> f["2"] = "4"
>>> f
DbfilenameShelf({'2': '4'})
>>> f["40"] = "50"
>>> f.dict
<dbm.dumb._Database object at 0x02ACC038>
>>> f.dict.keys()
[b'40', b'2']
>>>
msg221450 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-06-24 11:41
I don't think this is right repr for shelve. As file's repr doesn't read and expose a content of a file, shelve's repr shouldn't read and expose all database content.
msg221451 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-24 11:49
Fair point, Serhiy. But I see the shelve more similar to a persistent, dictionary-like object, than to a file. The fact that it uses some database behind is just an implementation detail.
msg221454 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-06-24 12:23
When shelve stores its data on a disk, it is more similar to a file. After all, it can contain gigabytes of data, much larger than Python can address in RAM.

I you want more readable repr, I with Raymond, -- use the repr of the underlying db and add readable repr for dbm objects (including file name and open mode).
msg221455 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-24 12:37
Alright, I agree with you now. You can close the issue if you want.
History
Date User Action Args
2014-06-24 20:07:50berker.peksagsetstage: patch review -> resolved
2014-06-24 20:06:31rhettingersetstatus: open -> closed
resolution: wont fix
2014-06-24 12:37:54Claudiu.Popasetmessages: + msg221455
2014-06-24 12:23:58serhiy.storchakasetmessages: + msg221454
2014-06-24 11:49:45Claudiu.Popasetmessages: + msg221451
2014-06-24 11:41:49serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg221450
2014-06-23 05:12:00Claudiu.Popasetmessages: + msg221342
2014-06-23 04:44:12rhettingersetmessages: + msg221340
2014-06-23 04:23:13rhettingersetassignee: rhettinger

nosy: + rhettinger
stage: patch review
2014-06-05 17:26:57Claudiu.Popacreate