1.

What do you mean by object serialization? Is it the same as data persistence?

Answer»

Python has a built-in file object that represents a disk file. The file API has write() and read() methods to store data in Python objects in computer files and read it back.

To save data in a computer file, file object is first obtained from built-in open() function.

>>> fo=open("file.txt","w") >>> fo.write("Hello World") 11 >>> fo.close()

Here ‘w’ as mode parameter of open() function indicates that file is opened for WRITING data. To read back the data from file open it with ‘r’ mode.

>>> fo=open("file.txt","r") >>> fo.read() 'Hello World'

However, the file object can store or retrieve only string data. For other type of objects, they must be either converted to string or their byte representations and store in binary files opened with ‘wb’ mode.

This type of manual conversion of objects in string or byte format (and vice versa) is very cumbersome. Answer to this situation is object serialization.

Object serialization means transforming it in a format that can be stored, so as to be able to deserialize it later, recreating the original object from the serialized format. Serialization may be done disk file, byte string or can be TRANSMITTED via network sockets. When serialized data is BROUGHT back in a form identical to original, the mechanism is called de-serialization.

Pythonic term for serialization is pickling while de-serialization is often referred to as unpickling. Python’ library contains pickle module that provides dumps() and loads() functions to serialize and deserialize Python objects.

Following code shows a dictionary object PICKLED using dumps() function in pickle module.

>>> import pickle >>> data={'No':1, 'name':'Rahul', 'marks':50.00} >>> pckled=pickle.dumps(data) >>> pckled b'\x80\x03}q\x00(X\x02\x00\x00\x00Noq\x01K\x01X\x04\x00\x00\x00nameq\x02X\x05\x00\x00\x00Rahulq\x03X\x05\x00\x00\x00marksq\x04G@I\x00\x00\x00\x00\x00\x00u.'

Original state of object is retrieved from pickled representation using loads() function.

>>> data=pickle.loads(pckled) >>> data {'No': 1, 'name': 'Rahul', 'marks': 50.0}

The serialized representation of data can be persistently stored in disk file and can be retried back using dump() and load() functions.

>>> import pickle >>> data={'No':1, 'name':'Rahul', 'marks':50.00} >>> fo=open('pickledata.txt','wb') >>> pickle.dump(data, fo) >>> fo.close() >>> #unpickle >>> fo=open('pickledata.txt','rb') >>> data=pickle.load(fo) >>> data {'No': 1, 'name': 'Rahul', 'marks': 50.0}

Pickle data format is Python-specific in nature. To serialize and deserialize data to/from other standard formats Python library provides json, csv and xml modules.

As we can see, serialized data can be persistently stored in files. However, file created using write() method of file object does store data persistently but is not in serialized form. Hence we can say that serialized data can be stored persistently, but converse is not necessarily true.



Discussion

No Comment Found