issue189:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue189:python [2023/01/29 20:30] – créée d52fr | issue189:python [2023/02/02 10:25] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Python 3.11.1 is now out (as of 6 December 2022) and with it comes, as usual, a number of changes and improvements. If you want to know what is new with 3.11.1, you can go to https:// | + | **Python 3.11.1 is now out (as of 6 December 2022) and with it comes, as usual, a number of changes and improvements. If you want to know what is new with 3.11.1, you can go to https:// |
One of the new things that did come along with 3.11.1 is a new standard library module called tomllib, which has " | One of the new things that did come along with 3.11.1 is a new standard library module called tomllib, which has " | ||
Ligne 7: | Ligne 7: | ||
TOML stands for Tom's Obvious Minimal Language and from what I understand, was created mainly as a means of storing configuration data. Many other ways of storing configuration data don't provide a means of saving comments inline. | TOML stands for Tom's Obvious Minimal Language and from what I understand, was created mainly as a means of storing configuration data. Many other ways of storing configuration data don't provide a means of saving comments inline. | ||
- | To get started, let's assume that I created a Python program that used PAGE to create the GUI front end. Let's further assume that I want to give the user the ability to select the Theme for the program (since it uses ttk Widgets). I want the program to remember what that user decided for their theme of choice. To do this, I will use a configuration file to keep all the customization information. | + | To get started, let's assume that I created a Python program that used PAGE to create the GUI front end. Let's further assume that I want to give the user the ability to select the Theme for the program (since it uses ttk Widgets). I want the program to remember what that user decided for their theme of choice. To do this, I will use a configuration file to keep all the customization information.** |
- | Shown top right is a simplified version of the hypothetical configuration file. | + | Python 3.11.1 est maintenant disponible (à partir du 6 décembre 2022) et, comme d' |
+ | |||
+ | L'une des nouveautés de la version 3.11.1 est un nouveau module de la bibliothèque standard appelé tomllib, qui offre un « support » pour l' | ||
+ | |||
+ | Qu' | ||
+ | |||
+ | TOML est l' | ||
+ | |||
+ | Pour commencer, supposons que j'ai créé un programme Python qui utilise PAGE pour créer l' | ||
+ | |||
+ | |||
+ | **Shown top right is a simplified version of the hypothetical configuration file. | ||
At this point, it looks like a standard configuration file that you would find just about anywhere. However, if this were a " | At this point, it looks like a standard configuration file that you would find just about anywhere. However, if this were a " | ||
Ligne 26: | Ligne 37: | ||
data = tomllib.load(f) | data = tomllib.load(f) | ||
- | Using pretty print, we can now look at the data that was brought in from the config file (next page, top right). | + | Using pretty print, we can now look at the data that was brought in from the config file (next page, top right).** |
- | You can see that it is simply just a dictionary. To access the data, we do it just like any other dictionary (next page, bottom left). | + | En haut à droite, vous voyez une version simplifiée du fichier de configuration hypothétique. |
+ | |||
+ | À ce stade, il ressemble à un fichier de configuration standard que l'on peut trouver à peu près partout. Cependant, s'il s' | ||
+ | |||
+ | Voyons maintenant comment introduire les données dans un programme. | ||
+ | |||
+ | Bien sûr, nous devons importer la bibliothèque tomllib. Rappelez-vous que celle-ci n'est prise en charge directement que sous Python 3.11. | ||
+ | |||
+ | import tomllib | ||
+ | import pprint | ||
+ | |||
+ | Ensuite, nous ouvrons le fichier de configuration et utilisons la méthode load de la bibliothèque. | ||
+ | |||
+ | with open(" | ||
+ | |||
+ | data = tomllib.load(f) | ||
+ | |||
+ | En utilisant pretty print, nous pouvons maintenant regarder les données qui ont été apportées par le fichier de configuration (page suivante, en haut à droite). | ||
+ | |||
+ | **You can see that it is simply just a dictionary. To access the data, we do it just like any other dictionary (next page, bottom left). | ||
The output of our little program will look like this… | The output of our little program will look like this… | ||
Ligne 37: | Ligne 67: | ||
Program version 0.7.1 | Program version 0.7.1 | ||
- | The Python tomllib library provides only two functions, tomllib.loads which loads a TOML string and returns a dictionary, and tomllib.load which reads a TOML file and returns again, a dictionary. See https:// | + | The Python tomllib library provides only two functions, tomllib.loads which loads a TOML string and returns a dictionary, and tomllib.load which reads a TOML file and returns again, a dictionary. See https:// |
- | Unfortunately, | + | Vous pouvez voir qu'il s'agit simplement d'un dictionnaire. Pour accéder aux données, nous procédons comme pour tout autre dictionnaire (page suivante, en bas à gauche). |
+ | |||
+ | Le résultat de notre petit programme ressemblera à ceci : | ||
+ | |||
+ | Available Themes: [' | ||
+ | Your default Theme is: waldorf | ||
+ | Your current Theme is: notsodark | ||
+ | Program version 0.7.1 | ||
+ | |||
+ | La bibliothèque Python tomllib ne fournit que deux fonctions, tomllib.loads qui charge une chaîne TOML et retourne un dictionnaire, | ||
+ | |||
+ | |||
+ | **Unfortunately, | ||
pip install tomli-w | pip install tomli-w | ||
Ligne 56: | Ligne 98: | ||
import pprint | import pprint | ||
- | The home pages for tomli-w and tomli can be found at https:// | + | The home pages for tomli-w and tomli can be found at https:// |
+ | Malheureusement, | ||
+ | pip install tomli-w | ||
- | https:// | + | Une fois qu'il est installé, vous pouvez simplement l' |
+ | |||
+ | Rappelez-vous que le tomllib (en bas à droite) n'est livré qu' | ||
+ | |||
+ | pip install tomli | ||
+ | |||
+ | Bien sûr, si vous utilisez tomli plutôt que tomllib, vous devez faire l' | ||
+ | |||
+ | #import tomllib | ||
+ | import tomli | ||
+ | import tomli_w | ||
+ | import pprint | ||
+ | |||
+ | Les pages d' | ||
+ | https:// | ||
+ | |||
+ | et | ||
+ | |||
+ | **https:// | ||
If you want to take a look at the complete information on TOML, you can visit the home page at https:// | If you want to take a look at the complete information on TOML, you can visit the home page at https:// | ||
Ligne 70: | Ligne 132: | ||
That’s it for this month. Happy New Year!!! | That’s it for this month. Happy New Year!!! | ||
- | Until next time, as always; stay safe, healthy, positive and creative! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | https:// | ||
+ | |||
+ | Si vous souhaitez consulter les informations complètes sur TOML, vous pouvez visiter la page d' | ||
+ | |||
+ | Il existe un autre paquet tiers TOML pour Python à l' | ||
+ | |||
+ | J'ai placé le fichier config.toml et le fichier Python (toml1.py) dans mon dépôt à https:// | ||
+ | |||
+ | C'est tout pour ce mois-ci. Bonne année ! !! | ||
+ | |||
+ | Jusqu' | ||
issue189/python.1675020641.txt.gz · Dernière modification : 2023/01/29 20:30 de d52fr