ZDNet UK


Skip to Main Content

ZDNet.co.uk - Winner of Best Business Website 2007
  1. Home
  2. News
  3. Blogs
  4. Reviews
  5. Prices
  6. Resources
  7. Community
  8. My ZDNet

 

ZDNet UK RSS Feeds


IT Jobs

Application development Toolkit

Get yourself into a Python cPickle

Shelley Doll Builder.com

Published: 15 Aug 2002 11:44 BST

  • Email
  • Trackback
  • Clip Link
  • Print friendly
  • Post Comment

There are many reasons to do this, and most languages today provide some prepackaged approach. Python is no different. I'll start with a little background information on serialisation. Then, I'll walk through an example that shows how to use cPickle to serialise and deserialise list values.

Your basic pickle
Serialisation, also called pickling or flattening, converts structured data into a data stream format. Essentially, this means that structures such as lists, tuples, functions, and classes are preserved using ASCII characters between data values. The pickle data format is standardised, so strings serialised with pickle can be deserialised with cPickle and vice versa.

The main difference between cPickle and pickle is performance. The cPickle module is many times faster to execute because it's written in C and because its methods are functions instead of classes. While this improves performance, it also means that the cPickle methods can't be extended or customised, whereas pickle classes can.

Serialisation is useful in a number of ways. It can be a time and resource saver when used on data that will be transmitted, encrypted, or stored in a database. Information is serialised in Python and then processed. When the data is retrieved, it is deserialised and used.

For additional information on cPickle and pickle, refer to the Python online reference manual's section "3.14 pickle--Python object serialisation."

Serving up condiments: A cPickle example
Now let's look at a simple example that demonstrates basic usage of the cPickle module.

First, the condiments.py script informs Python that we'll be using the cPickle module:
import cPickle

Next, I define the object I want to serialise and store. In this case, it's a list of condiments I've got in the fridge:
inFridge = ["ketchup", "mustard", "relish"]
print inFridge

That print statement's output will display the following:
['ketchup', 'mustard', 'relish']

I want to save my results in a file called fridge.txt, so the script creates a file handler and opens the file for writing:
FILE = open("fridge.txt", 'w')

Now the magic happens. I call the cPickle command, dump, to pickle my data and dump the results to the file:
cPickle.dump(inFridge, FILE)

I'm finished for now, so the script closes the file:
FILE.close()

I now have a file, fridge.txt, that contains the following:
(lp1
S'ketchup'
p2
aS'mustard'
p3
aS'relish'
p4
a.

The pickle and cPickle modules have an option to save the information in a binary format; however, I've used the default ASCII because it is human-readable.

Next

Previous

1 2


  • Email
  • Trackback
  • Clip Link
  • Print friendly Print with Dell

Did you find this article useful?
13 out of 27 people found this useful


Full Talkback thread

0 comments


Company/Topic Alerts

Create a new alert from the list below:










Related Jobs

TEST CONSULTANT POSITIONS (Tester) TESCOM UK Central London

Other benefits are: - A formal and structured appraisal process, to ensure progression within the company - Diverse and varied projects with various ...

C++ Expert Bnaking London City

Large-scale application enhancements with object-oriented methods are also likely. C++, UNIX An amazing opportunity for a C++ UNIX expert with OR ...

SAP Programme/Project Manager

The successful candidates will be able to :- Consult on business issues and information technology to deliver lasting value to clients as part of IBM ...

Discussions

319762 319762

Eve of Distraction

Saturday 26 July 2008, 4:37 AM

1 comment

Featured Talkback

The fact is: Software developers today are really designers and not coders. The reason that business anlaysts exist today to model solutions is because they understand the value of designing software before writing it. All too often developers create code that has little value because they do not understand that business classes interact with other classes within the confines of a working model or pattern.

By: 1000165269

Read full story:
Making sense of agile modelling