Location>code7788 >text

The Best Object-Oriented Programming Tutorial on the Net for Getting Started: 55 Python Strings and Serialization - Byte Sequence Types and Variable Byte Strings

Popularity:597 ℃/2024-10-03 00:46:37

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 55 Python Strings and Serialization - Byte Sequence Types and Variable Byte Strings

image

Abstracts:

In Python, character encoding is the process of mapping characters to bytes, and bytearrays (bytes) are the actual data structures that store those bytes. The main difference between bytearrays and mutable byte strings is that they are mutable and usable. bytearrays are mutable byte sequences that allow modification of their contents.

Link to original article:

FreakStudio's Blog

Past Recommendations:

You're learning embedded and you don't know how to be object oriented?

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 00 Introduction to Object-Oriented Design Methods

The network's most suitable for the introduction of object-oriented programming tutorials: 01 Basic Concepts of Object-Oriented Programming

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 02 Python Implementations of Classes and Objects - Creating Classes with Python

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 03 Python Implementations of Classes and Objects - Adding Attributes to Custom Classes

The Best Object-Oriented Programming Tutorial on the Net for Getting Started: 04 Python Implementation of Classes and Objects - Adding Methods to Custom Classes

The Best Object-Oriented Programming Tutorial on the Net for Getting Started: 05 Python Implementation of Classes and Objects - PyCharm Code Tags

The best object-oriented programming tutorials on the net for getting started: 06 Python implementation of classes and objects - data encapsulation of custom classes

The best object-oriented programming tutorial on the net for getting started: 07 Python implementation of classes and objects - type annotations

The best object-oriented programming tutorials on the net for getting started: 08 Python implementations of classes and objects - @property decorator

The best object-oriented programming tutorials on the net for getting started: 09 Python implementation of classes and objects - the relationship between classes

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 10 Python Implementations of Classes and Objects - Class Inheritance and Richter's Replacement Principle

The best object-oriented programming tutorials on the net for getting started: 11 Python implementation of classes and objects - subclasses call parent class methods

The network's most suitable for the introduction of object-oriented programming tutorials: 12 classes and objects of the Python implementation - Python using the logging module to output the program running logs

The network's most suitable for the introduction of object-oriented programming tutorials: 13 classes and objects of the Python implementation - visual reading code artifacts Sourcetrail's installation use

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 14 Python Implementations of Classes and Objects - Static Methods and Class Methods for Classes

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 15 Python Implementations of Classes and Objects - __slots__ Magic Methods

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 16 Python Implementations of Classes and Objects - Polymorphism, Method Overriding, and the Principle of Open-Close

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 17 Python Implementations of Classes and Objects - Duck Types and "file-like objects"

The network's most suitable for the introduction of object-oriented programming tutorials: 18 classes and objects Python implementation - multiple inheritance and PyQtGraph serial data plotting graphs

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 19 Python Implementations of Classes and Objects - Using PyCharm to Automatically Generate File Annotations and Function Annotations

The best object-oriented programming tutorials on the web for getting started: 20 Python implementation of classes and objects - Combinatorial relationship implementation and CSV file saving

The best introductory object-oriented programming tutorials on the net: 21 Python implementation of classes and objects - Organization of multiple files: modulemodule and packagepackage

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 22 Python Implementations of Classes and Objects - Exceptions and Syntax Errors

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 23 Python Implementation of Classes and Objects - Throwing Exceptions

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 24 Python Implementations of Classes and Objects - Exception Catching and Handling

The best object-oriented programming tutorials on the web for getting started: 25 Python implementation of classes and objects - Python to determine the type of input data

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 26 Python Implementations of Classes and Objects - Context Managers and with Statements

The best introductory object-oriented programming tutorials on the web: 27 Python implementation of classes and objects - Exception hierarchy and custom exception class implementation in Python

The best object-oriented programming tutorials on the net for getting started: 28 Python implementations of classes and objects - Python programming principles, philosophies and norms in a big summary

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 29 Python Implementations of Classes and Objects - Assertions and Defensive Programming and Use of the help Function

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 30 Python's Built-In Data Types - the root class of object

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 31 Python's Built-In Data Types - Object Object and Type Type

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 32 Python's Built-in Data Types - Class Class and Instance Instance

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 33 Python's Built-In Data Types - The Relationship Between the Object Object and the Type Type

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 34 Python's Built-In Data Types - Python's Common Compound Data Types: Tuples and Named Tuples

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 35 Python's Built-In Data Types - Document Strings and the __doc__ Attribute

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 36 Python's Built-In Data Types - Dictionaries

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 37 Python's Common Composite Data Types - Lists and List Derivatives

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 38 Python's Common Composite Data Types - Using Lists to Implement Stacks, Queues, and Double-Ended Queues

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 39 Python Common Composite Data Types - Collections

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 40 Python's Common Compound Data Types - Enumeration and Use of the enum Module

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 41 Python's Common Composite Data Types - Queues (FIFO, LIFO, Priority Queue, Double-Ended Queue, and Ring Queue)

The best introductory object-oriented programming tutorials on the web: 42 Python commonly used composite data types-collections container data type

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 43 Python's Common Composite Data Types - Extended Built-In Data Types

The Best Object-Oriented Programming Tutorial on the Net for Getting Started: 44 Python Built-In Functions and Magic Methods - Magic Methods for Rewriting Built-In Types

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 45 Python Implementations of Common Data Structures - Chain Tables, Trees, Hash Tables, Graphs, and Heaps

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 46 Python Function Methods and Interfaces - Functions and Event-Driven Frameworks

The network's most suitable for the introduction of object-oriented programming tutorials: 47 Python function methods and interfaces - callback function Callback

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 48 Python Function Methods and Interfaces - Positional Arguments, Default Arguments, Variable Arguments, and Keyword Arguments

Best Object-Oriented Programming Tutorials on the Net for Getting Started: 49 Python Functions Methods and Interfaces - Difference between Functions and Methods and lamda Anonymous Functions

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 50 Python Function Methods and Interfaces - Interfaces and Abstract Base Classes

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 51 Python Function Methods and Interfaces - Implementing Interfaces with Zope

Best Object-Oriented Programming Tutorials for Beginners on the Web: 52 Python Functions Methods and Interfaces-Protocol Protocols and Interfaces

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 53 Python Strings and Serialization - Strings and Character Encoding

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 54 Python Strings and Serialization - String Formatting and the format method

More highlights to watch:

Accelerating Your Python: A Quick Guide to Python Parallel Computing

Understanding CM3 MCU Debugging Principles in One Article

Liver half a month, embedded technology stack summary out of the big

The "Secrets of the Martial Arts" of the Computer Competition

A MicroPython open source project collection: awesome-micropython, including all aspects of Micropython tool library

Avnet ZUBoard 1CG Development Board - A New Choice for Deep Learning

SenseCraft Deploys Models to Grove Vision AI V2 Image Processing Module

Documentation and code acquisition:

The following link can be accessed to download the document:

/leezisheng/Doc

image

This document mainly introduces how to use Python for object-oriented programming, which 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, with common serial port data sending and receiving, data processing, and dynamic graph drawing as application examples for the host computer and the lower computer, and using Sourcetrail code software to visualize and read the code for readers' easy understanding.

The link to get the relevant sample code is below:/leezisheng/Python-OOP-Demo

main body (of a book)

Character encoding and byte sequences bytes

In computer memory, we follow standardized encoding principles and generally use the Unicode encoding system. However, when data needs to be persisted to the hard disk or transferred over a network, it is converted to UTF-8 encoding. For example, when editing a document using Notepad, the UTF-8 characters read from the file are converted to Unicode characters for memory processing. When the editing is complete, the system converts these Unicode characters back to UTF-8 encoding to ensure the accuracy and compatibility of document storage. This process ensures the smooth exchange of data between different platforms and systems, and meets the diverse needs for data encoding in modern computing environments.

image

image

image

We can get the default encoding of our own computer with the following code:

import sys
print(())

Let's take a look at the results of the run, showing the system's default UTF-8 encoding:

image

Python3 makes a clear distinction between strings (str) and byte sequences (bytes), also known as byte streams. Data is stored in memory and on disk as a stream of bytes, which consists of a sequence of bytes (byte, 8 bits). In the previous section, we mentioned that bytes are the lowest level of storage in computing, so strings are actually built-in bytes. However, people are not accustomed to using bytes directly, which is not readable and cumbersome to manipulate, and it is strings that people can easily read and understand. So strings and byte streams need to be converted. The process of converting a byte stream into something people can read is called decoding, and the process of converting a string into a byte stream is called encoding. In the following example, we declare an array of bytes and then use the .decode method of the bytes class to convert it to a Unicode string that we can understand. This method accepts different character encoding as a parameter, the sample code is as follows:

_# denotes cliché in Latin-1 encoding_.
_# b denotes that a bytes object is being defined.
_# \x character indicates that each byte is represented as a hexadecimal number _#
bytestr = b'\x63\x6c\x69\x63\x68\xe9'
_# Output the ASCII symbols of these bytes themselves _#
_# Whether the character is unknown to ASCII or retains its original hexadecimal format _#
_# The output contains the character b, which tells us that this is a bytes, not a string.
print(bytestr)
_# Decode the string in Latin-1 encoding; the decode method returns a normal string_ _# The output contains the b character, which tells us it's a bytes, not a string.
print(("latin-1"))
_# Decode a string using iso8859-5 encoding _# print(("iso8859-5"))
print(("iso8859-5"))
_# Decode a string using CP437 encoding _# print(("CP437"))
print(("CP437"))

The results of the run are as follows:

image

We can also use encode to convert a string into an array of bytes, as shown in the following example code:

str = "cliché"
_# The first 3 encodings create a different set of bytes for the accented characters _
print(("UTF-8"))
print(("latin-1")))
print(("cp437"))
_# The fourth one can't handle this case, there is an unknown character _#.
print(("ascii"))

The results of the run are as follows:

image

As you can see, the last case throws an exception because there are unknown characters, and sometimes we want those unknown characters to be handled differently. encode method also accepts an optional string parameter called errors, which defines how the character should be handled. The optional values are shown below:

  • The strict: strict substitution rule is the default and throws an exception when the byte sequence cannot represent a character in the encoding we are using;
  • replace: When the replace policy is used, the character is replaced with another character;
  • ignore: The ignore policy simply omits unrecognized characters;
  • xmlcharrefreplace: The xmlcharrefreplace policy creates an xml entity representing Unicode characters so that strings can be converted based on the XML document.

image

image

Python has about 100 different encoding formats, see the following link for details:

/zh-cn/3/library/#standard-encodings

Some encoding formats have multiple names, such as 'latin-1', 'iso_8859_1' and '8859' all referring to the same encoding. Choosing the correct encoding is critical when working with encoded files. I highly recommend the UTF-8 encoding, which not only has the ability to represent any Unicode character, but has become a widely accepted standard in modern software.

Python source code is also a text file, so when you save source code that contains Chinese characters, you need to be sure to specify that it be saved in UTF-8 encoding. When the Python interpreter reads the source code, we usually write these two lines at the beginning of the file in order for it to read it in UTF-8 encoding:

_#!/usr/bin/env python3_
_# -*- coding: utf-8 -*-_

The first comment is to tell the Linux/OS X system that this is a Python executable program, and Windows systems will ignore the comment;

The second comment is to tell the Python interpreter to read the source code in UTF-8 encoding; otherwise, the Chinese output you write in the source code may be garbled.

Declaring UTF-8 encoding does not mean that your .py files are UTF-8 encoded, you must and should make sure that the IDE is using UTF-8 encoding. Take the PyCharm IDE as an example:

image

Variable byte string bytearray

The bytes type is similar to str in that they are immutable. This means that once a bytes object has been created, we cannot extend or modify it. Nevertheless, we can still perform indexing or slicing operations on bytes objects, or even search for specific byte sequences. However, this immutability may cause some inconvenience when dealing with I/O operations. This is because when performing I/O operations, we often need to cache bytes until they are ready to be sent.

For example, when receiving data from a socket, it may be necessary to call the recv function several times to receive the complete message. In this case, we can solve this problem by using the built-in bytearray type. bytearray behaves like a list, but its elements are bytes. Its constructor can be initialized by taking bytes objects as arguments. In addition, bytearray provides an extend method that allows us to add more bytes to the array, for example, when more data is received from a socket or other I/O channel, and this flexibility makes bytearray useful for scenarios where the sequence of bytes needs to be dynamically modified.

bytearray is used as follows:

Syntax: bytearray(source, encoding, errors)
Parameters:
-source[optional]: Initializes the array of bytes
-encoding[optional]: Encoding of the string
-errors[optional]: Takes action when encoding fails
Returns: 
Returns an array of bytes of the given size.

In the following example, we create two bytearray arrays and encode the strings using the bytearray() function:

str = "Geeksforgeeks"

_# encoding the string with unicode 8 and 16_
array1 = bytearray(str, 'utf-8')
array2 = bytearray(str, 'utf-16')

print(array1)
print(array2)

The code output is as follows:

image

We can also modify bytearray directly through slicing operations:

array2[4:6] = b"\x15\xa3"
print(array2)

The results of the run are as follows:

image

Note that if we want to manipulate a single element in a bytearray, we need to pass in an integer value from 0 to 255, which represents a specific bytes. This integer represents a specific bytes, and if we use a character or bytes object, an exception will be thrown.

Single-byte characters can be converted to integers using the ord function (short for ordinal). This function returns an integer representing a single character. Sample code is as follows:

b = bytearray(b'abcdef')
b[3] = ord(b'g')
b[4] = 68
print(b)

The results of the run are as follows:

image

Here, after constructing the variable byte string, we replace the character at index 3 (the fourth character, since, like the list, the index starts at 0) with 103, which is the number corresponding to the lowercase letter g in the ASCII character returned by the ord function. As an illustration, we also replace the next character with the byte number 68, which corresponds to the uppercase letter D in the ASCII character.

The bytearray type has methods that allow it to be manipulated like a list, and we can also iterate over it, find its length, and use methods such as count and find, just as we can with bytes or str objects. The difference is that bytearray is a mutable type, which can be used to build complex sequences from a specific input source.

_# establishbytearray_
array = bytearray(b"acbcdc")
_# (math.) ergodic_
for value in array:
    print(value)
_# pair of elementsb'c'utilizationcountmethodologies_
print("Count of c is:", (b"c"))
_# pair of elementsb'c'utilizationfindmethodologies_
print("Count of c is:", (b"c"))
_# pair of elementsb'c'utilizationlenmethodologies_
print("Count of bytes:", len(array))

image