This series is a part of Club ShowMeDo. Click to learn why you should join our
club with a simple monthly subscription.
We teach Python and all the cool modules
Topics include beginner Python, GUIs, Web and Desktop Apps
Long, specially crafted video tutorials just for you
Club video tutorials extend our normal Free content
Save your time - we've done the research for you
Keep your skills up-to-date
Learn at your own pace, everything you need is shown to you
Over 12 hours of archived material are waiting for you in the club
 Ian Ozsvald
I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production company `ProCasts <http://procasts.co.uk>`_:
.. image:: http://procasts.co.uk/media/procasts_sma ...
Serialising for persistence with cPickle [ID:1353] (5/5)
 Ian Ozsvald
I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production ...
Our authors tell us that feedback from you is a big motivator. Please take a few moments to let them know what you think of their work.
(Showmedo is undergoing major changes. To
report any problems viewing the videos please
email us and include
browser and OS specifics. Cheers - Kyran.)
cPickle lets us serialise our data very simply to a file. This means we can build up a datastore that is persistent between runs of our Python code and we don't need to write any parsers to load it back in - we just un-pickle it.
To understand the limitations, see what can and can't be pickled.
"""cPickly allows us to quick store/retrieve structured data"""
# cPickle is the faster implementation of the standard pickle module
# Protocol version 0 is the original ASCII protocol and is backwards compatible with earlier versions of Python.
# Protocol version 1 is the old binary format which is also compatible with earlier versions of Python.
# Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes.
# what can be pickled?
d = dict()
d['alice'] = 22
d['bob'] = 33
# we'll write using the original ASCII protocol 0
f0 = file('data_0.pickle', 'w')
cPickle.dump(d, f0, 0)
# protocol 2 requires binary files, it is much more space efficient
# note that protocols *aren't* compatible, you must stick to one and use it
#f2 = file('data_2.pickle', 'wb')
#cPickle.dump(d, f2, 2)
# open the file for binary input
#f_in = file('data_2.pickle', 'rb')
# load the pickled object, the protocol is detected
#d_in = cPickle.load(f_in)
# print the unpickled object
Are you learning Python? To keep the site running clean and ad-free we've started Club ShowMeDo. The club will allow you access to some special videos we're making to pass on programming skills we consider important to the development and enjoyment of Free Open Source Software (FOSS).
The focus at the moment is on developing with the Python programming language, one of the backbones of the FOSS movement with a huge number of included and third-party libraries.
We welcome feedback and we want to hear your ideas.
Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.