toml


Nametoml JSON
Version 0.10.2 PyPI version JSON
download
home_pagehttps://github.com/uiri/toml
SummaryPython Library for Tom's Obvious, Minimal Language
upload_time2020-11-01 01:40:22
maintainer
docs_urlNone
authorWilliam Pearson
requires_python>=2.6, !=3.0.*, !=3.1.*, !=3.2.*
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            ****
TOML
****

.. image:: https://img.shields.io/pypi/v/toml
    :target: https://pypi.org/project/toml/

.. image:: https://travis-ci.org/uiri/toml.svg?branch=master
    :target: https://travis-ci.org/uiri/toml

.. image:: https://img.shields.io/pypi/pyversions/toml.svg
    :target: https://pypi.org/project/toml/


A Python library for parsing and creating `TOML <https://en.wikipedia.org/wiki/TOML>`_.

The module passes `the TOML test suite <https://github.com/BurntSushi/toml-test>`_.

See also:

* `The TOML Standard <https://github.com/toml-lang/toml>`_
* `The currently supported TOML specification <https://github.com/toml-lang/toml/blob/v0.5.0/README.md>`_

Installation
============

To install the latest release on `PyPI <https://pypi.org/project/toml/>`_,
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.

.. code:: pycon

  >>> 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.

.. code:: pycon

  >>> 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.

.. code:: pycon

  >>> 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.

.. code:: pycon

  >>> 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, 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.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/uiri/toml",
    "name": "toml",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*",
    "maintainer_email": "",
    "keywords": "",
    "author": "William Pearson",
    "author_email": "uiri@xqz.ca",
    "download_url": "https://files.pythonhosted.org/packages/be/ba/1f744cdc819428fc6b5084ec34d9b30660f6f9daaf70eead706e3203ec3c/toml-0.10.2.tar.gz",
    "platform": "",
    "description": "****\nTOML\n****\n\n.. image:: https://img.shields.io/pypi/v/toml\n    :target: https://pypi.org/project/toml/\n\n.. image:: https://travis-ci.org/uiri/toml.svg?branch=master\n    :target: https://travis-ci.org/uiri/toml\n\n.. image:: https://img.shields.io/pypi/pyversions/toml.svg\n    :target: https://pypi.org/project/toml/\n\n\nA Python library for parsing and creating `TOML <https://en.wikipedia.org/wiki/TOML>`_.\n\nThe module passes `the TOML test suite <https://github.com/BurntSushi/toml-test>`_.\n\nSee also:\n\n* `The TOML Standard <https://github.com/toml-lang/toml>`_\n* `The currently supported TOML specification <https://github.com/toml-lang/toml/blob/v0.5.0/README.md>`_\n\nInstallation\n============\n\nTo install the latest release on `PyPI <https://pypi.org/project/toml/>`_,\nsimply run:\n\n::\n\n  pip install toml\n\nOr to install the latest development version, run:\n\n::\n\n  git clone https://github.com/uiri/toml.git\n  cd toml\n  python setup.py install\n\nQuick Tutorial\n==============\n\n*toml.loads* takes in a string containing standard TOML-formatted data and\nreturns a dictionary containing the parsed data.\n\n.. code:: pycon\n\n  >>> import toml\n  >>> toml_string = \"\"\"\n  ... # This is a TOML document.\n  ...\n  ... title = \"TOML Example\"\n  ...\n  ... [owner]\n  ... name = \"Tom Preston-Werner\"\n  ... dob = 1979-05-27T07:32:00-08:00 # First class dates\n  ...\n  ... [database]\n  ... server = \"192.168.1.1\"\n  ... ports = [ 8001, 8001, 8002 ]\n  ... connection_max = 5000\n  ... enabled = true\n  ...\n  ... [servers]\n  ...\n  ...   # Indentation (tabs and/or spaces) is allowed but not required\n  ...   [servers.alpha]\n  ...   ip = \"10.0.0.1\"\n  ...   dc = \"eqdc10\"\n  ...\n  ...   [servers.beta]\n  ...   ip = \"10.0.0.2\"\n  ...   dc = \"eqdc10\"\n  ...\n  ... [clients]\n  ... data = [ [\"gamma\", \"delta\"], [1, 2] ]\n  ...\n  ... # Line breaks are OK when inside arrays\n  ... hosts = [\n  ...   \"alpha\",\n  ...   \"omega\"\n  ... ]\n  ... \"\"\"\n  >>> parsed_toml = toml.loads(toml_string)\n\n\n*toml.dumps* takes a dictionary and returns a string containing the\ncorresponding TOML-formatted data.\n\n.. code:: pycon\n\n  >>> new_toml_string = toml.dumps(parsed_toml)\n  >>> print(new_toml_string)\n  title = \"TOML Example\"\n  [owner]\n  name = \"Tom Preston-Werner\"\n  dob = 1979-05-27T07:32:00Z\n  [database]\n  server = \"192.168.1.1\"\n  ports = [ 8001, 8001, 8002,]\n  connection_max = 5000\n  enabled = true\n  [clients]\n  data = [ [ \"gamma\", \"delta\",], [ 1, 2,],]\n  hosts = [ \"alpha\", \"omega\",]\n  [servers.alpha]\n  ip = \"10.0.0.1\"\n  dc = \"eqdc10\"\n  [servers.beta]\n  ip = \"10.0.0.2\"\n  dc = \"eqdc10\"\n\n*toml.dump* takes a dictionary and a file descriptor and returns a string containing the\ncorresponding TOML-formatted data.\n\n.. code:: pycon\n\n  >>> with open('new_toml_file.toml', 'w') as f:\n  ...     new_toml_string = toml.dump(parsed_toml, f)\n  >>> print(new_toml_string)\n  title = \"TOML Example\"\n  [owner]\n  name = \"Tom Preston-Werner\"\n  dob = 1979-05-27T07:32:00Z\n  [database]\n  server = \"192.168.1.1\"\n  ports = [ 8001, 8001, 8002,]\n  connection_max = 5000\n  enabled = true\n  [clients]\n  data = [ [ \"gamma\", \"delta\",], [ 1, 2,],]\n  hosts = [ \"alpha\", \"omega\",]\n  [servers.alpha]\n  ip = \"10.0.0.1\"\n  dc = \"eqdc10\"\n  [servers.beta]\n  ip = \"10.0.0.2\"\n  dc = \"eqdc10\"\n\nFor more functions, view the API Reference below.\n\nNote\n----\n\nFor 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.\n\n.. code:: pycon\n\n  >>> import toml\n  >>> import numpy as np\n  >>> a = np.arange(0, 10, dtype=np.double)\n  >>> output = {'a': a}\n  >>> toml.dumps(output)\n  'a = [ \"0.0\", \"1.0\", \"2.0\", \"3.0\", \"4.0\", \"5.0\", \"6.0\", \"7.0\", \"8.0\", \"9.0\",]\\n'\n  >>> toml.dumps(output, encoder=toml.TomlNumpyEncoder())\n  'a = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,]\\n'\n\nAPI Reference\n=============\n\n``toml.load(f, _dict=dict)``\n  Parse a file or a list of files as TOML and return a dictionary.\n\n  :Args:\n    * ``f``: A path to a file, list of filepaths (to be read into single\n      object) or a file descriptor\n    * ``_dict``: The class of the dictionary object to be returned\n\n  :Returns:\n    A dictionary (or object ``_dict``) containing parsed TOML data\n\n  :Raises:\n    * ``TypeError``: When ``f`` is an invalid type or is a list containing\n      invalid types\n    * ``TomlDecodeError``: When an error occurs while decoding the file(s)\n\n``toml.loads(s, _dict=dict)``\n  Parse a TOML-formatted string to a dictionary.\n\n  :Args:\n    * ``s``: The TOML-formatted string to be parsed\n    * ``_dict``: Specifies the class of the returned toml dictionary\n\n  :Returns:\n    A dictionary (or object ``_dict``) containing parsed TOML data\n\n  :Raises:\n    * ``TypeError``: When a non-string object is passed\n    * ``TomlDecodeError``: When an error occurs while decoding the\n      TOML-formatted string\n\n``toml.dump(o, f, encoder=None)``\n  Write a dictionary to a file containing TOML-formatted data\n\n  :Args:\n    * ``o``: An object to be converted into TOML\n    * ``f``: A File descriptor where the TOML-formatted output should be stored\n    * ``encoder``: An instance of ``TomlEncoder`` (or subclass) for encoding the object. If ``None``, will default to ``TomlEncoder``\n\n  :Returns:\n    A string containing the TOML-formatted data corresponding to object ``o``\n\n  :Raises:\n    * ``TypeError``: When anything other than file descriptor is passed\n\n``toml.dumps(o, encoder=None)``\n  Create a TOML-formatted string from an input object\n\n  :Args:\n    * ``o``: An object to be converted into TOML\n    * ``encoder``: An instance of ``TomlEncoder`` (or subclass) for encoding the object. If ``None``, will default to ``TomlEncoder``\n\n  :Returns:\n    A string containing the TOML-formatted data corresponding to object ``o``\n\n\n\nLicensing\n=========\n\nThis project is released under the terms of the MIT Open Source License. View\n*LICENSE.txt* for more information.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python Library for Tom's Obvious, Minimal Language",
    "version": "0.10.2",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "dc26cd71b80d6757139f38156a43c545",
                "sha256": "806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"
            },
            "downloads": -1,
            "filename": "toml-0.10.2-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dc26cd71b80d6757139f38156a43c545",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*",
            "size": 16588,
            "upload_time": "2020-11-01T01:40:20",
            "upload_time_iso_8601": "2020-11-01T01:40:20.672915Z",
            "url": "https://files.pythonhosted.org/packages/44/6f/7120676b6d73228c96e17f1f794d8ab046fc910d781c8d151120c3f1569e/toml-0.10.2-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "59bce5d8d67e858735ec3f399ec90253",
                "sha256": "b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"
            },
            "downloads": -1,
            "filename": "toml-0.10.2.tar.gz",
            "has_sig": false,
            "md5_digest": "59bce5d8d67e858735ec3f399ec90253",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*",
            "size": 22253,
            "upload_time": "2020-11-01T01:40:22",
            "upload_time_iso_8601": "2020-11-01T01:40:22.204866Z",
            "url": "https://files.pythonhosted.org/packages/be/ba/1f744cdc819428fc6b5084ec34d9b30660f6f9daaf70eead706e3203ec3c/toml-0.10.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-11-01 01:40:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "uiri",
    "github_project": "toml",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "toml"
}
        
Elapsed time: 0.03645s