它使使用pyyaml更加方便。
enhancedyaml的Python项目详细描述
增强的pyyaml
它使pyyaml更加方便,并且避免了重复的yaml数据。
功能如下:
- 按deafult,使用类的名称作为yamlobject的yaml_标记
- 将锚加载为yamlobject实例的属性
- 将yamlobject实例的属性anchor转储为anchor&;name
安装
您可以通过pypi安装它,
sudo pip install enhancedyaml
或者手动下载。
示例
这是yaml文件:
# examples/enhanced_data.yaml %YAML 1.1 --- examples: - !Example &first data: I am the first one. - !Example &second data: I am the second one. order: - \*first - \*second
创建一个yamlobject而不指定yaml标签
import enhancedyaml
class Example(enhancedyaml.YAMLObject): pass
加载的yaml
代码:
from pprint import pprint data = enhancedyaml.load(open('data/enhanced_data.yaml')) pprint(data)
输出:
{'examples': [<__main__.Example object at 0x7fa825fb4fd0>, <__main__.Example object at 0x7fa825fb4cd0>], 'order': [<__main__.Example object at 0x7fa825fb4fd0>, <__main__.Example object at 0x7fa825fb4cd0>]}
数据的内容['order']
代码:
pprint(list(example.__dict__ for example in data['order']))
输出:
[{'anchor': u'first', 'data': 'I am the first one.'}, {'anchor': u'second', 'data': 'I am the second one.'}]
示例具有addnational属性锚。
再次转储数据
代码:
print enhancedyaml.dump(data, default_flow_style=False)
输出:
examples: - &first !Example data: I am the first one. - &second !Example data: I am the second one. order: - \*first - \*second
它几乎和原来的山药一样。
转储运行时生成的对象
代码:
e1 = Example() e2 = Example() e1.data = "I don't have `anchor`." e2.data = "I don't have `anchor`, too." es = [e1, e2, e2, e1] print enhancedyaml.dump(es, default_flow_style=False)
输出:
- &example002 !Example data: I don't have `anchor`. - &example001 !Example data: I don't have `anchor`, too. - \*example001 - \*example002
您可以在enhancedyaml/examples目录中找到更多示例。
玩得开心!