GithubHelp home page GithubHelp logo

toml's Introduction

TOML

https://img.shields.io/pypi/v/toml https://travis-ci.org/uiri/toml.svg?branch=master

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"

toml.dump takes a dictionary and a file descriptor and returns a string containing the corresponding TOML-formatted data.

>>> with open('new_toml_file.toml', 'w') as f:
...     new_toml_string = toml.dump(parsed_toml, f)
>>> 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.

Note

For Numpy users, by default the data types np.floatX will not be translated to floats by toml, but will instead be encoded as strings. To get around this, specify the TomlNumpyEncoder when saving your data.

>>> import toml
>>> import numpy as np
>>> a = np.arange(0, 10, dtype=np.double)
>>> output = {'a': a}
>>> toml.dumps(output)
'a = [ "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "6.0", "7.0", "8.0", "9.0",]\n'
>>> toml.dumps(output, encoder=toml.TomlNumpyEncoder())
'a = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,]\n'

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, pathlib object, 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, encoder=None)

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
  • encoder: An instance of TomlEncoder (or subclass) for encoding the object. If None, will default to TomlEncoder
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, encoder=None)

Create a TOML-formatted string from an input object

Args:
  • o: An object to be converted into TOML
  • encoder: An instance of TomlEncoder (or subclass) for encoding the object. If None, will default to TomlEncoder
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.

toml's People

Contributors

uiri avatar samvasko avatar jdufresne avatar progval avatar jenselme avatar pkulev avatar thombashi avatar mraspberry avatar tharvik avatar x10an14 avatar nateprewitt avatar zed avatar pearcedavis avatar ryanhiebert avatar prajwalm2212 avatar nikolas avatar nicoe avatar jsancio avatar jpsca avatar mottosso avatar sivel avatar seifertm avatar mgorny avatar bobotig avatar niklasrosenstein avatar abred avatar huangsam avatar tmose1106 avatar takluyver avatar dirkphilip avatar

toml's Issues

What is this fork?

I was disappointed by the state of TOML libraries in python, so I decided to fork library which is better than others.

  • tomli/tomli_w: I don't get those arguments about splitting writer and reader. Everyone will question that decision after discovering that new shiny python3.11 tomllib has no dump() method. Also writer is kinda bad and inextensible from user's code, also has no support for pathlib objects encoding.
  • tomlkit: too difficult to understand and has no support for pathlib objects too.
  • other shinenigans that already died like qtoml, libtoml, pytoml.

Closest goals to achieve:

  • DONE get rid of python <= 3.6 compatibility code
  • DONE process pathlib objects in the base TomlEncoder
  • replace travis CI with Github Actions
  • make release on PyPI, for sure by different name, because @uiri is inaccesible for couple of years
  • TOML v1.0.0/1.1.0 spec support

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.