1

In layman's terms, what is "Serialization", and why do I need it? I read the Wikipedia entry, but still don't understand it. Why do I need to convert data into a sequence of bits to store it in a file? I am specifically concerned with using Python's pickle module to do serialization.

Thanks for the time!

3 Answers 3

2

You can save the state of a programm (of specific objects). Imagine you have a program which runs for many hours or even days. Using pickle you can save the state of the calculation, kill the programm and resume the calculation later if you want to.

You could even email the saved objects to other people, who than can resume the calculation or view your results.

I sometimes pickle userpreferences or (in a quiz) what questions where asked the last time and what answers were given.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you define "State of the Programm". What does State mean? @your example (which I very much appreciate): You mean e.g. a Loop that runs e.g. 1.000.000 and I can stop at 500.000 and than resume? Is that it?
You can safe objects, you could in your example safe the counter. Generators are not pickable. But if you wanted to approximate a zero-point of a function using Newton-Approximation, you could save your newest result and later load it again to do another thousand cycles.
1

Let me try to explain using some examples...

You need to pass a dictionary to some other python process that runs out of your python environment (maybe some other project or on some other machine)...

somelist = {1:1,2:2,3:3}

How can you pass this dictionary to that process? You cannot convert it to string, even if you did, you cannot convert it back to its original form...

If you pickle this dictionary it will give you

dumps({1: 1, 2: 2, 3: 3})
'(dp1\nI1\nI1\nsI2\nI2\nsI3\nI3\ns.'

which has a string-like structure... So you can send this via post, or something else... and the receiver can unpickle it to obtain the original object...

loads('(dp1\nI1\nI1\nsI2\nI2\nsI3\nI3\ns.')
{1: 1, 2: 2, 3: 3}

1 Comment

For that task json, yaml or xml could possibly be a better alternative: being not python-specific they are readable with any modern computer language. json/yaml has an advantage of better readability by humans and xml is probably more suitable for complicated structures. json and xml libs ship with python standard distribution.
0

A program that produces some statistics, but not too much of it so that using DB is overkill.

For example, benchmarking a program to choose the best algorithm.

Upon completion it draws a graph. Now you might not like the way the graph is drawn. You pickle the results, then unpickle in another script (perhaps after a couple of subsequent benchmark runs) and fine-tune the visualization as you wish.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.