AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

The serialization of objects with Python Pickle

PARTAGEZ

With python pickleit is possible to serialize objects and dearialize them later. Many types of data can be converted, such as lists, dictionaries, or personalized classes, to save them, transmit them or reuse them easily. Discover in this article how this module works, its main methods, and practical examples to master serialization and deialialization in Python.

What is Python pickle ?

THE Python module pickle is a powerful tool to serialize objects, that is to say converting them into a binary format in order to save them, transmit them or reuse them later. This practice, called serializationis particularly useful for recording complex structures such as lists, dictionaries or class objects.

The reverse process, known as deialialization (apickling), makes it possible to rebuild an object from this binary format, by restoring all its information. Thus, once serialized, an object can be reused without requiring a complete recreation.

Python pickle Converts objects into an byte flow and provides the instructions necessary for successful deialialization, ensuring that the original structure is accurately rebuilt. These features allow considerable time saving, especially when it comes to frequently reuse the same objects. By default, the files generated with pickle are recorded in format .pkl.

These operations are carried out using methods such as pickle.dump() For serialization pickle.load() for deialialization.

Advice

As malware can be stored in a memory file, you should only convert files from reliable sources to the original format.

Managed NextCloud of Ionos Cloud

Work as a team in your own cloud

  • Data security
  • Integrated collaboration tools
  • Accommodation in European data centers

What types of data can be converted?

Python pickle can serialize the following types of data:

  • Boolean values: True And False as well as None
  • Complete whole and numbers
  • Character strings (normal and unicode)
  • Lists
  • Sets
  • Python tules
  • Directories composed only of corresponding objects
  • Functions
  • Python classes

Certain objects, such as open files, network sockets or execution threads, cannot be serialized. If you try to serial these types, Python Pickle will send an error.

What are the different methods available?

To work with Python picklethere are four methods inside the module:

  • pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None) : is used for serialization and creates a file with the desired result.
  • pickle.dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None) : is also used for serialization, but returns a chain of bytes.
  • pickle.load(file, *, fix_imports=True, encoding='ASCII', errors="strict", buffers=None) : is used for deialialization by reading the stored file.
  • pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict", buffers=None) : is also used for deialialization, but operates with a chain of bytes.

To distinguish methods, here is a mnemonic means: remember that the « S » in pickle.dumps And pickle.loads means  » string (Character chain in English).

Python's example pickle

To better illustrate the functioning of Python picklewe will work with a simple example. To do this, we will create a simple list containing four colors. Here is our code:

import pickle
couleurs = ['bleu', 'rouge', 'jaune', 'orange']

python

Then we open a text file in the format .pkl and let's use pickle.dump() To store our list there. For this, we use this code:

with open('fichier_couleurs.pkl', 'wb') as f:
pickle.dump(couleurs, f)

python

Abbreviation wb Indicates the system to open the file in binary form. This also makes it possible to take out the data it contains as byte objects. The function dump() Then saves the « Color » list in this file. Finally, the file is automatically closed.

Convert the backup file into its original format

If you want to dearialize a binary file, use the Python method pickle.load(). The following code makes it possible to convert the object in its original format and to launch an output. We add the abbreviation rbwhich means  » Read Binary ».

with open('fichier_couleurs.pkl', 'rb') as f:
couleurs_deserialisees = pickle.load(f)
print(couleurs_deserialisees)

python

We thus obtain the following output:

['bleu', 'rouge', 'jaune', 'orange']

python

Serialize a dictionary with Python pickle

Even more complex data types, such as directoriescan be easily serialized with python pickle And then converted into their original form. For this, we start by creating a repertoire called « people ». We deposit some different data on different people:

import pickle
personnes = {
'Personne 1': {
'Nom': "Marie", 'Âge': 56, 'Ville': "Paris"
},
'Personne 2': {
'Nom': "Paul", 'Âge': 66, 'Ville': "Paris"
},
'Personne 3': {
'Nom': "Lisa", 'Âge': 22, 'Ville': "Marseille"
},
'Personne 4': {
'Nom': "Laure", 'Âge': 34, 'Ville': "Lille"
}
}

python

In the following code, we therefore create a new file, convert the data, then convert them again to test the serialization of this directory:

with open("personnes_dict.pkl", "wb") as f:
pickle.dump(personnes, f)
with open("personnes_dict.pkl", "rb") as f:
dict_deserialise = pickle.load(f)
print(dict_deserialise)

python

The output obtained then looks like this:

personnes = {
    'Personne 1': { 'Nom': "Marie", 'Âge': 56, 'Ville': "Paris"},
    'Personne 2': { 'Nom': "Paul", 'Âge': 66, 'Ville': "Paris"},
    'Personne 3': { 'Nom': "Lisa", 'Âge': 22, 'Ville': "Marseille"},
    'Personne 4': { 'Nom': "Laure", 'Âge': 34, 'Ville': "Lille"}
}

python

You can now access this information as usual. For example, we ask for the following release:

# Définir le dictionnaire
dict_deserialise = {
    'Personne 1': {'Nom': "Marie", 'Âge': 56, 'Ville': "Paris"},
    'Personne 2': {'Nom': "Paul", 'Âge': 66, 'Ville': "Paris"},
    'Personne 3': {'Nom': "Lisa", 'Âge': 22, 'Ville': "Marseille"},
    'Personne 4': {'Nom': "Laure", 'Âge': 34, 'Ville': "Lille"}
}
# Imprimer la sortie
print(
    "Le nom de la troisième personne est "
    + dict_deserialise["Personne 3"]["Nom"]
    + " et elle a "
    + str(dict_deserialise["Personne 3"]["Âge"])
    + " ans."
)

python

Here is what our outing looks like:

Le nom de la troisième personne est Lisa et elle a 22 ans.

python

Convert a class into a character string

In the following example, we use Python pickle To record a class in a character string. This class contains completely different data types, but which can all be taken into account. We will therefore create a class called « classexample » and serialize it. Here is the corresponding code:

import pickle
class ClasseExemple:
    def __init__(self):
        self.a_number = 17
        self.a_list = [5, 10, 15]
        self.a_tuple = (18, 19)
        self.a_string = "bonjour"
        self.a_dict = {"couleur": "bleu", "chiffre": 3}
objet_exemple = ClasseExemple()
objet_serialise = pickle.dumps(objet_exemple)
print(f"Voici l’objet sérialisé :\n{objet_serialise}\n")
objet_exemple.a_dict = None
objet_deserialise = pickle.loads(objet_serialise)
print(f"Voici le a_dict de l’objet désérialisé :\n{objet_deserialise.a_dict}\n")

python

After having serialized the class, then having converted it into its original format, we obtain this outing:

Voici l’objet sérialisé :
b'\x80\x03c__main__\nClasseExemple\nq\x00)\x81q\x01.'
Voici le a_dict de l’objet désérialisé :
{'couleur': 'bleu', 'chiffre': 3}

python

Compress serialized objects

In principle, files stored with Python pickle are relatively compact. However, it is possible, and sometimes recommended, to compress the memory files even more. It works for example with the free compression program Bzip2which is part of the standard library of this programming language. In the following example, we create a character string, we serve it, then we apply the compression program:

import pickle
import bz2
exemple_chaine = """Almost heaven, West Virginia
Blue Ridge Mountains, Shenandoah River
Life is old there, older than the trees
Younger than the mountains, growin' like a breeze
Country roads, take me home
To the place I belong
West Virginia, mountain mama
Take me home, country roads."""
serialise = pickle.dumps(exemple_chaine)
compresse = bz2.compress(serialise)

python

Safety note to work with Python pickle

Although Python pickle Or a practical and effective method to convert objects, there is a major drawback that you must take into account when you work with this module: it is possible to transport malicious code via serialized data. This is not a problem when using your own data, but you have to pay attention to foreign files. So only desire for files whose source you know and in which you trust!

Advice

Direct deployment via GitHub: With Deploy Now of Ionos, you not only benefit from automatic detection of the framework and a quick installation, but you also have the choice between different types of prices. Find the solution that perfectly meets your needs!

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Web Marketing

Localhost: how to connect to 127.0.0.1?

When you call an IP address, you are usually trying to contact another computer on the Internet. However, if you call the IP address 127.0.0.1,

Web Marketing

What is Proxmox? – IONOS

Proxmox is an open source platform dedicated to virtualization and containerization. It allows you to manage and operate virtual machines, containers and high availability clusters.

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact