[Tutor] updating shelved record
Joseph Paish
jpaish@freenet.edmonton.ab.ca
Sun Jul 6 19:27:33 2003
i am trying to update a file "in place" using shelve. =20
this is what i have so far, using an example i found in a book to build t=
he=20
original database in order to test ideas an my understanding of how shelv=
e=20
works :
Python 2.0 (#1, Apr 11 2001, 19:18:08)=20
[GCC 2.96 20000731 (Linux-Mandrake 8.0 2.96-0.48mdk)] on linux-i386
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>> import shelve
>>> dbase =3D shelve.open("mydbase")
>>> object1 =3D ['The', 'bright', ('side', 'of'), ['life']]
>>> object2 =3D {'name': 'Brian', 'age': 33, 'motto': object1}
>>> dbase['brian'] =3D object2
>>> dbase.close()
>>> import shelve
>>> dbase =3D shelve.open("mydbase")
>>> dbase['brian']
{'age': 33, 'name': 'Brian', 'motto': ['The', 'bright', ('side', 'of'),=20
['life']]}
>>> dbase['brian']['age']
33
# okay so far
>>> dbase.close()
>>> dbase =3D shelve.open("mydbase")
>>> print dbase['brian']['age']
33
# try and change brian's age to 44
>>> dbase['brian']['age'] =3D '44'
>>> print dbase['brian']['age']
33
# no luck
# try closing and re-opening the file to force a flush and hopefully comm=
it=20
the change.
>>> dbase.close()
>>> dbase =3D shelve.open("mydbase")
>>> print dbase['brian']['age']
33
# brian still 33 years old.
i guess the question is "how can i change brian's age on disk?". i must =
be=20
missing something very obvious.
thanks
joe
ps. i have stripped out a lot of the IDLE session (mainly errors), but=20
nothing that impacts what i am trying to do.