Last active
August 5, 2022 00:57
-
-
Save thuwarakeshm/162c3149d5122aa96931d58b212a9226 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
import tomllib | |
except ModuleNotFoundError: | |
import tomli as tomllib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a TOML document | |
title = "TOML Example" | |
[owner] | |
name = "Tom Preston-Werner" | |
dob = 1979-05-27T07:32:00-08:00 | |
[database] | |
enabled = true | |
ports = [ 8000, 8001, 8002 ] | |
data = [ ["delta", "phi"], [3.14] ] | |
temp_targets = { cpu = 79.5, case = 72.0 } | |
[servers] | |
[servers.alpha] | |
ip = "10.0.0.1" | |
role = "frontend" | |
[servers.beta] | |
ip = "10.0.0.2" | |
role = "backend" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toml_str = """ | |
# This is a TOML document | |
title = "TOML Example" | |
[owner] | |
name = "Tom Preston-Werner" | |
dob = 1979-05-27T07:32:00-08:00 | |
[database] | |
enabled = true | |
ports = [ 8000, 8001, 8002 ] | |
data = [ ["delta", "phi"], [3.14] ] | |
temp_targets = { cpu = 79.5, case = 72.0 } | |
[servers] | |
[servers.alpha] | |
ip = "10.0.0.1" | |
role = "frontend" | |
[servers.beta] | |
ip = "10.0.0.2" | |
role = "backend" | |
""" | |
data = tomllib.loads(toml_str) | |
data | |
# {'title': 'TOML Example', 'owner': {'name': 'Tom Preston-Werner', 'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600)))}, 'database': {'enabled': True, 'ports': [8000, 8001, 8002], 'data': [['delta', 'phi'], [3.14]], 'temp_targets': {'cpu': 79.5, 'case': 72.0}}, 'servers': {'alpha': {'ip': '10.0.0.1', 'role': 'frontend'}, 'beta': {'ip': '10.0.0.2', 'role': 'backend'}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tomli as tomllib | |
with open("./config.toml", "rb") as f: | |
data = tomllib.load(f) | |
data | |
# {'title': 'TOML Example', 'owner': {'name': 'Tom Preston-Werner', 'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600)))}, 'database': {'enabled': True, 'ports': [8000, 8001, 8002], 'data': [['delta', 'phi'], [3.14]], 'temp_targets': {'cpu': 79.5, 'case': 72.0}}, 'servers': {'alpha': {'ip': '10.0.0.1', 'role': 'frontend'}, 'beta': {'ip': '10.0.0.2', 'role': 'backend'}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment