I saw this code on stack overflow

def writeProfile(profileName, profileData):
    """ pickle user profile data """

    os.chdir(PROFILE_DIR)

    fileName = profileName + ".dat"
    f = open(fileName, "wb")
    pickle.dump(profileData, f)
    f.close()

    os.chdir("..")

It is for saving progress under the pickle module. I don't see a way to save variables. How would I do that?

Well, pickle.dump() should have no problem saving a variable, as in pickle.dump(variable,f). Now if pickle doesn't like that it will raise an exception, but I suspect if you just try it will work fine...based on pickle.dumps() working on a test case I just tried.

You will probably find your real problem comes later, when tryng to unpickle. But that's later...

You can pickle your code's local or global dictionary, this will save the name and value of each variable.

If you follow Lardmeister's advice, make sure your variable names are rather unique.

I just stumbled into this thread and, not being a python programmer, found it quite aumusing ... pickle, unpickle, huh? Can you cucumber.dump() too?

No, Dani, cucumber is for Ruby. :)

Python has Lettuce. But cucumber claims to work with python too.

@dani
pickle is for turning objects*
into text,to write to a file so you
can save variables

*Most objects, including user-defined, If not, you can customize to do it.
It can even do recusive lists!

So it's like php's var_export() or serialize() then I guess.

My preference would be Python module shelve, which creates a 'persistent to file' dictionary. Dictionary objects are highly optimized in Python.

My preference would be Python module shelve, which creates a 'persistent to file' dictionary.

I don't like shelve. I think it is very slow. Did you try cerealizer ? (in linux mint, install it with the package manager!)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.