• Home
  • About
  • Contact
  • Privacy
  • Terms
  • DCMA
  • Write For Us / Submit
Tech News, Magazine & Review WordPress Theme 2017
  • Tech
    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    120+ Best Creative Names for Team at Work

    120+ Best Creative Names for Team at Work

    Can You Repair Printed Circuit Board ?

    Mastering PCB Board Repair Comprehensive Guide

    Breaking Barriers Federal Inmate Texting Service

    Breaking Barriers Federal Inmate Texting Service

    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

  • Gear
    • All
    • Camera
    • Laptop
    • Smartphone
    The New Galaxy Watch Ultra and Galaxy Ring Are Announced by Samsung

    The New Galaxy Watch Ultra and Galaxy Ring Are Announced by Samsung

    Exploring the Innovative Features of Pear Phones

    Exploring the Innovative Features of Pear Phones

    Latest Smart Home Gadgets for a Connected Life

    Latest Smart Home Gadgets for a Connected Life

    Eco-Friendly Products for Students

    Eco-Friendly Products for Students

    Essential Photography Equipment and Gadgets

    Mastering Your Shots: Essential Photography Equipment and Gadgets

    Level Up Your Game: Must-Have Gaming Gear!

    Level Up Your Game: Must-Have Gaming Gear!

    Trending Tags

    • Best iPhone 7 deals
    • Apple Watch 2
    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • iOS 10
    • iPhone 7
    • Sillicon Valley
  • Gaming
    Master the World of Online Gaming with the Best Gaming VPN

    Master the World of Online Gaming with 3 Best Gaming VPN

    7 Powerful Strategies to Overcome Video Game Addiction and Reclaim Your Life

    7 Powerful Strategies to Overcome Video Game Addiction and Reclaim Your Life

    Powerful Ways Xbox Cloud Gaming is Revolutionizing the Gaming World

    5 Powerful Ways Xbox Cloud Gaming is Revolutionizing the Gaming World

    9 Best Alternative Games Like Kahoot

    9 Best Alternative Games Like Kahoot

    The Top 8 Free Bubble Shooter Games for Endless Entertainment

    The Top 8 Free Bubble Shooter Games for Endless Entertainment

    Cloud Gaming Revolution: How Streaming is Changing the Future of Gaming

    Cloud Gaming Revolution: How Streaming is Changing the Future of Gaming

  • Crypto
    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    120+ Best Creative Names for Team at Work

    120+ Best Creative Names for Team at Work

    Can You Repair Printed Circuit Board ?

    Mastering PCB Board Repair Comprehensive Guide

    Breaking Barriers Federal Inmate Texting Service

    Breaking Barriers Federal Inmate Texting Service

    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

  • Business
    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    120+ Best Creative Names for Team at Work

    120+ Best Creative Names for Team at Work

    Can You Repair Printed Circuit Board ?

    Mastering PCB Board Repair Comprehensive Guide

    Breaking Barriers Federal Inmate Texting Service

    Breaking Barriers Federal Inmate Texting Service

    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

No Result
View All Result
Geeky Insider
  • Tech
    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    120+ Best Creative Names for Team at Work

    120+ Best Creative Names for Team at Work

    Can You Repair Printed Circuit Board ?

    Mastering PCB Board Repair Comprehensive Guide

    Breaking Barriers Federal Inmate Texting Service

    Breaking Barriers Federal Inmate Texting Service

    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

  • Gear
    • All
    • Camera
    • Laptop
    • Smartphone
    The New Galaxy Watch Ultra and Galaxy Ring Are Announced by Samsung

    The New Galaxy Watch Ultra and Galaxy Ring Are Announced by Samsung

    Exploring the Innovative Features of Pear Phones

    Exploring the Innovative Features of Pear Phones

    Latest Smart Home Gadgets for a Connected Life

    Latest Smart Home Gadgets for a Connected Life

    Eco-Friendly Products for Students

    Eco-Friendly Products for Students

    Essential Photography Equipment and Gadgets

    Mastering Your Shots: Essential Photography Equipment and Gadgets

    Level Up Your Game: Must-Have Gaming Gear!

    Level Up Your Game: Must-Have Gaming Gear!

    Trending Tags

    • Best iPhone 7 deals
    • Apple Watch 2
    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • iOS 10
    • iPhone 7
    • Sillicon Valley
  • Gaming
    Master the World of Online Gaming with the Best Gaming VPN

    Master the World of Online Gaming with 3 Best Gaming VPN

    7 Powerful Strategies to Overcome Video Game Addiction and Reclaim Your Life

    7 Powerful Strategies to Overcome Video Game Addiction and Reclaim Your Life

    Powerful Ways Xbox Cloud Gaming is Revolutionizing the Gaming World

    5 Powerful Ways Xbox Cloud Gaming is Revolutionizing the Gaming World

    9 Best Alternative Games Like Kahoot

    9 Best Alternative Games Like Kahoot

    The Top 8 Free Bubble Shooter Games for Endless Entertainment

    The Top 8 Free Bubble Shooter Games for Endless Entertainment

    Cloud Gaming Revolution: How Streaming is Changing the Future of Gaming

    Cloud Gaming Revolution: How Streaming is Changing the Future of Gaming

  • Crypto
    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    120+ Best Creative Names for Team at Work

    120+ Best Creative Names for Team at Work

    Can You Repair Printed Circuit Board ?

    Mastering PCB Board Repair Comprehensive Guide

    Breaking Barriers Federal Inmate Texting Service

    Breaking Barriers Federal Inmate Texting Service

    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

  • Business
    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

    120+ Best Creative Names for Team at Work

    120+ Best Creative Names for Team at Work

    Can You Repair Printed Circuit Board ?

    Mastering PCB Board Repair Comprehensive Guide

    Breaking Barriers Federal Inmate Texting Service

    Breaking Barriers Federal Inmate Texting Service

    transfer whatsapp from android to iphone

    How to Transfer WhatsApp from Android to iPhone!

    Understanding the Role of Humidity Chambers in Climate Testing

    Understanding the Role of Humidity Chambers in Climate Testing

    Everything You Get to Know About Movember Beard Memes

    Everything You Get to Know About Movember Beard Memes

    Best Book Recommendation Apps

    11 Best Book Recommendation Apps

    How to quickly divide or Split PDF files

    How to Quickly Divide or Split PDF Files

Submit
Geeky Insider
No Result
View All Result

What is TOML? An easier way to configure Python apps and more

by Staff Writer
October 5, 2022
in Apps And Softwares, Articles
Reading Time: 4 mins read
Into to TOML: A more human-readable format for configuration files.
Share on FacebookShare on Twitter

It’s funny how some of the simplest software development decisions turn out to also be the toughest. One example is choosing the configuration file format for your application or service. Both JSON and YAML come to mind, of course. But if you need a config file format that’s easy to understand and relatively easy to parse into a data structure, you might consider TOML.

TOML, or Tom’s Obvious Minimal Language, was created chiefly for storing configuration data, with features that JSON and other formats lack. As an example, JSON doesn’t support inline comments. TOML lets you insert comments simply by prefixing them with a hash symbol, as you would in Python. Small wonder, then, that Python itself is gravitating toward TOML as a configuration standard. (See pyproject.toml used by pip to buld packages.)

Table of Contents

Toggle
    • READ ALSO
    • 120+ Best Creative Names for Team at Work
    • 10 Free Excel Template Ideas for Streamlining Your Task
  • TOML format basics
  • Arrays in TOML
  • Tables in TOML
  • Dotted names, inline tables, and table arrays
  • Using TOML in Python
  • TOML gotchas

READ ALSO

How to Write Interactive Facebook Posts

How to Write Interactive Facebook Posts

November 4, 2024
Comprehensive Guide on Absolute Function in Excel

Comprehensive Guide on Absolute Function in Excel

November 22, 2023

TOML format basics

TOML format files consist of key-value pairs, where keys are strings and values can be one of a number of types. In some ways it’s reminiscent of the Microsoft Windows .ini file format, but with support for a broader range of data types.

Here’s an example:

name = “string”
integer = 3
float-value = 3.14159
boolean_value = true
“quoted ünicode key” = true
# this is a comment
data = “OK” # a comment after a key/value

Keys are always interpreted as strings. Values can be strings, integers, floats, booleans, various types of date-time values, and two special kinds of values called arrays and inline tables. (More on these shortly.)

Everything after a hash symbol to the end of a line is a comment. This doesn’t include hash symbols that are part of key or value strings themselves.

Arrays in TOML

An array is a way to store multiple values in a single key:

int_values = [1, 2, 4, 8, 16]
strings = [“prime”, “audio”, “soup”]
mixed = [1, 2, “Three”, 4.0]
multi-line = [
“array”,
“of”,
“strings”
]

As you can guess, arrays don’t have to all contain the same type of value. And their definitions can span multiple lines if needed. In Python, arrays map directly to lists.

Tables in TOML

A table is a collection of key-value pairs in a TOML file, labeled with a header in square brackets. In Python, a table would be handled like a nested dictionary:

[general]
make_network_connection = true
ping_time = 1200

[user]
default_name = “Anonymous”
ping_time = 1600

The Microsoft Windows .ini file format has a similar feature, and with a similar function. It’s to allow groups of key-value pairs to have their own separate namespaces. For instance, the ping_time values for general and user are distinct in their own namespaces; one won’t overwrite the other.

Dotted names, inline tables, and table arrays

Another way to produce the same namespacing effect as tables in TOML is to use dotted names. Here’s how the above example could be rendered:

general.make_network_connection = true
general.ping_time = 1200

user.default_name = “Anonymous”
user.ping_time = 1600

Yet another way is with inline tables, which is a more compact style and may be more readable for some collections of values:

general = {make_network_connection = true, ping_time = 1200}
user = {default_name = “Anonymous”, ping_time = 1600}

Note that the inline table format may look superficially similar to a Python dictionary declaration, but it isn’t; it uses = instead of : to depict key/value pairs.

One more thing you can do with tables is create an array of tables, or a table array:

[[movies]]
name = “Blade Runner”
year = 1982
[[movies]]
name = “Blade Runner 2049”
year = 2017

This creates a nested structure, akin to the following in JSON:

movies = {
{name: “Blade Runner”, year: 1982},
{name: “Blade Runner 2049”, year: 2017}
}

Using TOML in Python

Because some parts of the Python ecosystem now use TOML as a configuration language, Python’s support for TOML is growing.

As of Python 3.11, a standard library module for TOML, tomllib provides a Python-native way to read TOML and parse it into Python objects, mainly dictionaries. However, that’s all tomllib does—it doesn’t serialize Python objects into TOML files, so it’s not suited to reading and writing TOML; only reading.

The Python core team might eventually add writing TOML as part of tomllib’s duties, but it’s not expected to happen anytime soon. In the meantime, the tomllib module is useful for ingesting and parsing TOML from configuration files without needing external libraries. If you need to serialize Python objects to TOML, there are a couple of third-party solutions:

  • tomli-w is a minimal library for writing out Python dictionaries to TOML. Note that it doesn’t perform automatic validation of the written TOML; for that you’ll want to load the data with tomllib and see if it validates.
  • tomlkit performs both reading from and writing out to TOML, so it’s a fairly complete solution, especially if you need to support Python versions earlier than 3.11 (which most people will).

TOML gotchas

Finally, there are a couple of gotchas to be careful about, especially if you’re writing TOML by hand:

  1. Watch out for directly mapping key names to program variable names that are not supported in the language you’re using. For instance, Python allows an underscore in a variable name, but not a dash, so the key float-value above could not be mapped directly to a variable with that name. However, it could be mapped to a key in a dictionary, since Python dictionary keys can be any string.

  2. Booleans in TOML use JSON rendering. One example is using JSON’s true and false rather than Python’s idiosyncractic True and False. TOML libraries shoud deal with this automatically, but be aware of it when authoring TOML by hand.

Copyright © 2022 IDG Communications, Inc.

Source by www.infoworld.com

Related Posts

How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots
Articles

How Drones and 360-Degree Cameras Are Changing Bridal Photoshoots

March 3, 2025
How to Download Pinterest Images and Videos.png
Apps And Softwares

How to Download Pinterest Images and Videos

November 4, 2024
How to Write Interactive Facebook Posts
Apps And Softwares

How to Write Interactive Facebook Posts

November 4, 2024
Comprehensive Guide on Absolute Function in Excel
Apps And Softwares

Comprehensive Guide on Absolute Function in Excel

November 22, 2023

Recommended.

Hotwav K2 Prime

[Flash File] Hotwav K2 Prime Firmware Download [Stock Rom]

October 5, 2022
Symbolic Link Header Image. A Windows folder on a blue background.

The Complete Guide to Creating Symbolic Links (aka Symlinks) on Windows

July 27, 2022

Trending.

No Content Available
  • Home
  • About
  • Contact
  • Privacy
  • Terms
  • DCMA
  • Write For Us / Submit
Contact us for submission queries. editor[at]geekyinsider.com.
No Result
View All Result
  • Home
  • Review
  • Apple
  • Gaming
  • Gadget and Gear
    • Camera
    • Smartphone
  • Microsoft
  • Security