Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Guide] Object Serialization in Python
#1
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.
[Completely Honest. Seriously.]

Protip: Anonymous Reputation Points are for Pussies
Reply
#2
Nice guide you got here. Thanks for the post. Keep it up! =)
[Image: inm4jO.png]
[ AIM: YoRelapse | Gamertag: N/A | MSN: YoRelapse@live.com ]
Reply
#3
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.
Reply
#4
This looks quite nice, I was thinking about starting Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Help Dεlluzion 3 1,749 09-30-2019, 12:59 AM
Last Post: samsmith001
  TypeError: attribute of type 'NoneType' is not callable object chris0147 0 961 09-05-2014, 07:42 PM
Last Post: chris0147
  'X' object has no attribute 'y' band_master 1 1,094 04-23-2012, 09:54 PM
Last Post: band_master
  Simple Python Python Compiler Canoris 21 8,337 06-01-2011, 06:30 PM
Last Post: Filefinder
  Python 2 vs 3? Jake 8 2,216 12-11-2010, 04:13 PM
Last Post: Bursihido

Forum Jump:


Users browsing this thread: 1 Guest(s)