README.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ****
  2. TOML
  3. ****
  4. .. image:: https://img.shields.io/pypi/v/toml
  5. :target: https://pypi.org/project/toml/
  6. .. image:: https://travis-ci.org/uiri/toml.svg?branch=master
  7. :target: https://travis-ci.org/uiri/toml
  8. .. image:: https://img.shields.io/pypi/pyversions/toml.svg
  9. :target: https://pypi.org/project/toml/
  10. A Python library for parsing and creating `TOML <https://en.wikipedia.org/wiki/TOML>`_.
  11. The module passes `the TOML test suite <https://github.com/BurntSushi/toml-test>`_.
  12. See also:
  13. * `The TOML Standard <https://github.com/toml-lang/toml>`_
  14. * `The currently supported TOML specification <https://github.com/toml-lang/toml/blob/v0.5.0/README.md>`_
  15. Installation
  16. ============
  17. To install the latest release on `PyPI <https://pypi.org/project/toml/>`_,
  18. simply run:
  19. ::
  20. pip install toml
  21. Or to install the latest development version, run:
  22. ::
  23. git clone https://github.com/uiri/toml.git
  24. cd toml
  25. python setup.py install
  26. Quick Tutorial
  27. ==============
  28. *toml.loads* takes in a string containing standard TOML-formatted data and
  29. returns a dictionary containing the parsed data.
  30. .. code:: pycon
  31. >>> import toml
  32. >>> toml_string = """
  33. ... # This is a TOML document.
  34. ...
  35. ... title = "TOML Example"
  36. ...
  37. ... [owner]
  38. ... name = "Tom Preston-Werner"
  39. ... dob = 1979-05-27T07:32:00-08:00 # First class dates
  40. ...
  41. ... [database]
  42. ... server = "192.168.1.1"
  43. ... ports = [ 8001, 8001, 8002 ]
  44. ... connection_max = 5000
  45. ... enabled = true
  46. ...
  47. ... [servers]
  48. ...
  49. ... # Indentation (tabs and/or spaces) is allowed but not required
  50. ... [servers.alpha]
  51. ... ip = "10.0.0.1"
  52. ... dc = "eqdc10"
  53. ...
  54. ... [servers.beta]
  55. ... ip = "10.0.0.2"
  56. ... dc = "eqdc10"
  57. ...
  58. ... [clients]
  59. ... data = [ ["gamma", "delta"], [1, 2] ]
  60. ...
  61. ... # Line breaks are OK when inside arrays
  62. ... hosts = [
  63. ... "alpha",
  64. ... "omega"
  65. ... ]
  66. ... """
  67. >>> parsed_toml = toml.loads(toml_string)
  68. *toml.dumps* takes a dictionary and returns a string containing the
  69. corresponding TOML-formatted data.
  70. .. code:: pycon
  71. >>> new_toml_string = toml.dumps(parsed_toml)
  72. >>> print(new_toml_string)
  73. title = "TOML Example"
  74. [owner]
  75. name = "Tom Preston-Werner"
  76. dob = 1979-05-27T07:32:00Z
  77. [database]
  78. server = "192.168.1.1"
  79. ports = [ 8001, 8001, 8002,]
  80. connection_max = 5000
  81. enabled = true
  82. [clients]
  83. data = [ [ "gamma", "delta",], [ 1, 2,],]
  84. hosts = [ "alpha", "omega",]
  85. [servers.alpha]
  86. ip = "10.0.0.1"
  87. dc = "eqdc10"
  88. [servers.beta]
  89. ip = "10.0.0.2"
  90. dc = "eqdc10"
  91. *toml.dump* takes a dictionary and a file descriptor and returns a string containing the
  92. corresponding TOML-formatted data.
  93. .. code:: pycon
  94. >>> with open('new_toml_file.toml', 'w') as f:
  95. ... new_toml_string = toml.dump(parsed_toml, f)
  96. >>> print(new_toml_string)
  97. title = "TOML Example"
  98. [owner]
  99. name = "Tom Preston-Werner"
  100. dob = 1979-05-27T07:32:00Z
  101. [database]
  102. server = "192.168.1.1"
  103. ports = [ 8001, 8001, 8002,]
  104. connection_max = 5000
  105. enabled = true
  106. [clients]
  107. data = [ [ "gamma", "delta",], [ 1, 2,],]
  108. hosts = [ "alpha", "omega",]
  109. [servers.alpha]
  110. ip = "10.0.0.1"
  111. dc = "eqdc10"
  112. [servers.beta]
  113. ip = "10.0.0.2"
  114. dc = "eqdc10"
  115. For more functions, view the API Reference below.
  116. Note
  117. ----
  118. 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.
  119. .. code:: pycon
  120. >>> import toml
  121. >>> import numpy as np
  122. >>> a = np.arange(0, 10, dtype=np.double)
  123. >>> output = {'a': a}
  124. >>> toml.dumps(output)
  125. 'a = [ "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "6.0", "7.0", "8.0", "9.0",]\n'
  126. >>> toml.dumps(output, encoder=toml.TomlNumpyEncoder())
  127. 'a = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,]\n'
  128. API Reference
  129. =============
  130. ``toml.load(f, _dict=dict)``
  131. Parse a file or a list of files as TOML and return a dictionary.
  132. :Args:
  133. * ``f``: A path to a file, list of filepaths (to be read into single
  134. object) or a file descriptor
  135. * ``_dict``: The class of the dictionary object to be returned
  136. :Returns:
  137. A dictionary (or object ``_dict``) containing parsed TOML data
  138. :Raises:
  139. * ``TypeError``: When ``f`` is an invalid type or is a list containing
  140. invalid types
  141. * ``TomlDecodeError``: When an error occurs while decoding the file(s)
  142. ``toml.loads(s, _dict=dict)``
  143. Parse a TOML-formatted string to a dictionary.
  144. :Args:
  145. * ``s``: The TOML-formatted string to be parsed
  146. * ``_dict``: Specifies the class of the returned toml dictionary
  147. :Returns:
  148. A dictionary (or object ``_dict``) containing parsed TOML data
  149. :Raises:
  150. * ``TypeError``: When a non-string object is passed
  151. * ``TomlDecodeError``: When an error occurs while decoding the
  152. TOML-formatted string
  153. ``toml.dump(o, f, encoder=None)``
  154. Write a dictionary to a file containing TOML-formatted data
  155. :Args:
  156. * ``o``: An object to be converted into TOML
  157. * ``f``: A File descriptor where the TOML-formatted output should be stored
  158. * ``encoder``: An instance of ``TomlEncoder`` (or subclass) for encoding the object. If ``None``, will default to ``TomlEncoder``
  159. :Returns:
  160. A string containing the TOML-formatted data corresponding to object ``o``
  161. :Raises:
  162. * ``TypeError``: When anything other than file descriptor is passed
  163. ``toml.dumps(o, encoder=None)``
  164. Create a TOML-formatted string from an input object
  165. :Args:
  166. * ``o``: An object to be converted into TOML
  167. * ``encoder``: An instance of ``TomlEncoder`` (or subclass) for encoding the object. If ``None``, will default to ``TomlEncoder``
  168. :Returns:
  169. A string containing the TOML-formatted data corresponding to object ``o``
  170. Licensing
  171. =========
  172. This project is released under the terms of the MIT Open Source License. View
  173. *LICENSE.txt* for more information.