python pickle example | python pickle tutorial | part 2

Namaste Friends,






                                                      This is our second part of python pickle module tutorial series. and in this tutorial i am going to show you how to use pickle.pickler(), pickle.Unpickler(), cStringIO.StringIO() etc.

so, keep reading ....

if you are new visitor then, first check our First Part Of Python pickle tutorial series.

or directly check our index for more interesting tutorials.

so, let's start



Input : [1]  


#
# ==================================================
#          PYTHON PICKLE MODULE TUTORIAL
# ==================================================
# 
# author   : suraj singh bisht
# contact  : SSB
#            surajsinghbisht054@gmail.com
#            https://bitforestinfo.blogspot.com
#            
# Here, For This Tutorial
#
# I am using
#
# Operating System : Ubuntu 16.04
# Python Version   : python 2.7.12
# Editor           : ipython notebook
#

Output : [1]  



Input : [2]  

#
# import pickle module
#
import pickle
from cStringIO import StringIO

Output : [2]  



Input : [3]  

#
# Choose Any File Name Only For Example Purpose
#
stored_file = "save_pickled_data.txt"

Output : [3]  



Input : [4]  

#
# Creating Mathematics function Solving python class object
# you can use as you want
#
class Math:
    def __init__(self, x,y):
        self.x = x
        self.y = y
        
    # Adding Values
    def add(self):
        return self.x + self.y
    
    # substracting values
    def substract(self):
        return self.x - self.y
    
    # multiplying value
    def multiply(self):
        return self.x * self.y
    
    # dividing value
    def divide(self):
        return self.x / self.y
    
    # square value
    def square(self):
        return self.x ** 2

Output : [4]  



Input : [5]  

#
# here, create class object
#
m = Math(50 , 24)

Output : [5]  



Input : [6]  

#
# let's try class functions
#
print "Adding      : ", m.add()
print "Subtracting : ", m.substract()
print "Multiplying : ", m.multiply()
print "Dividing    : ", m.divide()
print "Square      : ", m.square()

Output : [6]  



Input : [7]  

# 
# Here, i am creating a file object
#
file_src_f = open(stored_file,'wb')
#
# StringIO also completely works like a file object
# or you can say this function completely works
# like a virtual file object
#
file_src_s = StringIO()

Output : [7]  



Input : [8]  

#
# creating pickler object for pickling directly
# data into file
#
p = pickle.Pickler(file_src_s)

Output : [8]  



Input : [9]  

#
# converting structure object into pickles 
# and saving on file
#
p.dump(m)

Output : [9]  



Input : [10]  

#
# get saved values from StringIO files 
#
file_src_s.getvalue()

Output : [10]  

"(i__main__\nMath\np0\n(dp1\nS'y'\np2\nI24\nsS'x'\np3\nI50\nsb."

Input : [11]  

#
# read data from StringIO object 
#
read_data = file_src_s.getvalue()

Output : [11]  



Input : [12]  

#
# unpickling data using loads function
#
p = pickle.loads(read_data)

Output : [12]  



Input : [13]  

#
# testing class object functions working 
# correctly or not.
#
print "Adding      : ", m.add()
print "Subtracting : ", m.substract()
print "Multiplying : ", m.multiply()
print "Dividing    : ", m.divide()
print "Square      : ", m.square()

Output : [13]  



Input : [14]  

#
# here, creating Unpickler function object
#
un_p = pickle.Unpickler(StringIO(read_data))

Output : [14]  



Input : [15]  

#
# unpickling data 
#
p = un_p.load()

Output : [15]  



Input : [16]  

#
# testing math class object is working correctly
# or not
#
print "Adding      : ", m.add()
print "Subtracting : ", m.substract()
print "Multiplying : ", m.multiply()
print "Dividing    : ", m.divide()
print "Square      : ", m.square()

Output : [16]  



Input : [17]  

# 
# again pickle data because here, we will try
# to use StringIO for sharing pickled data
#
src = pickle.dumps(p)

Output : [17]  



Input : [18]  

#
# creating virtual file handler
# 
virtual_file = StringIO()

Output : [18]  



Input : [19]  

#
# pickle data and save on virtual file object
#
double_pickle = pickle.dump(src, virtual_file)

Output : [19]  



Input : [20]  

#
# import pprint because with this module we can
# print functions and class object neatly
#
import pprint

Output : [20]  



Input : [21]  

#
# print virtual file object data
#
pprint.pprint(virtual_file.getvalue())

Output : [21]  



Input : [22]  

#
# now, here i am using unpickler function with
# virtual file object
#
single_pickled = pickle.Unpickler(virtual_file)

Output : [22]  



Input : [23]  

#
# seek(0) is useful for flushing data 
#
virtual_file.seek(0)

Output : [23]  



Input : [24]  

#
# loading unpickling data from virtual file handler
#
single_unpickled_data = single_pickled.load()

Output : [24]  



Input : [25]  

#
# printing unpickling data
#
pprint.pprint(single_unpickled_data)

Output : [25]  



Input : [26]  

#
# again unpickling data
# because we use pickle process 2 times
#
p = pickle.loads(single_unpickled_data)

Output : [26]  



Input : [27]  

#
# now, here i am testing math class object function
#
print "Adding      : ", m.add()
print "Subtracting : ", m.substract()
print "Multiplying : ", m.multiply()
print "Dividing    : ", m.divide()
print "Square      : ", m.square()

Output : [27]  





this python pickle module tutorial series ends here.

in my next tutorial i will show you more interesting python module usages and examples.



So, stay tunned with us.




Thanks For Reading.

For More Update, Visit Our Blog Regularly. 
, Subscribe Our Blog, 
Follow Us and share it.
For Any Type of Suggestion, Help Or Question
Contact me:
S.S.B
surajsinghbisht054@gmail.com
or Comment Below

Share this

Related Posts

Previous
Next Post »