[Tutor] cannot save changes to dictionary in a shelve file
David Lawrence
David Lawrence" <dlphone@bellsouth.net
Wed, 16 Oct 2002 16:43:12 -0400
This is a multi-part message in MIME format.
------=_NextPart_000_0014_01C27533.1E1C0590
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Please help - if you'd like to skip my long intro, see my IDLE session =
capture at the end of this message. At the end of the session, you can =
see that although I added '4:"proj004"' to the end of the dictionary, =
the dictionary doesn't contain that pair after I close & open the shelve =
file. Why?
I am learning Python pretty much as my first programming language. I'm a =
working technical writer who is seeking to expand my abilities. Years =
ago I attempted to learn C++ on my own using "Teach Yourself ..." books, =
but I became a bit overwhelmed when it came to pointers (probably should =
have sought out a programmer for some help!!) and stopped.=20
I am very pleased with Python so far ... the syntax is concise, the =
documentation is plentiful and thorough, and it's Open Source. But, I'm =
having a problem with the "shelve" module. As a learning project I am =
attempting to put together a simple one-user database of projects in =
Python code. I'd like to make it object-oriented - I've written some =
code w/a "Project" class (not included) to create project objects with - =
but I'd settle for a simple searchable collection of dictionaries & =
lists if I could just figure out the persistence thing.=20
Excuse the long code example. Oviously, I created a shelve database =
file, and created a new dictionary assigned to the "projects" key. But I =
can't update the dictionary and make the shelve file retain it! At the =
end of the example, I added the key:value pair '4:"proj004" to the end =
of the dictionary, but the dictionary doesn't contain that pair after I =
close & open the shelve file.
What am I doing wrong?=20
I've also tried this with a simple list of integers instead of a =
dictionary, and it does the same thing.
Thanks for your help,
Dave
-------------------------session snapshot =
------------------------------------
Python 2.2.1 (#34, Apr 9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> import shelve
>>> db =3D shelve.open("projectsdb","c")
>>> db.keys()
[]
>>> db["projects"] =3D {1:"proj001",2:"proj002",3:"proj003"}
>>> db["otherlist"] =3D (5,7,9,10)
>>> db.close()
>>> db.keys()
Traceback (most recent call last):
File "<pyshell#6>", line 1, in ?
db.keys()
File "C:\Program Files\Python22\lib\shelve.py", line 56, in keys
return self.dict.keys()
AttributeError: 'int' object has no attribute 'keys'
>>> db =3D shelve.open("projectsdb","c")
>>> db.keys()
['projects', 'otherlist']
>>> projects =3D db["projects"]
>>> projects
{1: 'proj001', 2: 'proj002', 3: 'proj003'}
>>> projects[4] =3D "proj004"
>>> projects
{1: 'proj001', 2: 'proj002', 3: 'proj003', 4: 'proj004'}
>>> db.close()
>>> db =3D shelve.open("projectsdb","c")
>>> db.keys()
['projects', 'otherlist']
>>> projects =3D db["projects"]
>>> projects
{1: 'proj001', 2: 'proj002', 3: 'proj003'}
>>>=20
------=_NextPart_000_0014_01C27533.1E1C0590
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4915.500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2><FONT size=3D2>
<P>Please help - if you'd like to skip my long intro, see my =
IDLE=20
session capture at the end of this message. At the end of the =
session, you=20
can see that although I added '4:"proj004"' to the end of the =
dictionary, the=20
dictionary doesn't contain that pair after I close & open the shelve =
file.=20
Why?</P>
<P>I am learning Python pretty much as my first programming language. =
I'm a=20
working technical writer who is seeking to expand my abilities. Years =
ago I=20
attempted to learn C++ on my own using "Teach Yourself ..." books, but I =
became=20
a bit overwhelmed when it came to pointers (probably should have sought =
out a=20
programmer for some help!!) and stopped. </P>
<P>I am very pleased with Python so far ... the syntax is concise, the=20
documentation is plentiful and thorough, and it's Open Source. But, I'm =
having a=20
problem with the "shelve" module. As a learning project I am attempting =
to put=20
together a simple one-user database of projects in Python code. I'd like =
to make=20
it object-oriented - I've written some code w/a "Project" class (not =
included)=20
to create project objects with - but I'd settle for a simple searchable=20
collection of dictionaries & lists if I could just figure out the=20
persistence thing. </P>
<P>Excuse the long code example. Oviously, I created a shelve database =
file, and=20
created a new dictionary assigned to the "projects" key. But I can't =
update the=20
dictionary and make the shelve file retain it! At the end of the =
example, I=20
added the key:value pair '4:"proj004" to the end of the dictionary, but =
the=20
dictionary doesn't contain that pair after I close & open the shelve =
file.</P>
<P>What am I doing wrong? </P>
<P>I've also tried this with a simple list of integers instead of a =
dictionary,=20
and it does the same thing.</P>
<P>Thanks for your help,</P>
<P>Dave</P>
<P>-------------------------session snapshot=20
------------------------------------</P>
<P>Python 2.2.1 (#34, Apr 9 2002, 19:34:33) [MSC 32 bit (Intel)] on =
win32</P>
<P>Type "copyright", "credits" or "license" for more information.</P>
<P>IDLE 0.8 -- press F1 for help</P>
<P>>>> import shelve</P>
<P>>>> db =3D shelve.open("projectsdb","c")</P>
<P>>>> db.keys()</P>
<P>[]</P>
<P>>>> db["projects"] =3D =
{1:"proj001",2:"proj002",3:"proj003"}</P>
<P>>>> db["otherlist"] =3D (5,7,9,10)</P>
<P>>>> db.close()</P>
<P>>>> db.keys()</P>
<P>Traceback (most recent call last):</P>
<P>File "<pyshell#6>", line 1, in ?</P>
<P>db.keys()</P>
<P>File "C:\Program Files\Python22\lib\shelve.py", line 56, in keys</P>
<P>return self.dict.keys()</P>
<P>AttributeError: 'int' object has no attribute 'keys'</P>
<P>>>> db =3D shelve.open("projectsdb","c")</P>
<P>>>> db.keys()</P>
<P>['projects', 'otherlist']</P>
<P>>>> projects =3D db["projects"]</P>
<P>>>> projects</P>
<P>{1: 'proj001', 2: 'proj002', 3: 'proj003'}</P>
<P>>>> projects[4] =3D "proj004"</P>
<P>>>> projects</P>
<P>{1: 'proj001', 2: 'proj002', 3: 'proj003', 4: 'proj004'}</P>
<P>>>> db.close()</P>
<P>>>> db =3D shelve.open("projectsdb","c")</P>
<P>>>> db.keys()</P>
<P>['projects', 'otherlist']</P>
<P>>>> projects =3D db["projects"]</P>
<P>>>> projects</P>
<P>{1: 'proj001', 2: 'proj002', 3: 'proj003'}</P>
<P>>>> </P></FONT></FONT></DIV></BODY></HTML>
------=_NextPart_000_0014_01C27533.1E1C0590--