
In this Article we're going to see :-
serializing object *(a process called serialization)
de-serializing object *(a process called de-serialization)
let's start with the definations :-
Serialization :-
Serialization is the process of converting data structures or object state into a format that can be stored or saved in memory for latter use, that format in which the object state saved will follow some rules which will help inde-serializing the object later.
De-serialization :-
De-serialization is just opposite ofserialization in De-serialization ,we get the object state back to recreate the original object from the serialized format.
Now that we have understand defination ofserialization and de-serialization.
let's see some place where they are very helpful :-
While sending data over theinternet the data is transfered mostly inJson form (JSON is a format that encodes objects in a string and deserialization it (convert string -> object))
Hugh Machine Learning Model that are trained on hugh amount of data ,need to stored in some form for later use ,we cannot re-train them again and again , that is where serialization help
to storeML model state.Big Data system usesserialization and deseralization to store large amount of data.
we have seen only few applications,serialization anddeserialization are pretty much applied whererever there is need to store data in some form to use it for later use.
Now we going to look how to doSerialzation and Deserializatiom in Python
Pickle :-
Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk.
Pickling is a way to convert a python object (list, dict, etc.) into a character stream
It saves the find the.pkl format
methods available :-
dump() − The dump() method serializes to an open file (file-like object).
dumps() − Serializes to a string
load() − Deserializes from an open-like object.
loads() − Deserializes from a string.
Serializing the object
import pickleclass Student: def __init__(self,firstname,lastname,age,standard): self.firstname = firstname self.lastname = lastname self.age = age self.standard = standard def showinfo(self): print(f"Firstname :- {self.firstname}") print(f"Lastname :- {self.lastname}") print(f"Age :- {self.age}") print(f"Standarad :- {self.standard}")student1 = Student("Adarsh","Raven",21,"12th")student2 = Student("Ankit",'Raven',24,'11th')# Student Infoprint("Student1 :- ")student1.showinfo()print("\nStudent2 :- ")student2.showinfo()# Serializing objectpicked_student1 = pickle.dumps(student1)picked_student2 = pickle.dumps(student2)# Object stored in Byte steamprint("serialized student",picked_student1)Output :-Student1 :- Firstname :- AdarshLastname :- RavenAge :- 21Standarad :- 12thStudent2 :- Firstname :- AnkitLastname :- RavenAge :- 24Standarad :- 11thserialized student b'\x80\x03c__main__\nStudent\nq\x00)\x81q\x01}q\x02(X\t\x00\x00\x00firstnameq\x03X\x06\x00\x00\x00Adarshq\x04X\x08\x00\x00\x00lastnameq\x05X\x05\x00\x00\x00Ravenq\x06X\x03\x00\x00\x00ageq\x07K\x15X\x08\x00\x00\x00standardq\x08X\x04\x00\x00\x0012thq\tub.'
Deserializing the same object
orignal_student1 = pickle.loads(picked_student1)orignal_student2 = pickle.loads(picked_student2)print("\n\nAfter Getting object back from the saved state :- \n")print("Student1 :- ")orignal_student1.showinfo()print("\nStudent2 :- ")orignal_student2.showinfo()Output :-After Getting object back from the saved state :- Student1 :- Firstname :- AdarshLastname :- RavenAge :- 21Standarad :- 12thStudent2 :- Firstname :- AnkitLastname :- RavenAge :- 24Standarad :- 11th
there are also other ways available to do the same task
we have covered in the above example, it will mention resources to those if you're intrested :-
:-)
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse