YAML是YAML Ain’t Markup Language (YAML™)的縮寫。漢語意思是YAML不是一種標記語言...python
Python使用須要安裝PyYaml模塊web
pip install pyyaml
書寫格式:django
一、YAML大小寫敏感;
二、使用縮進表明層級關係,縮進只能使用空格,不能使用TAB;
三、相同層級左對齊;json
四、只有註釋行語法,使用 # 註釋api
支持的數據格式:數組
一、對象,使用鍵值對的數據。字典、哈希bash
二、數組,一組值的集合。列表網絡
三、常量,單個值。字符串(str)、布爾值(bool)、整數(int)、浮點數(float)、Null、時間(time)、日期(date)app
語法:ide
一、字典格式,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 } }
二、列表格式,- 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' ] }
三、列表和字典互相嵌套
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 } ] }
四、!! 強制數據類型轉換
port: !!str 8000 num: !!int '1999' boolean: !!bool 'true' second: !!float '18.362' # yaml中,強制轉換時,只是給字符串加引號,去引號,若是去引號後的值和要求的類型不符,轉換報錯。 # 結果 { port: '8000', num: 1999, boolean: true, second: 18.362 }
五、定義錨點&和引用,&和*不能做爲行首使用。做用是重複引用相同的值
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' ] }
六、 合併,<<,配合錨點使用,合併成一個字典
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 } }
七、定義格式符號,>和|
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' }
八、---把內容分割成多個文檔,如下例子至關於兩個文件。
--- port: &webport # 定義錨點,&不可用在行首 - 8001 - 8002 server: ip: 192.168.1.1 port: *webport # 引用 --- public: addr: &addr1 liaoning # 定義錨點 address: # 引用 - *addr1
九、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}