Support Forums
[Guide] Object Serialization in Python - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Python Programming Language (https://www.supportforums.net/forumdisplay.php?fid=32)
+---- Thread: [Guide] Object Serialization in Python (/showthread.php?tid=20179)



[Guide] Object Serialization in Python - manipulate - 07-03-2011

Object Serialization and Deserialization in Python
by Kye Russell - From Completely Honest

This piece was originally published on my blog, Completely Honest. Please feel free to check it out, as there's plenty more where this came from...

Object serialisation and deserialisation refers to converting objects into ‘flat’ bitstreams and turning them back in to fully functional objects, respectively. The main/only use for this is a pathway to allow you to dump Python objects to files, and open them later. If you still aren’t seeing it, serialisation/deserialisation lets you create objects and have them be persistent, instead of dying every time you close your Python App.

Python comes with a module to do this, and it’s called Pickle. It sounds stupid at first, but it’s rather appropriate, because we’re practically ‘pickling’ objects for later use.

Lets jump into some code, shall we?

PHP Code:
import pickle
import random

# Opening a file handler.
fh      open('obj.txt''w'

# Create a new object.
eggs = {'green eggs''ham',
        
'sam'       'i am'}

# Lets see what's there...
print(eggs)

# Creating a new Pickler.
pickler pickle.Pickler(fh)

# Dump eggs object to file.
pickler.dump(eggs

So, as you can see, it isn’t all that hard. We’ve essentially created a dummy object (we’ve used a dictionary here, but you can use any Python object), as well as a file-handler object and an instance of the Pickler class. The file handler object is used to write the serialised (or ‘pickled’) object to the disk, and the Pickler object is the meat of the script, as it actually ‘translates’ the target object to a flat bitstream.

In the end, we end up with a serialised version of the ‘eggs’ object stored inside of obj.txt. You can check out the contents of obj.txt and — depending on your version of python — you may or nay mot be able to actually see the human-readable contents of the object in the text file.

“Oh cool, you’ve written a list to a file…what’s fancy about that?”. Well, this is where the cool stuff comes in.

PHP Code:
import pickle

# Opening our object dumpfile.
fh   open('obj.txt''r')

# Creating the unpickler object.
unpk pickle.Unpickler(fh)

# Re-load the eggs object from our dumpfile.
eggs unpk.load()

# Here's our object, safe and sound.
print(eggs

Okay, so this is a completely different Python script, and has no attachment to the first script in any way, shape, or form, except for the fact that this script will pull the contents of obj.txt. As you can see, we simply open a file-handler object, as well as a Depickler object. The Depickler object will essentially do the reverse of what the Picker object did, a.k.a. it’ll grab a bistream representation of pickled object and instantize it in the current instance of the Python interpreter.

We simply pass the Depickler object our file-handler object as part of our instantisation, and write whatever Depickler.load() returns to a variable. If you still don’t see it, Depickler.load() will dive in to obj.txt and grab our ‘eggs’ object, and instantise it in the current instance.

This means that, even after closing the Python interpreter, you can keep your important objects.

Serialisation is a very powerful tool, and is used in a wide range of scripts.

All work on Completely Honest — unless stated otherwise — is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. You may copy and share this work for non-commercial purposes, and you must attribute Completely Honest and Kye Russell alongside the content. Failure to comply to the terms of this license is punishable by international copyright law.



RE: [Guide] Object Serialization in Python - Relapse - 07-13-2011

Nice guide you got here. Thanks for the post. Keep it up! =)


RE: [Guide] Object Serialization in Python - Caffeine - 08-12-2011

By dumping objects into files to be read later, would this cause the final application to run with better performance, or slower performance?

I'm quite new to Python still.


RE: [Guide] Object Serialization in Python - BreShiE - 08-12-2011

This looks quite nice, I was thinking about starting Python.