Serialization

Serialization:
Serialization(pickling) allows you to save non-textual information to memory or transmit it over a network.Pickling essentially takes any data object,such as dictionaries,lists,or even class instances and converts it into a byte set that can be used to “reconstitute” the original data.

Example for Serialization:

# Python program to illustrate 
# pickle.dump() 
import pickle 
from StringIO import StringIO 
class SimpleObject(object): 
def __init__(self, name): 
self.name = name 
l = list(name) 
l.reverse() 
self.name_backwards = ''.join(l) 
return 
data = [] 
data.append(SimpleObject('pickle')) 
data.append(SimpleObject('cPickle')) 
data.append(SimpleObject('last')) 
                                                # Simulate a file with StringIO 
out_s = StringIO() 
                                                # Write to the stream 
for o in data: 
print 'WRITING: %s (%s)' % (o.name, o.name_backwards) 
pickle.dump(o, out_s) 
out_s.flush() 

OUTPUT:
WRITING: pickle (elkcip)
 WRITING: cPickle (elkciPc)
 WRITING: last (tsal) 
Translate »