支持YAML序列化的字典,具有双项目属性访问器
项目描述
yamlns.namespace
一个扩展字典,可以方便地通过直接映射从YAML和其他结构化格式访问您的结构化数据。除了像['field']
这样的项目访问之外,还提供了像.field
这样的属性访问。它还提供了许多其他功能。
- 使用
dump()
和load()
方法直接映射到YAML。 - 在YAML和Python之间映射值类型时,提供了对纯YAML规范的方便变体。
- 内部YAML映射(
dict
)也被加载为namespace
,而不是Pythondict
。 - 命名空间保留了插入顺序,因为它们基于
odict
。这样,当存储时,插入顺序和原始加载文件的顺序被保留。 - YAML浮点数被加载为
Decimal
,而Decimal
对象则存储为常规YAML浮点数。这避免了在交替加载/存储周期中丢失精度。 - YAML日期映射到
datetime.date
的扩展,它提供了作为属性的输出格式,方便在format
模板中调用。
- 内部YAML映射(
- 用于对具有复杂命名空间结构的模板进行格式化的工具。
- 由于提供了属性访问,因此多级字典的
format
模板结果更简洁。 - 给定一个具有替换的模板,提取空YAML骨架的函数。
- 填充
format
模板(如文件)的YAML文件的函数。 - 命令行工具,用于运行这两个功能
- 由于提供了属性访问,因此多级字典的
unittest
断言assertNsEqual
用于比较它们之间或与 YAML 字符串的类似 JSON 结构,并以漂亮的逐行差异显示差异。assertNsContains
确保类似 JSON 的结构是期望的超集
pyunit
集成pytestutils.assert_ns_equal
:与assertNsEqual
等效,用于 pytestpytestutils.assert_ns_contains
:与assertNsContains
等效,用于 pytestpytestutils.yaml_snapshot
:用于在 YAML 格式之间检测测试执行之间结构变化的 fixture。pytestutils.text_snapshot
:用于检测测试执行之间文本变化的 fixture。
示例
>>> from yamlns import namespace as ns
>>> n = ns()
>>> n.attribute1 = "value1"
>>> ns['attribute2'] = "value2"
>>> print(n.dump())
attribute1: value1
attribute2: value2
>>> n.attribute2
'value2'
>>> n['attribute1']
'value1'
>>> n.update(ns.loads("""
... attribute3: value3
... attribute4:
... attribute5: [ 4,3,2,value5 ]
... attribute6: 2015-09-23
... attribute7:
... - value7.1
... - value7.2
... """))
>>> n.attribute4.attribute5
[4, 3, 2, 'value5']
>>> n.attribute4.attribute6
datetime.date(2015,9,23)
>>> n.attribute7
['value7.1', 'value7.2']
模板示例
>>> template = (
... "{client.name} {client.midname[0]}. {client.surname} buys {item.name} "
... "by {item.price.amount:0.02f} {item.price.coin}."
... )
...
>>> print(ns.fromTemplate(template).dump())
client:
name: ''
midname: ''
surname: ''
item:
name: ''
price:
amount: ''
coin: ''
>>> template.format(**ns.loads("""
client:
name: 'John'
midname: 'Archivald'
surname: 'Doe'
item:
name: 'Apples'
price:
amount: 30
coin: 'dollars'
"""))
John A. Doe buys Apples by 30.00 dollars.
命令行工具使用方法
nstemplate apply <template> <yamlfile> <output>
nstemplate extract <template> <yamlskeleton>
cat file.json | json2yaml > file.yaml
测试结构内容
class MyTest(unittest.TestCase):
from yamlns.testutils import assertNsEqual, assertNsContains
def test(self):
data = dict(letters = dict(
(letter, i) for i,letter in enumerate('murcielago'))
)
self.assertNsEqual(data, """\
letters:
a: 7
c: 3
e: 5
g: 8
i: 4
l: 6
m: 0
o: 9
r: 2
u: 1
""")
# Data is a superset of the expectation
self.assertNsContains(data, """\
letters:
a: 7
e: 5
i: 4
o: 9
u: 1
""")
Pytest 集成
以下为 pytest 提供的辅助工具
pytestutils.assert_ns_equal
:与assertNsEqual
等效,用于 pytestpytestutils.assert_ns_contains
:与assertNsContains
等效,用于 pytestpytestutils.yaml_snapshot
:用于在 YAML 格式之间检测测试执行之间结构变化的 fixture。pytestutils.text_snapshot
:用于检测测试执行之间文本变化的 fixture。
assert_ns_equal
一个自定义断言,将两边都归一化为命名空间,并作为 YAML 输出,以逐行比较。
归一化过程如下,首先如果数据是字符串,它被解析为 YAML。然后,结果数据递归地转换为命名空间,按字母顺序排序键。最后,结果以 YAML 格式输出以进行比较。
from yamlns.pytestutils import assert_ns_equal
def test_with_assert_ns_equal():
data = dict(hello='world')
assert_ns_equal(data, """\
hello: world
""")
assert_ns_contains
一个类似于 assert_ns_equal
的自定义断言,但忽略期望中不存在的任何键。
from yamlns.pytestutils import assert_ns_equal
def test_with_assert_ns_equal():
data = dict(hello='world', ignored=data)
assert_ns_equal(data, """\
hello: world
""")
yaml_snapshot
和 text_snapshot
yaml_snapshot
和 text_snapshot
是 yamlns 安装时始终可用的 fixture。您可以使用它来生成可以与先前执行比较的数据快照。快照存储在 testdata/snapshots/
中,并以测试的完全限定名称命名。具有 .expected
后缀的快照是接受的快照,而以 .result
结尾的快照是在当前执行与预期不匹配时生成的。
如果您认为 .result
是有效的,只需将其重命名为 .expected
。为了方便起见,断言消息指示执行重命名的命令行。
text_snapshot
仅输出文本,而 yaml_snapshot
则比较数据规范化输出的类似 assert_ns_equal
。
def test_with_yaml_snapshot(yaml_snapshot):
data = dict(hello='world')
yaml_snapshot(data)
def test_with_text_snapshot(text_snapshot):
who = 'world'
text_snapshot('hello {}'.format(who))
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。
源分布
yamlns-0.11.0.tar.gz (20.5 kB 查看散列)
构建分布
yamlns-0.11.0-py3-none-any.whl (20.5 kB 查看散列)
关闭
yamlns-0.11.0.tar.gz 的散列
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d5cad27ada26d0872c4124c87601a71150e75f699ff347f53f1c2ec3b329de49 |
|
MD5 | f6cad21e29157ece67003aa0f9721512 |
|
BLAKE2b-256 | 44b43b8b64cb198d6bffbee674687043ad1dd1b40193c0de1d93efe689641e65 |
关闭
yamlns-0.11.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | abc4b013bf82670c5d6f0d70afb286037f48762b52942bc4d7e1c0274844ca50 |
|
MD5 | 7a3e6603ef526d663ce096a92cc99cd8 |
|
BLAKE2b-256 | e1c2cec459e3a0deda1570220f1481014d5fe480d129be10bc0571432a6a5e72 |