Python: Object Serialization with pickle

Serialization:

#!/usr/bin/env python                                                                                                                                                                                           

import cPickle as pickle
import os
import sys

sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))

class Package(object):
    def __init__(self, pn, pv):
        self.pn = pn
        self.pv = pv

    def __str__(self):
        return str(self.pn)+' '+str(self.pv)

def serialize(cachefile, *listPackages):
    '''                                                                                                                                                                                                         
    serialize a list of packages to the specified cache file                                                                                                                                                    
    '''
    pickler = pickle.Pickler(cachefile)
    for package in listPackages:
        pickler.dump(package)

def main(argv=None):
    if len(argv) != 1:
        cachefile = 'tmp.dat'
    else:
        cachefile = argv[0]

    p1 = Package('package1', 1.0)
    p2 = Package('package1', 2.0)
    p3 = Package('package1', 3.0)
    p4 = Package('package1', 4.0)
    p5 = Package('package1', 5.0)

    with open(cachefile, 'wb') as cachefile:
        serialize(cachefile, p1, p2, p3, p4, p5)

if __name__ == '__main__':
    main(sys.argv[1:])

De-serialization

#!/usr/bin/env python                                                                                                                                                                                           

import cPickle as pickle
import os
import sys

sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
from serialize import Package

def main(argv=None):
    '''                                                                                                                                                                                                         
    read the cache file and print the contents                                                                                                                                                                  
    '''
    if len(argv) != 1:
        cachefile = 'tmp.dat'
    else:
        cachefile = argv[0]

    with open(cachefile, 'rb') as cachefile:
        pickled = pickle.Unpickler(cachefile)
        while cachefile:
            try:
                package = pickled.load()
            except:
                break
            if isinstance(package, Package):
                print package

if __name__ == '__main__':
    main(sys.argv[1:])

Result

chenqi@chenqi-OptiPlex-760:~/mypro/python$ ./serialize.py cache.dat
chenqi@chenqi-OptiPlex-760:~/mypro/python$ ./de-serialize.py cache.dat
package1 1.0
package1 2.0
package1 3.0
package1 4.0
package1 5.0

 

你可能感兴趣的:(python,serialization,pickle,cPickle)