YAML是YAML Ain’t Markup Language (YAML™)的缩写。汉语意思是YAML不是一种标记语言...
Python使用需要安装PyYaml模块
pip install pyyaml
书写格式:
1、YAML大小写敏感;
2、使用缩进代表层级关系,缩进只能使用空格,不能使用TAB;
3、相同层级左对齐;
4、只有注释行语法,使用 # 注释
支持的数据格式:
1、对象,使用键值对的数据。字典、哈希
2、数组,一组值的集合。列表
3、常量,单个值。字符串(str)、布尔值(bool)、整数(int)、浮点数(float)、Null、时间(time)、日期(date)
语法:
1、字典格式,Key: value,value前必须加空格
webserver: ip: 192.168.1.1 port: 8000 create_date: 2019-06-09 create_time:1:01:01:01 # 结果: { webserver: { ip: '192.168.1.1', port: 8000, create_date: Sun Jun 09 2019 08:00:00 GMT+0800 (中国标准时间), create_time: 219661 } }
2、列表格式,- value,-value前必须加空格
ip: - 192.168.1.1 - 192.168.1.2 - 192.168.1.3 - 192.168.1.4 # 结果: { ip: [ '192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4' ] }
3、列表和字典互相嵌套
webserver: - ip: - 192.168.1.1 - 192.168.1.2 - 192.168.1.3 - 192.168.1.4 - port: 8000 # 列表和字典嵌套 # 结果: { webserver: [ { ip: [ '192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4' ] }, { port: 8000 } ] }
4、!! 强制数据类型转换
port: !!str 8000 num: !!int '1999' boolean: !!bool 'true' second: !!float '18.362' # yaml中,强制转换时,只是给字符串加引号,去引号,如果去引号后的值和要求的类型不符,转换报错。 # 结果 { port: '8000', num: 1999, boolean: true, second: 18.362 }
5、定义锚点&和引用,&和*不能作为行首使用。作用是重复引用相同的值
port: &webport # 定义锚点,&不可用在行首 - 8001 - 8002 server: ip: 192.168.1.1 port: *webport # 引用 public: addr: &addr1 liaoning # 定义锚点 address: # 引用 - *addr1 结果: { port: [ 8001, 8002 ], server: [ '192.168.1.1', [ 8001, 8002 ] ], public: null, addr: 'liaoning', address: [ 'liaoning' ] }
6、 合并,<<,配合锚点使用,合并成一个字典
merge: - &CENTER { x: 1, y: 2 } - &LEFT { x: 0, y: 2 } - &BIG { r: 10 } - &SMALL { r: 1 } sample1: <<: *CENTER r: 10 sample2: << : [ *CENTER, *BIG ] other: haha sample3: << : [ *CENTER, *BIG ] r: 100 s: 6 # 结果: { merge: [ { x: 1, y: 2 }, { x: 0, y: 2 }, { r: 10 }, { r: 1 } ], sample1: { x: 1, y: 2, r: 10 }, sample2: { x: 1, y: 2, r: 10, other: 'haha' }, sample3: { x: 1, y: 2, r: 100, s: 6 } }
7、定义格式符号,>和|
segment_enter: > # >符号,只保留段落最后一个回车 Mark set a major league home run record in 1998. line_enter: | # |符号,所见即所得,保留每行的回车 65 Home Runs 0.278 Batting Average # 结果: { segment_enter: 'Mark set a major league home run record in 1998.\n', line_enter: '65 Home Runs\n0.278 Batting Average\n' }
8、---把内容分割成多个文档,以下例子相当于两个文件。
--- port: &webport # 定义锚点,&不可用在行首 - 8001 - 8002 server: ip: 192.168.1.1 port: *webport # 引用 --- public: addr: &addr1 liaoning # 定义锚点 address: # 引用 - *addr1
9、app自动化测试配置用例(来源于网络):
# Test using included Django test app # First install python-django # Then launch the app in another terminal by doing # cd testapp # python manage.py testserver test_data.json # Once launched, tests can be executed via: # python resttest.py http://localhost:8000 miniapp-test.yaml --- - config: - testset: "Tests using test app" - test: # create entity - name: "Basic get" - url: "/api/person/" - test: # create entity - name: "Get single person" - url: "/api/person/1/" - test: # create entity - name: "Get single person" - url: "/api/person/1/" - method: 'DELETE' - test: # create entity by PUT - name: "Create/update person" - url: "/api/person/1/" - method: "PUT" - body: '{"first_name": "Gaius","id": 1,"last_name": "Baltar","login": "gbaltar"}' - headers: {'Content-Type': 'application/json'} - test: # create entity by POST - name: "Create person" - url: "/api/person/" - method: "POST" - body: '{"first_name": "Willim","last_name": "Adama","login": "theadmiral"}' - headers: {Content-Type: application/json}