/  Technology   /  Python Serialization & Deserialization

Python Serialization & Deserialization

Python does not enforce user to perform Object-Oriented Programming. It provides the feasibility of performing both Procedure Oriented Programming and Object-Oriented Programming.

In Object-Oriented Programming, for performing any operation we do create Objects. As per OOPS basics, Object is an instance of Class. But the functions and variables we create in Class will be understandable by Python.

we need a mechanism to transform Python Code into Byte Code (Machine Readable Code) which can be achieved through Serialization. The Converted Byte Code can be saved in any file format i.e., the file can have any extension.

Serialization is also known as Marshalling or Pickling.

The Converted Byte Code from Python Code as part of Serialization can be converted back to Python Code through Deserialization.

Deserialization is also known as Unmarshalling or Unpickling.

Python has a specialized package named “Pickle” to perform Serialization and Deserialization.

How to Perform Serialization:

Serialization is performed using dump () method in Pickle library.

Let us understand the Serialization through below example:

Step 1: Create Class named Employee

#create a class named Employee
class Emp:
    #initialize the attributes
def __init__(self, name, employee_id, department, title):
        self.name = name
        self.employee_id = employee_id
        self.department = department
        self.title = title
    def __str__(self):
        return '{} , id={}, is in {} and is a {}.'.format(self.name, self.employee_id, self.department, self.title)

Step 2: Saving data as a pickle file

Now, we have a class named Employee, the next step is to convert into byte code using pickle library and is performed as below:

import pickle # The standard naming convention for importing Pickle Library
with open ('Emp.pickle','wb') as f: 

We have used the “wb” file mode to open the file in binary mode for performing a write operation to Byte Code (Binary Language)

Here we are enclosing open file through with Statement so that the file gets automatically closed once the operation is accomplished.

Protocols in Pickle:

As the name suggests Protocols are the conventions used for performing Serialization and Deserialization through Pickle Library. There are 4 different Protocols available as part of Pickling.

Protocol Version and Python Version are directly Proportionate to ensure the higher the protocol the higher the Python Version it supports.

  • Protocol 0 – It is the main and original Protocol, the best feature of this protocol is its compatibility with earlier versions.
  • Protocol 1 – It is an old binary format Protocol which is used till Python 2.3
  • Protocol 2 – It is introduced as a part of python 2.3
  • Protocol 3 – It is introduced as a part of Python 3.0
  • Protocol 4 – It is introduced as a part of Python 3.4 which has support for very large objects

Protocol can be mentioned as part of Open condition as below:

with open(‘file name’, ‘wb’) as f:
	pickle.dump(filename,f,protocol name)

If we don’t specify Protocol number default will be Protocol 0.

The dump () method in the pickle module takes a serializable Python data structure, in this case, the variables created as part of Employee Class and performs the following operation.

  • Serializes it into a binary format using the latest version of the pickle protocol.
  • Saves it to an open file.

Output file is saved in Byte format so if we open the file it will be displayed in Binary Language.

How to Perform Deserialization:

If we want to convert Machine Code to Python Code Deserialization is used. Deserialization is performed using load () method in Pickle library.

Let us understand the Deserialization through below example:

Step 1: Reading Data from Pickle file

import pickle
with open ('Employee.pickle', 'rb') as f:
    data = pickle.load(f)
    print (data)

Leave a comment