[Tutor] Persistent Dictionary Variable

Jeff Shannon jeff@ccvcorp.com
Fri Jun 27 14:11:08 2003


Jacob Abraham wrote:

>Dear Tutors,
>
>   I was wondering if it were possible to create a
>dictionary variable look alike string into dictionary
>variable using Python. 
>

If I understand what you're looking for, the answer is the shelve 
module.  This puts a dictionary interface on persistent (file-based) 
backend storage.  When you load it, it's (almost) exactly like using a 
dictionary, but it can be conveniently saved to disk for later usage by 
a later invocation of the same (or other, related) program.

 >>> import shelve
 >>> help(shelve)
Help on module shelve:

NAME
    shelve - Manage shelves of pickled objects.

FILE
    c:\python22\lib\shelve.py

DESCRIPTION
    A "shelf" is a persistent, dictionary-like object.  The difference
    with dbm databases is that the values (not the keys!) in a shelf can
    be essentially arbitrary Python objects -- anything that the "pickle"
    module can handle.  This includes most class instances, recursive data
    types, and objects containing lots of shared sub-objects.  The keys
    are ordinary strings.

    [...]

Jeff Shannon
Technician/Programmer
Credit International