Skip to main content

Python Library for Tom's Obvious, Minimal Language

Project description

https://badge.fury.io/py/toml.svg https://travis-ci.org/uiri/toml.svg?branch=master https://img.shields.io/pypi/pyversions/toml.svg

A Python library for parsing and creating TOML.

The module passes the TOML test suite.

See also:

Installation

To install the latest release on PyPI, simply run:

pip install toml

Or to install the latest development version, run:

git clone https://github.com/uiri/toml.git
cd toml
python setup.py install

Quick Tutorial

toml.loads takes in a string containing standard TOML-formatted data and returns a dictionary containing the parsed data.

>>> import toml
>>> toml_string = """
... # This is a TOML document.
...
... title = "TOML Example"
...
... [owner]
... name = "Tom Preston-Werner"
... dob = 1979-05-27T07:32:00-08:00 # First class dates
...
... [database]
... server = "192.168.1.1"
... ports = [ 8001, 8001, 8002 ]
... connection_max = 5000
... enabled = true
...
... [servers]
...
...   # Indentation (tabs and/or spaces) is allowed but not required
...   [servers.alpha]
...   ip = "10.0.0.1"
...   dc = "eqdc10"
...
...   [servers.beta]
...   ip = "10.0.0.2"
...   dc = "eqdc10"
...
... [clients]
... data = [ ["gamma", "delta"], [1, 2] ]
...
... # Line breaks are OK when inside arrays
... hosts = [
...   "alpha",
...   "omega"
... ]
... """
>>> parsed_toml = toml.loads(toml_string)

toml.dumps takes a dictionary and returns a string containing the corresponding TOML-formatted data.

>>> new_toml_string = toml.dumps(parsed_toml)
>>> print(new_toml_string)
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00Z
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002,]
connection_max = 5000
enabled = true
[clients]
data = [ [ "gamma", "delta",], [ 1, 2,],]
hosts = [ "alpha", "omega",]
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"

For more functions, view the API Reference below.

API Reference

toml.load(f, _dict=dict)

Parse a file or a list of files as TOML and return a dictionary.

Args:
  • f: A path to a file, list of filepaths (to be read into single object) or a file descriptor

  • _dict: The class of the dictionary object to be returned

Returns:

A dictionary (or object _dict) containing parsed TOML data

Raises:
  • TypeError: When f is an invalid type or is a list containing invalid types

  • TomlDecodeError: When an error occurs while decoding the file(s)

toml.loads(s, _dict=dict)

Parse a TOML-formatted string to a dictionary.

Args:
  • s: The TOML-formatted string to be parsed

  • _dict: Specifies the class of the returned toml dictionary

Returns:

A dictionary (or object _dict) containing parsed TOML data

Raises:
  • TypeError: When a non-string object is passed

  • TomlDecodeError: When an error occurs while decoding the TOML-formatted string

toml.dump(o, f)

Write a dictionary to a file containing TOML-formatted data

Args:
  • o: An object to be converted into TOML

  • f: A File descriptor where the TOML-formatted output should be stored

Returns:

A string containing the TOML-formatted data corresponding to object o

Raises:
  • TypeError: When anything other than file descriptor is passed

toml.dumps(o)

Create a TOML-formatted string from an input object

Args:
  • o: An object to be converted into TOML

Returns:

A string containing the TOML-formatted data corresponding to object o

Licensing

This project is released under the terms of the MIT Open Source License. View LICENSE.txt for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toml-0.9.6.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

toml-0.9.6-py2.py3-none-any.whl (14.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file toml-0.9.6.tar.gz.

File metadata

  • Download URL: toml-0.9.6.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/38.2.4 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/2.7.9

File hashes

Hashes for toml-0.9.6.tar.gz
Algorithm Hash digest
SHA256 380178cde50a6a79f9d2cf6f42a62a5174febe5eea4126fe4038785f1d888d42
MD5 efbe91ac7bec4e2ff23dd7d69cd7bb97
BLAKE2b-256 0ee81aa958599e5326b690a31334112da68a9b75e7563879e2c5103ca219d30a

See more details on using hashes here.

File details

Details for the file toml-0.9.6-py2.py3-none-any.whl.

File metadata

  • Download URL: toml-0.9.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/38.2.4 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/2.7.9

File hashes

Hashes for toml-0.9.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a7901919d3e4f92ffba7ff40a9d697e35bbbc8a8049fe8da742f34c83606d957
MD5 71f7dbe9f7fa164a618eaf8da2fae6ec
BLAKE2b-256 c71976c3cb84949a0593767b32b9be83a604d8a68c3580ff5d0ee64856b39ade

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page