February 23, 2011

This so easy. I feel dirty

I'm talking of course about Python's pickle. Has anyone seen this thing? It just serializes anything at all. Any sort of internal or proprietary information you could possibly want to save or transfer is handled easily by this component. It's especially helpful if you're not really sure what format you want to use, you don't want to have to write out a schema for a new format, and really all you care about is efficiency. Just pickle it.

Serializing is as simple as this:


pdata = ("3", 1, "hello")
file = open("dumpedData.pkl", "w")
pickle.dump(pdata, file)


And de-serializing:


file = open("dumpedData.pkl", "r")
pdata = pickle.load(file)
print pdata
>>> ("3", 1, "hello")



That's it. No dealing with schemas. No custom parsing routines. Just nice, simple data persistence.

Love it.

No comments: