跳转到主要内容

又一款保留样式的TOML库

项目描述

PyPI Version Python Versions License Github Actions codecov

ATOML - 另一个适用于Python的保留样式的TOML库

ATOML是一个符合TOML 1.0.0rc1规范的库。

它包含一个解析器,可以保留所有注释、缩进、空白和内部元素顺序,并通过直观的API提供访问和编辑。

您还可以使用提供的辅助程序从头开始创建新的TOML文档。

该名称来源于著名的日本卡通人物 铁臂阿童木(Atom)

实现变更:从1.0版本开始,ATOML是基于tomlkit v0.7.0的分支,具有更少的错误和不一致性。

使用方法

解析

ATOML提供了一个快速且保留样式的解析器,帮助您访问TOML文件和字符串的内容。

>>> from atoml import dumps
>>> from atoml import parse  # you can also use loads

>>> content = """[table]
... foo = "bar"  # String
... """
>>> doc = parse(content)

# doc is a TOMLDocument instance that holds all the information
# about the TOML string.
# It behaves like a standard dictionary.

>>> assert doc["table"]["foo"] == "bar"

# The string generated from the document is exactly the same
# as the original string
>>> assert dumps(doc) == content

修改

ATOML提供了一个直观的API来修改TOML文档。

>>> from atoml import dumps
>>> from atoml import parse
>>> from atoml import table

>>> doc = parse("""[table]
... foo = "bar"  # String
... """)

>>> doc["table"]["baz"] = 13

>>> dumps(doc)
"""[table]
foo = "bar"  # String
baz = 13
"""

# Add a new table
>>> tab = table()
>>> tab.add("array", [1, 2, 3])

>>> doc["table2"] = tab

>>> dumps(doc)
"""[table]
foo = "bar"  # String
baz = 13

[table2]
array = [1, 2, 3]
"""

# Remove the newly added table
>>> doc.remove("table2")
# del doc["table2] is also possible

编写

您还可以从头开始编写新的TOML文档。

假设我们想创建以下文档

# This is a TOML document.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z # First class dates? Why not?

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true

可以使用以下代码创建它

>>> from atoml import comment
>>> from atoml import document
>>> from atoml import nl
>>> from atoml import table

>>> doc = document()
>>> doc.add(comment("This is a TOML document."))
>>> doc.add(nl())
>>> doc.add("title", "TOML Example")
# Using doc["title"] = "TOML Example" is also possible

>>> owner = table()
>>> owner.add("name", "Tom Preston-Werner")
>>> owner.add("organization", "GitHub")
>>> owner.add("bio", "GitHub Cofounder & CEO\nLikes tater tots and beer.")
>>> owner.add("dob", datetime(1979, 5, 27, 7, 32, tzinfo=utc))
>>> owner["dob"].comment("First class dates? Why not?")

# Adding the table to the document
>>> doc.add("owner", owner)

>>> database = table()
>>> database["server"] = "192.168.1.1"
>>> database["ports"] = [8001, 8001, 8002]
>>> database["connection_max"] = 5000
>>> database["enabled"] = True

>>> doc["database"] = database

安装

如果您正在使用PDM,请使用以下方法将atoml添加到您的pyproject.toml文件中

pdm add atoml

如果不使用PDM,则可以使用pip

pip install atoml

从TOMLKit迁移

ATOML 具有与 TOMLKit 完全兼容的 API,您可以将 tomlkit 全部替换为 atoml

import atoml as tomlkit

ATOML 与 TOMLkit 有以下区别

  • 仅支持 Python 3.6+
  • 表格和数组分别是 MutableMappingMutableSequence 的子类,以减少容器行为的某些不一致性
  • 增加了 loaddump 方法
  • 更少的错误

项目详情


下载文件

下载您平台上的文件。如果您不确定要选择哪个,请了解更多关于 安装包 的信息。

源分发

atoml-1.1.1.tar.gz (138.2 kB 查看哈希值)

上传时间

构建分发

atoml-1.1.1-py3-none-any.whl (32.1 kB 查看哈希值)

上传时间 Python 3

支持者