Labs / Python OOP

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects

Abstract:

If we want to pass objects between different programming languages, we must serialize the objects into a standard format, such as serialized Web objects in formats like XML, YAML, and JSON. Such serialized Web objects are easy to interact with other programming languages, highly readable, and can be readily transmitted to other systems or Clients.

FreakStudio's Blog

Previous Recommendations:

You who are learning embedded systems still don't know Object Oriented? ?!

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 00 Introduction to Object Oriented Design Methodology

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 01 Basic Concepts of Object Oriented Programming

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 02 Python Implementation of Classes and Objects - Creating Classes with Python

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 03 Python Implementation of Classes and Objects - Adding Attributes to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 04 Python Implementation of Classes and Objects - Adding Methods to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 05 Python Implementation of Classes and Objects - PyCharm Code Labels

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 06 Python Implementation of Classes and Objects - Data Encapsulation of Custom Classes

The most beginner-friendly Object Oriented programming tutorial on the entire network: 07 Python implementation of classes and objects - type annotations

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 08 Python Implementation of Classes and Objects - @property Decorator

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 09 Python Implementation of Classes and Objects - Relationships Between Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 10 Python Implementation of Classes and Objects - Class Inheritance and Liskov Substitution Principle

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 11 Python Implementation of Classes and Objects - Subclass Calling Parent Class Methods

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 12 Python Implementation of Classes and Objects - Using Python's logging Module to Output Program Runtime Logs

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 13 Python Implementation of Classes and Objects – Installation and Usage of Sourcetrail, a Visual Code Reading Tool

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 14 Python Implementation of Classes and Objects - Static Methods and Class Methods of Classes

The most beginner-friendly Object Oriented programming tutorial across the entire network: 15 Python implementation of classes and objects - the __slots__ magic method

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Internet: 16 Python Implementation of Classes and Objects - Polymorphism, Method Overriding and Open Closed Principle

The most beginner-friendly Object Oriented programming tutorial across the network: 17 Python implementation of classes and objects - duck typing and "file-like object"

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 18 Python Implementation of Classes and Objects - Multiple Inheritance and Drawing Curve Graphs of Serial Port Data with PyQtGraph

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 19 Python Implementation of Classes and Objects – Automatically Generate File Comments and Function Comments with PyCharm

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 20 Python Implementation of Classes and Objects - Implementation of Composition Relationship and CSV File Saving

The Most Beginner-Friendly Object Oriented Programming Tutorial Online: 21 Python Implementation of Classes and Objects - Multi-File Organization: Module and Package

The most beginner-friendly Object Oriented programming tutorial on the entire network: 22 Python implementation of classes and objects - exceptions and syntax errors

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 23 Python Implementation of Classes and Objects - Throwing Exceptions

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 24 Python Implementation of Classes and Objects - Exception Capture and Handling

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 25 Python Implementation of Classes and Objects - Python Determines Input Data Types

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 26 Python Implementation of Classes and Objects - Context Managers and the with Statement

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 27 Python Implementation of Classes and Objects - Implementation of Exception Hierarchy and Custom Exception Classes in Python

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 28 Python Implementation of Classes and Objects - A Comprehensive Summary of Python Programming Principles, Philosophy and Specifications

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 29 Python Implementation of Classes and Objects – Assertions, Defensive Programming and the Usage of the help function

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 30 Python's Built-in Data Types - The object Root Class

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 31 Python's Built-in Data Types - Object and Type

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 32 Python's Built-in Data Types - Class and Instance

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 33 Built-in Data Types in Python – The Relationship Between Object and Type

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 34 Python's Built-in Data Types - Commonly Used Composite Data Types in Python: Tuples and Named Tuples

The most beginner-friendly Object Oriented programming tutorial on the entire network: 35 Python's built-in data types - docstrings and the __doc__ attribute

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 36 Python's Built-in Data Type - Dictionary

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 37 Common Composite Data Types in Python - Lists and List Comprehensions

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 38 Common Composite Data Types in Python – Implementing stack, Queue and Deque with Lists

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 39 Common Composite Data Types in Python - Sets

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 40 Common Composite Data Types in Python – Usage of Enumerations and the enum Module

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 41 Common Composite Data Types in Python - Queues (FIFO, LIFO, Priority Queue, Deque and Circular Queue)

The most beginner-friendly Object Oriented programming tutorial across the network: 42 Commonly used composite data types in Python - collections container data types

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 43 Common Composite Data Types in Python - Extending Built-in Data Types

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 45 Implementing Common Data Structures in Python - Linked List, Tree, Hash Table, Graph and Heap

The most beginner-friendly Object Oriented programming tutorial across the network: 46 Python function methods and interfaces - functions and event-driven frameworks

The most beginner-friendly Object Oriented programming tutorial across the network: 47 Python function methods and interfaces - Callback function

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 48 Python function Methods and Interfaces - Positional Arguments, Default Arguments, Variable Arguments and Keyword Arguments

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 49 Python function Methods and Interfaces – The Difference Between function and Methods, and Lambda Anonymous Functions

The most beginner-friendly Object Oriented programming tutorial on the entire network: 50 Python function methods and interfaces - Interfaces and Abstract Base Classes

The most beginner-friendly Object Oriented programming tutorial on the entire network: 51 Python function methods and interfaces - Implementing interfaces with Zope

The most beginner-friendly Object Oriented programming tutorial across the network: 52 Python function methods and interfaces - Protocol and interfaces

The most beginner-friendly Object Oriented programming tutorial across the entire network: 53 Python Strings and Serialization - Strings and Character Encodings

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 54 Python Strings and Serialization - String Formatting and the format Method

The most beginner-friendly Object Oriented programming tutorial on the entire network: 55 Python Strings and Serialization - Byte Sequence Types and Mutable Byte Strings

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 56 Python Strings & Serialization - Regular Expression and re Module Application

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 57 Python Strings and Serialization - Serialization and Deserialization

More exciting content can be found here:

Speed Up Your Python: A Quick Guide to Python Parallel Computing

A Complete Guide to Debugging Principles of CM3 Microcontrollers

After half a month of grinding work, the ultimate summary of embedded technology stack is now released

The "Secret Martial Arts Manual" for Computer Science Competitions

Avnet ZUBoard 1CG Development Board — A New Choice for Deep learning

Deploy SenseCraft Model to Grove Vision AI V2 Image Processing Module

Document and Code Access:

You can download the document via the following link:

https://github.com/leezisheng/Doc

19

This document mainly introduces how to use Python for Object Oriented programming, and requires readers to have a basic understanding of Python syntax and microcontroller development. Compared with other blogs or books that explain Python Object Oriented programming, this document is more detailed and focuses on embedded host computer applications, taking the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the slave computer as application examples. Meanwhile, it uses the Sourcetrail code software to visualize the code for reading, so as to facilitate readers' understanding.

The link to obtain the relevant sample code is as follows:

https://github.com/leezisheng/Python-OOP-Demo

Body Text

Serialize Web objects

If we want to pass objects between different programming languages, we must serialize the objects into a standard format, such as serialized Web objects in formats like XML, YAML, and JSON. Such serialized Web objects are easy to interact with other programming languages, highly readable, and can be readily transmitted to other systems or Clients.

In Python, the most commonly used serialized Web object is the yaml file for environment configuration. Anaconda can manage different environment configurations. When we want to share our own environment configuration with others, we can generate a yaml file, so that others can quickly import the yaml file to build an environment identical to ours to run the code.

We can use the following command in the command line to generate a yaml file:

conda env export > environment.yml
bzip2

Implementing serialization of Web objects using JSON

JavaScript Object Notation (JSON) is a human-readable format for storing basic data types. As a standard format, JSON can be parsed by a wide variety of Client systems, which makes it highly suitable for data transmission between completely disparate systems. Furthermore, JSON does not support any executable code — only data can be serialized — making it far more difficult to embed malicious code within it.Due to its characteristic of being easily parsed by JavaScript engines, JSON format is often used for data transmission between Web servers and browsers with JavaScript capabilities. If the server side of a Web application is written in Python, it is necessary to convert internal data into JSON format to ensure data compatibility and versatility.

We can use the json module in Python to generate JSON files. Similar to pickle, this module also provides four functions: dumps (), dump (), loads (), and load (), but the output result is in JSON format. Additionally, the json functions operate on str objects rather than bytes. Therefore, when outputting or loading, we need to create files in text mode instead of binary mode. JSON can only serialize basic types such as integers, floating-point numbers, and strings, as well as simple containers like dictionaries and lists.All of these map directly to the JSON format, however JSON cannot represent classes, methods, or function. It is not possible to transmit complete objects in this format. Since the recipient is not typically a Python object, it cannot interpret classes or methods in the same way that Python does.

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects
import json

# List of personnel information
humaninfodic={
    'age'   : 18,
    'name'  : True,
    'gender': 10,
    'email' : 11.1,
}

# Serialize to a file
with open('test.json', 'w') as fp:
    json.dump(humaninfodic, fp, indent=4)

# Deserialize the contents of the file
with open('test.json', 'r') as fp:
    dic = json.load(fp)
    print(dic)

The running results are as follows:

18 10 11.1 0
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects

Here, it should be noted that when non-string valued keys in a Python dictionary are converted to JSON strings, they will all be converted to lowercase strings: for example, True will be mapped to true, False will be mapped to false, and None will be mapped to null; meanwhile, tuples in Python will be converted to the array type during serialization, but when deserializing, the array type will be converted to the list type in Python.

The correspondence between JSON and Python's built-in data types is as follows:

JSON Type

Python Types

{}

dict

[]

list

"string"

str

1234.56

int or float

true/false

True/False

null

None

If the object you want to serialize only contains data, you can directly serialize the __dict__ attribute of the object. Alternatively, for specific objects, we can also create or parse JSON serialization dictionaries through custom code.

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects

In the json module, both the object serialization and deserialization features provide an optional parameter for executing specific custom operations. Specifically, dump and dumps methods accept a keyword argument named cls (short for "class", as it is a reserved keyword in Python). When this parameter is passed, it must be a subclass of JSONEncoder that has overridden the default method. This method is designed to accept an object of any type as a parameter and convert it into a dictionary type that json can handle.If you encounter an object type that you do not know how to handle during the processing, you can call the super () method to enable the object to be serialized in a normal way, that is, to be serialized as a basic type.

Similarly, load and loads methods also accept a parameter named cls, which is a subclass of JSONDecoder. Normally, passing a function via the object_hook keyword is sufficient. This function is tasked with receiving a dictionary as an argument and returning an object. If it encounters a dictionary it does not know how to handle during processing, it can choose to return it directly without any modification. This design allows users to handle various complex data types more flexibly and conveniently when parsing JSON data.

import json

# Define the contact class
class Contact:
    def __init__(self, first, last):
        # Attribute 1, first name is the given name
        self.first = first
        # Attribute 2, last name is the family name
        self.last = last
    @property
    def full_name(self):
        return("{} {}".format(self.first, self.last))

# Custom serialization encoder class
class ContactEncoder(json.JSONEncoder):
    # The default method checks the type of the object we want to serialize
    def default(self, obj):
        # If it is a contact class, we manually convert it to a dictionary
        if isinstance(obj, Contact):
            return {
                    # An extra attribute is passed to indicate this is a contact object
                    # Because there is no other way to know its type after loading
                    'is_contact': True,
                    'first': obj.first,
                    'last': obj.last,
                    'full': obj.full_name}
        # Otherwise, let its parent class handle serialization (assuming it is a basic type, which json knows how to handle)
        return super().default(obj)

# Define a JSON file decoder function
def decode_contact(dic):
    # Write a function that takes a dictionary as its parameter
    # Check whether the is_contact variable is present to decide whether to convert it to a contact
    if dic.get('is_contact'):
        return Contact(dic['first'], dic['last'])
    else:
        return dic

if __name__ == '__main__':
    c = Contact("John", "Smith")
    data = json.dumps(c, cls=ContactEncoder)
    print(data)
    c = json.loads(data, object_hook=decode_contact)
    print(c.full_name)

The running results are as follows:

0

Implementing Serialization of Web Objects Using XML

In today's software development field, XML, as a flexible and powerful markup language, has been widely used in multiple scenarios such as data storage, configuration management, and network transmission. Its extensibility and self-describing nature make it an ideal format for data exchange between different systems and platforms.

XML stands for Extensible Markup Language, which is a set of rules for defining semantic tags that divide a document into numerous components and identify those components. It is designed for transmitting and storing data, rather than for presenting or displaying data. It is also a meta-markup language, that is, a syntactic language that defines the rules for creating other semantic, structured markup languages related to specific domains.

Below we list an example of an xml file:

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
        <item id="4">
            <caption>Test</caption>
        </item>
    </login>
    <item id="2">
        <caption>Zope</caption>
    </item>
</catalog>

The file in the above XML format contains a root element named "catalog". Under the root element, there is a child element named "maxid" with a value of "4". This is followed by a child element named "login", which contains two child elements: one is a child element named "caption" with a value of "Python"; the other is a child element named "item" whose id attribute value is "4", and it contains a child element named "caption" with a value of "Test". Finally, there is also a child element named "item" whose id attribute value is "2", and it contains a child element named "caption" with a value of "Zope".

The part enclosed in angle brackets, "<tag>", is a tag. Tags must appear in pairs: if there is an opening tag, there must be a corresponding closing tag. For example, "<name>" is an opening tag, and "</name>" is a closing tag.

A basic XML document structure consists of the following parts:

Structural Component

Function

Declaration Section

Located at the very beginning of a document, it declares the version and encoding of XML. For example: <? xml version="1.0" encoding="UTF-8"? >.

root element

Every XML document has a root element that contains all the other elements.

child element

The root element can contain multiple child elements inside it, and child elements can be nested to form a tree-like structure.

Attribute

Elements can have attributes, which provide additional information about the elements.

Text content

An element can contain text content.

As a concise and powerful programming language, Python provides a rich set of libraries for processing XML data, making it simple and efficient to parse, modify, and create XML documents. Python offers three methods for parsing XML: SAX, DOM, and ElementTree.

SAX2

Among them, xml.etree.ElementTree (referred to as ET) provides a lightweight Pythonic approach to processing XML data. ET allows users to easily read, modify, and create XML files. As it is part of the standard library, it can be used without additional installation, making it a convenient choice for handling XML data.

For the ElementTree library, common usage operations include parsing XML documents, obtaining the root element, traversing child elements, reading the tags, text and attributes of elements, as well as how to obtain or delete specific elements as needed, and saving the modified XML document.

Here, the XML file is only for reference and will not be elaborated on in detail.

Implementing Serialization of Web Objects Using YAML

While XML files are powerful, they are generally not very easy to read due to the nature of its markup language. Conversely, XML excels in complex projects that require fine-grained control over validation, schemas, and namespaces. In contrast, YAML focuses on data formatting, presenting content in the form of readable code, and its inline style is quite similar to JSON. YAML is designed to provide a more intuitive and readable data representation to meet the needs of different scenarios.

YAML, as a highly human-friendly data serialization language, can be seamlessly integrated with current mainstream programming languages. Its name "YAML" is a recursive abbreviation of "YAML Ain't a Markup Language", a naming that not only reflects its uniqueness but also highlights its distinctive design philosophy.

YAML has a syntax structure similar to that of other high-level languages, which can easily represent various data forms such as lists, hash tables, and scalars. It ingeniously uses whitespace characters for indentation and heavily relies on visual features to demonstrate the hierarchical relationships of data structures. This design makes YAML particularly suitable for scenarios including editing data structures, writing various configuration files, printing debugging information, and presenting document outlines.

In addition, YAML configuration files usually use ". yml" as the file suffix, and this naming convention helps users quickly identify and manage YAML files.

Its basic syntax rules are as follows:

(1) Case-sensitive;

(2) Use indentation to indicate hierarchical relationships;

(3) When indenting, the Tab key is not allowed; only spaces are permitted.

(4) The number of indentation spaces is not important, as long as elements at the same level are aligned on the left side.

There are three types of data structures supported by YAML:

(1) Object: A collection of Attribute–Value Pairs, also known as mapping / hashes / dictionary;

(2) Array: a set of values arranged in order, also known as a sequence / list;

(3) Scalars: individual values that cannot be further divided.

In Python, there are multiple libraries available for parsing and generating YAML data, among which PyYAML is the most commonly used one.

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects

Here, we will briefly explain the syntax of YAML files by taking the following YAML configuration file named config.yaml as an example:

# Configuration file example
  
# Server configuration
server:  
  host: localhost  
  port: 8080  

# Database configuration
database:  
  type: MySQL  
  host: 127.0.0.1  
  port: 3306  
  username: root  
  password: password  

# Logging
logging:  
  level: info  
  file: app.log

# Application configuration
app:
  debug: true
  log_level: info

Here, this file uses the hash symbol (#) to denote comments, indentation to indicate hierarchical relationships, and a colon (:) to separate keys and values.

This YAML file consists of the following sections:

(1) Server configuration: including the host name and Port Number;

(2) Database configuration: including database type, host name, Port Number, username and password;

(3) Logging: including log level and log file path;

(4) Application configuration: including whether to enable debug mode and the log level.

Both XML and YAML files have certain complex features that, if maliciously exploited, can allow arbitrary command execution on the host machine. This is different from JSON files, which do not support any executable code and only allow data to be serialized.

347622978