[Tutor] updating shelved record

Joseph Paish jpaish@freenet.edmonton.ab.ca
Sun Jul 6 19:54:02 2003


On Sunday 06 July 2003 17:29, you wrote:
> i am trying to update a file "in place" using shelve.
>
> this is what i have so far, using an example i found in a book to build=
 the
> original database in order to test ideas an my understanding of how she=
lve
> works :
>
>
> Python 2.0 (#1, Apr 11 2001, 19:18:08)
> [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'),
> ['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 co=
mmit
> 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 mus=
t be
> missing something very obvious.
>
> thanks
>
> joe
>
> ps.  i have stripped out a lot of the IDLE session (mainly errors), but
> nothing that impacts what i am trying to do.
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


never mind.

found the answer almost immediately after posting the question, though i=20
swear i looked for at least an hour before asking on this mailing list.

anyway, the solution :

item =3D dbase['brian']
item ['age'] =3D 44
dbase['brian'] =3D item

sorry for the wasted bandwidth.

joe