CSIT121_Lecture_ 15_-s1-full (6)

Object Serialization and Deserialization in Python

Introduction to Serialization

  • Serialization: The process of converting an object into a format that can be easily stored or transmitted.

  • Deserialization: The reverse process where the serialized data is converted back to an object.

Benefits of Serialization

  • Allows for efficient storage of data (e.g., binary format).

  • Data can be shared between different programs via network without needing to keep everything in text format.

  • Essential in parallel computing to share intermediate results between nodes in a cluster.

Applications

  • Used to save program state, share objects over the network, and handle storage for distributed systems.

  • In assignments, students will implement serialization to store records in binary or JSON format.

Python Modules for Serialization

  1. Pickle Module

    • Supports serialization of Python objects to binary format.

    • Serializes both data (attribute values) and function definitions.

    • Contains methods:

      • dump(): Saves serialized object to a file.

      • load(): Loads serialized object from a file.

    • Requirement: Both serialization and deserialization programs must have the same class definition.

  2. JSON Module

    • Serializes only the data (attribute values) but not function definitions, making it less comprehensive than Pickle.

    • More readable than binary format, as it uses text format.

    • Contains methods:

      • dumps(): Serializes a Python object to a JSON string.

      • loads(): Deserializes a JSON string back into a Python object.

    • Good for interoperability, especially with web technologies, as JSON is widely used in web applications.

Serialization and Deserialization Example

  • Using Pickle:

    import pickle
    data = {'key1': 'value1', 'key2': 2}
    with open('data.pkl', 'wb') as f:
        pickle.dump(data, f)  # Serialize
    
    with open('data.pkl', 'rb') as f:
        loaded_data = pickle.load(f)  # Deserialize
  • Using JSON:

    import json
    data = {'key1': 'value1', 'key2': 2}
    with open('data.json', 'w') as f:
        json.dump(data, f)  # Serialize
    
    with open('data.json', 'r') as f:
        loaded_data = json.load(f)  # Deserialize

Key Differences between Pickle and JSON

  • Pickle: Binary format, can serialize functions and methods. Not human-readable.

  • JSON: Text format, only serializes data, easier for human reading, but functions are not included.

Matplotlib Introduction

  • Matplotlib: A comprehensive library for creating static, animated, and interactive visualizations in Python. Allows for customizing plots.

  • Can draw various types of plots: line plots, scatter plots, bar charts, histograms, pie charts, etc.

Installation of Matplotlib

  • Use Python’s built-in package installer:

    # On Windows
    pip install matplotlib
    
    # On Mac
    pip3 install matplotlib
  • Verification: Import the library and check the version.

Basic Plotting with Matplotlib

  • Basic Components:

    • Lines, markers, grid, labels.

  • Example of Drawing a Line Plot:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.array([1, 2, 3, 4])
    y = np.array([4, 3, 2, 1])
    plt.plot(x, y)
    plt.title('Sample Line Plot')
    plt.xlabel('X-axis Label')
    plt.ylabel('Y-axis Label')
    plt.grid(True)
    plt.show()

Customizing Plots

  1. Line Styles:

    • Modify line thickness, style (solid, dashed, dotted).

    • Use parameters like linestyle and linewidth in plotting functions.

  2. Markers: Add markers on plot points.

  3. Subplots: Facilitate multi-plot layouts in a single window.

    plt.subplot(211)  # First subplot
    plt.subplot(212)  # Second subplot
  4. Legends: Use to distinguish different plots in the same figure.

Advanced Plotting Features

  • Grids and Backgrounds: Customize the grid appearance (style and color).

  • Annotations: To add text like labels at specific data points.

  • Saving Figures: Save plots in formats such as PNG, PDF using plt.savefig('filename.png').

Conclusion

  • Object serialization is vital for data storage, sharing, and processing.

  • Understanding the use of libraries such as Pickle and JSON, as well as Matplotlib for plotting, is essential for efficient programming and data visualization in Python.