YAML是YAML Ain’t Markup Language (YAML?)的縮寫。漢語(yǔ)意思是YAML不是一種標(biāo)記語(yǔ)言...
成都創(chuàng)新互聯(lián)是一家專業(yè)提供都昌企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為都昌眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
Python使用需要安裝PyYaml模塊
pip install pyyaml
書寫格式:
1、YAML大小寫敏感;
2、使用縮進(jìn)代表層級(jí)關(guān)系,縮進(jìn)只能使用空格,不能使用TAB;
3、相同層級(jí)左對(duì)齊;
4、只有注釋行語(yǔ)法,使用 # 注釋
支持的數(shù)據(jù)格式:
1、對(duì)象,使用鍵值對(duì)的數(shù)據(jù)。字典、哈希
2、數(shù)組,一組值的集合。列表
3、常量,單個(gè)值。字符串(str)、布爾值(bool)、整數(shù)(int)、浮點(diǎn)數(shù)(float)、Null、時(shí)間(time)、日期(date)
語(yǔ)法:
1、字典格式,Key: value,value前必須加空格
webserver: ip: 192.168.1.1 port: 8000 create_date: 2019-06-09 create_time:1:01:01:01 # 結(jié)果: { webserver: { ip: '192.168.1.1', port: 8000, create_date: Sun Jun 09 2019 08:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間), create_time: 219661 } }
2、列表格式,- value,-value前必須加空格
ip: - 192.168.1.1 - 192.168.1.2 - 192.168.1.3 - 192.168.1.4 # 結(jié)果: { 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 # 列表和字典嵌套 # 結(jié)果: { webserver: [ { ip: [ '192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4' ] }, { port: 8000 } ] }
4、!! 強(qiáng)制數(shù)據(jù)類型轉(zhuǎn)換
port: !!str 8000 num: !!int '1999' boolean: !!bool 'true' second: !!float '18.362' # yaml中,強(qiáng)制轉(zhuǎn)換時(shí),只是給字符串加引號(hào),去引號(hào),如果去引號(hào)后的值和要求的類型不符,轉(zhuǎn)換報(bào)錯(cuò)。 # 結(jié)果 { port: '8000', num: 1999, boolean: true, second: 18.362 }
5、定義錨點(diǎn)&和引用,&和*不能作為行首使用。作用是重復(fù)引用相同的值
port: &webport # 定義錨點(diǎn),&不可用在行首 - 8001 - 8002 server: ip: 192.168.1.1 port: *webport # 引用 public: addr: &addr1 liaoning # 定義錨點(diǎn) address: # 引用 - *addr1 結(jié)果: { port: [ 8001, 8002 ], server: [ '192.168.1.1', [ 8001, 8002 ] ], public: null, addr: 'liaoning', address: [ 'liaoning' ] }
6、 合并,<<,配合錨點(diǎn)使用,合并成一個(gè)字典
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 # 結(jié)果: { 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、定義格式符號(hào),>和|
segment_enter: > # >符號(hào),只保留段落最后一個(gè)回車 Mark set a major league home run record in 1998. line_enter: | # |符號(hào),所見(jiàn)即所得,保留每行的回車 65 Home Runs 0.278 Batting Average # 結(jié)果: { segment_enter: 'Mark set a major league home run record in 1998.\n', line_enter: '65 Home Runs\n0.278 Batting Average\n' }
8、---把內(nèi)容分割成多個(gè)文檔,以下例子相當(dāng)于兩個(gè)文件。
--- port: &webport # 定義錨點(diǎn),&不可用在行首 - 8001 - 8002 server: ip: 192.168.1.1 port: *webport # 引用 --- public: addr: &addr1 liaoning # 定義錨點(diǎn) address: # 引用 - *addr1
9、app自動(dòng)化測(cè)試配置用例(來(lái)源于網(wǎng)絡(luò)):
# 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}