Serialisation and DeSerialisation of python Objects


Serialisation and DeSerialisation a.k.a SerDe are the important aspects of streaming data into file from objects and from the file to objects. There are many ways in Pythons to do so.
Few of the tools are PyYaml, JSON and cPickle. However, cPickle is the fastest as it can SerDe any python objects such as Strings/texts, Lists, Dicts, Class obj in the byte formats.
One thing to worry about the SerDe such as cPickle is that, the security is a great concern if we store the objects information as byte stream into the file. Hence, cPickle should be used carefully with the highest possible protocol.Following Example, demonstrates the serDe with cPickle for Python class Objects.

import cPickle 
class acc: 
   def __init__(self, id, bal):
   self.id = id 
   self.bal = bal 
  def dep(self, amount): 
     self.bal += amount 
 def withdraw(self, amount): 
    self.bal -= amount ac = acc('1988999', 200) 

ac.dep(1000) 
ac.withdraw(500) 
fd = open("ty2", "w") 
cPickle.dump(ac, fd) 
fd.close() 
ac.dep(200) 
print ac.bal
fd = open("ty2", "r") 
ac = cPickle.load(fd) 
fd.close()
print ac.bal

Main thing to note in above code is that, cPickle dumps into file as below
with 'cPickle.dump(ac, fd)' :
(i__main__acc p1(dp2 S'bal'p3 I700 sS'id' p4 S'1988999'p5 sb. 

And it can be read easily and manipulated.Hence we replace the class object dumping into file with following code:
cPickle.dump(ac, fd,protocol=cPickle.HIGHEST_PROTOCOL)
This makes sure that, the data dumps into file is not readable.

Comments