Resuming a program's execution after correcting error

hanumizzle hanumizzle at gmail.com
Tue Oct 3 20:08:42 EDT 2006


On 3 Oct 2006 16:58:17 -0700, MRAB <google at mrabarnett.plus.com> wrote:

> > I like your idea Matthew but I don't know how to pickle the many
> > variables in one file. Do I need to pickle each and every variable into
> > a seperate file?
> > var1,var2
> > pickle.dump(var1,f)
> > pickle.dump(var2,f2)
> >
> Using the 'pickle' module:
>
>     # To store:
>     f = open(file_path, "wb")
>     pickle.dump(var1, f)
>     pickle.dump(var2, f)
>     f.close()
>
>     # To load
>     f = open(file_path, "rb")
>     var1 = pickle.load(f)
>     var2 = pickle.load(f)
>     f.close()
>
> A more flexible alternative is to use the 'shelve' module. This behaves
> like a dict:
>
>     # To store
>     s = shelve.open(file_path)
>     s["var1"] = "first"
>     s["var2"] = [2, 3]
>     s.close()
>
>     # To load
>     s = shelve.open(file_path)
>     print s["var1"] # This prints "first"
>     print s["var2"] # This prints [2, 3]
>     s.close()

As long as we're on the subject of data serialization, I should like
to bring up PyYaml. YAML is a portable format for storing data of all
kinds; it became popular via Ruby I think, but there are
implementations for many other languages. If you stick to storing
simple stuff like lists, strings, and dictionaries, you can use your
YAML data almost anywhere, but PyYaml even supports reifying things
like lambdas.



More information about the Python-list mailing list