s = shelve.open(blah), s.items() Attribute Error

Steve Purcell stephen_purcell at yahoo.com
Wed Jul 26 18:16:36 EDT 2000


Jeff Blaine wrote:
> It seems that shelf objects do not have an .items() method like
> normal dictionary objects.
> 
> Is this true?

You're right, according to the source.

> If so, it should probably be pointed out in the docs under 'Restrictions'
> for shelve.  And also, is there a reason?

Probably an omission on the part of the author, or it might be considered an
undesirable operation, since it is equivalent to slurping everything out of
the shelf file.

You can try something like:

  s = shelve.open(blah, 'c')
  i = map(s.__getitem__, s.keys())

to get the same effect, but that raw reference to '__getitem__' is bad form.

Best to stick with:

  s = shelve.open(blah, 'c')
  i = map(lamba s=s, k: s[k], s.keys())

or simply:

  s = shelve.open(blah, 'c')
  i = []
  for k in s.keys():
    i.append(s[k])


-Steve

-- 
Steve Purcell,  Technical Director, Inkontact
Get in touch at http://www.inkontact.com/
Get testing at http://pyunit.sourceforge.net/
"Life must be simple if I can do it" -- Me




More information about the Python-list mailing list