issue189:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue189:python [2023/01/30 07:38] – d52fr | issue189:python [2023/02/02 10:25] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 8: | Ligne 8: | ||
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.** | ||
+ | |||
+ | 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. | **Shown top right is a simplified version of the hypothetical configuration file. | ||
Ligne 27: | Ligne 38: | ||
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).** | ||
+ | |||
+ | 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). | **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). | ||
Ligne 38: | Ligne 68: | ||
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:// | ||
+ | |||
+ | 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, | **Unfortunately, | ||
Ligne 58: | Ligne 100: | ||
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 | ||
+ | |||
+ | 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:// | **https:// | ||
Ligne 71: | Ligne 133: | ||
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.1675060729.txt.gz · Dernière modification : 2023/01/30 07:38 de d52fr