Serialization of Pickle and Cpickle:python objects

Source: Internet
Author: User
Tags object serialization pprint

Purpose: Python object serialization
Availability: Pickle At least 1.4 version, Cpickle 1.5 version

pickle module implements an algorithm that converts any Python object into a sequence of bytes (byets). This process also calls the serializing object. The byte stream representing the object can be transferred or stored after it is reconstructed and a new object with the same characteristics (the same characteristics) is created.

cPickleThe same algorithm was implemented using C instead of Python. This is several times faster than Python implementations, but it does not allow the user to derive subclasses from Pickle. If subclasses don't matter to your use, then Cpickle is a better choice.

warning : This document directly indicates that Pickle does not provide security assurances. Be careful if you use pickle in multithreaded communications (inter-process communication) or data storage or storage data. Do not trust data that you are not sure is safe.

Import

try:   importas pickleexcept:   import pickle
Encoding and decoding

The first example encodes a data structure into a string and then prints the string to the console. Use a data structure that contains all native types (native types). Any type of instance can be pickled (pickled, translator Note: The module name Pickle Chinese meaning for pickles), in a later example will be demonstrated. Use pickle.dumps() to create a string that represents the value of the object.

Try:ImportCpickle asPickleexcept:ImportPickleImportPprintdata = [{' A ':' A ',' B ':2,' C ':3.0} ]Print ' DATA: ', Pprint.pprint (data) data_string = pickle.dumps (data)Print ' PICKLE: ', data_string

Pickle By default is made up of ASCII characters only. You can also use a more efficient binary format, just because it's easier to understand when printing, and all of the examples on this page use ASCII output.

$ python pickle_string.pydata:[{ ' A ' :  ' a ' ,  ' B ' : 2 ,  ' C ' : 3.0 }]pickle: (LP1 (Dp2s ' a '  s ' A '  ss ' C '  f3ss ' B '  i2sa. 

After the data is serialized, you can write them into files, sockets, pipelines, and so on. You can then read it out of the file, unpickled it, and construct a new object with the same worth.

Try:ImportCpickle asPickleexcept:ImportPickleImportPprintdata1 = [{' A ':' A ',' B ':2,' C ':3.0} ]Print ' before: ', Pprint.pprint (data1) data1_string = Pickle.dumps (data1) data2 = Pickle.loads (data1_string)Print ' after: ', Pprint.pprint (DATA2)Print ' same?: ', (data1 isDATA2)Print ' EQUAL?: ', (data1 = = data2)

As you can see, this new constructed object is the same as the original object, but not the same object. That's not surprising.

$ python pickle_unpickle.pybefore:[{' A ':' A ',' B ':2,' C ':3.0}]after:[{' A ':' A ',' B ':2,' C ':3.0}]same?:FalseEQUAL?:True
Working with the flow

dumps()in addition loads() to and, Pickle also provides a pair of conversion functions that are used in the class file stream (File-like streams). You can write pairs of objects in a previous stream and then read them out of the stream, which does not require a few pre-written objects, or how large they are.

Try:ImportCpickle asPickleexcept:ImportPickleImportPprint fromStringioImportStringio class simpleobject(object):     def __init__(self, name):Self.name = Name L = List (name) L.reverse () Self.name_backwards ="'. Join (L)returndata = []data.append (Simpleobject (' Pickle ')) Data.append (Simpleobject (' Cpickle ')) Data.append (Simpleobject (' last '))# Use Stringio to simulate a fileout_s = Stringio ()# write to the stream forOinchDataPrint ' WRITING:%s (%s) '% (O.name, o.name_backwards) pickle.dump (o, out_s) Out_s.flush ()# Create a readable streamin_s = Stringio (Out_s.getvalue ())# Read Data while True:Try: o = pickle.load (in_s)exceptEoferror: Break    Else:Print ' READ:%s (%s) '% (O.name, o.name_backwards)

This example uses the Sringio cache (buffer) to simulate the flow, so we played a game when we built the readable stream. The formatting of a simple database can also use Pickles to store objects, just shelve is easier to work with.

$ python pickle_stream.pyWRITING: pickle (elkcip)WRITING: cPickle (elkciPc)WRITING: last (tsal)READ: pickle (elkcip)READ: cPickle (elkciPc)READ: last (tsal)

In addition to storing data, pickles is also very much called hand in interprocess communication (inter-process communication). For example, usingos.fork()Andos.pipe()You can create worker processes (worker processes), read job instructions (job instruction) from one pipe (pipe), and then write the results to another pipeline. The management worker pool and core code that feeds, accepts responses (response) from jobs can be reused because jobs and responses do not belong to a particular class. If you use a pipe or socket (sockets), do not forget to flush (flush) after dumping (dumps) all objects and pushing the data through the connection to the other end (end). If you want to write your own worker pool manager, seemultiprocessing

Serialization of Pickle and Cpickle:python objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.