shelve, persistent dictionary

#!/usr/bin/env python

import shelve

FILE_NAME = "/tmp/testDB"

def main():
    #
    # flag
    #   'r' Open existing database for reading only (default)
    #   'w' Open existing database for reading and writing
    #   'c' Open database for reading and writing, creating it if it doesn't exist
    #   'n' Always create a new, empty database, open for reading and writing
    #
    # writeback
    #   By default modified objects are written only when assigned to the shelf. 
    #   If the optional writeback parameter is set to True, all entries accessed are also cached 
    #   in memory, and written back on sync() and close()
    #
    hsh = shelve.open( FILE_NAME, flag='c', writeback=False)
    hsh[ 'a'] = 'Hello World'
    hsh.close()

    hsh = shelve.open( FILE_NAME, flag='r')
    for k in list( hsh.keys()):
        print( "key: %s, value: %s" % ( k, hsh[ k]))
    
if __name__ == "__main__":
    main()