具有双项和属性访问器的yaml可序列化字典
yamlns的Python项目详细描述
一种有序字典,其值可以作为项或作为 属性,类似于javascript对象,但使用了pythonic sugar和yaml I/O.
它还提供一些好处:
- 使用dump()和load()方法直接映射到yaml。
- 在
在yaml和python之间映射值类型的方式:
- 内部yaml映射(dicts)加载为namespaces 而不是pythondict。
- 名称空间保留插入顺序,因为它们基于 odict。这样插入顺序和 存储时保留原始加载的文件。
- yaml float加载为Decimal和Decimal对象 作为普通的山药漂浮物储存。这样可以避免在 连续的加载/存储周期是交替的。
- yaml日期映射到datetime.date的扩展名,该扩展名 提供输出格式作为方便调用的属性 在format模板中。
- 具有复杂命名空间结构的format模板的工具。
- 给定属性如accessformattemplates result 更干净。
- 用yaml文件填充format类模板文件的api。
- 在给定模板的情况下提取空yaml scheletton的api 替代品。
- 用于执行这两个功能的命令行工具
示例
>>>fromyamlnsimportnamespaceasns>>>n=ns()>>>n.attribute1="value1">>>ns['attribute2']="value2">>>print(n.dump())attribute1:value1attribute2: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.attribute6datetime.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' """))JohnA.DoebuysApplesby30.00dollars.
命令行工具的使用
nstemplate apply <template> <yamlfile> <output> nstemplate extract <template> <yamlskeleton>