Ceci est une ancienne révision du document !
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://docs.python.org/3/whatsnew/3.11.html to find out all the information.
One of the new things that did come along with 3.11.1 is a new standard library module called tomllib, which has “support” for parsing TOML style documents. Notice that I said “support”. It's not complete support, but it is support. I'll talk more about that in a few moments.
What is TOML?
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.
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 “standard” configparser type config file, the first entry under the [Themes] section would not be possible directly, since configparser doesn’t support lists without manipulation. In TOML, sections are called tables. The available_themes key has the value of an array. Once it's ported into Python, it becomes a list.
Now let's look at how to get the data into a program.
Of course, we have to import the tomllib library. Remember this is supported directly only under Python 3.11.
import tomllib import pprint
Next, we open the configuration file and use the load method of the library.
with open(“config.toml”, “rb”) as 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).
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…
Available Themes: ['notsodark', 'plastik', 'waldorf', 'page_wheat', 'clearlooks', 'forest-light', 'forest-dark', 'default', 'clam', 'classic', 'alt'] Your default Theme is: waldorf Your current Theme is: notsodark 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://docs.python.org/3/library/tomllib.html.
Unfortunately, Python does not provide any way to properly write out the TOML data. The good news is that there is a third party library called tomli_w which will allow you to write the TOML data back to a file. So if the user decides to change his current theme from 'notsodark' to 'clam', it's a simple chore to make the change and write it back. You can install it via pip.
pip install tomli-w
Once it’s installed, you can simply write it pretty much as it’s a normal, but binary file.
Remember the tomllib (bottom right) comes with only Python 3.11. So what do you do if you are running Python 3.8.10 and you aren't ready to upgrade quite yet? Never fear. The tomli-w library is currently supported in versions 3.7 all the way up to 3.11. To get the parser analog to tomllib, you can install tomli. Again, just use pip.
pip install tomli
Of course, if you are using tomli rather than tomllib, you need to do the import a bit differently.
#import tomllib import tomli import tomli_w import pprint
The home pages for tomli-w and tomli can be found at https://github.com/hukkin/tomli-w
https://github.com/hukkin/tomli
If you want to take a look at the complete information on TOML, you can visit the home page at https://toml.io/en/
There is another TOML third-party package for Python at https://github.com/sdispater/tomlkit . It’s documentation can be found at https://github.com/sdispater/tomlkit/blob/master/docs/quickstart.rst. I haven’t had much of a chance to play with it, but it looks promising.
I’ve put the config.toml file and the Python file (toml1.py) in my repository at https://github.com/gregwa1953/FCM-189.
That’s it for this month. Happy New Year!!!
Until next time, as always; stay safe, healthy, positive and creative!