YAML data format learning

YAML is the abbreviation of YAML Ain't Markup Language (YAML ™). Chinese means YAML is not a markup language


PyYaml module needs to be installed for Python usage

pip install pyyaml


Writing format:

1. YAML is case sensitive;
2. Use indent to represent hierarchy, and only space can be used for indent, not TAB;
3. Left alignment of the same level;

4. Only comment line syntax, use "comment"

 

Supported data formats:

1. Object, using the data of key value pair. Dictionary, hash

2. Array, a set of values. list

3. Constant, single value. String (str), bool, int, float, Null, time, date

 

Syntax:

1. Dictionary format, Key: value, value must be preceded by a space

webserver:

  ip: 192.168.1.1
  port: 8000
  create_date: 2019-06-09
  create_time:1:01:01:01


#Results:
{ webserver: 
   { ip: '192.168.1.1',
     port: 8000,
     create_date: Sun Jun 09 2019 08:00:00 GMT+0800 (China standard time),
     create_time: 219661 } }

 

 

2. List format, - value, - value must be preceded by a space

ip: 
  - 192.168.1.1
  - 192.168.1.2
  - 192.168.1.3
  - 192.168.1.4

#Results:
{ ip: [ '192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4' ] }

 

 

3. Nesting of lists and dictionaries

webserver:
  - ip: 
    - 192.168.1.1
    - 192.168.1.2
    - 192.168.1.3
    - 192.168.1.4
  - port: 8000
#List and dictionary nesting
#Results:
{ webserver: 
   [ { ip: [ '192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4' ] },
     { port: 8000 } ] }

 

4.!! Force data type conversion

port: !!str 8000
num: !!int '1999'
boolean: !!bool 'true'
second: !!float '18.362'

#In yaml, when you cast a string, you only need to quote and de quote the string. If the value after the de quote does not match the required type, the conversion will report an error.
#Result
{ port: '8000', num: 1999, boolean: true, second: 18.362 }

 

5. Define anchor & and reference, & and * cannot be used as the beginning of a line. The function is to repeatedly reference the same value

port: &webport  #Define anchor, & not available at the beginning of line
  - 8001
  - 8002
server:
  ip: 192.168.1.1
  port: *webport    #Quote

public: 
 addr: &addr1 liaoning  #Define anchor point


address:                #Quote
  - *addr1

//Result:
{ port: [ 8001, 8002 ],
  server: [ '192.168.1.1', [ 8001, 8002 ] ],
  public: null,
  addr: 'liaoning',
  address: [ 'liaoning' ] }

6. Merge, < and use with anchor points to merge into a dictionary

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

#Results:
{ 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. Define format symbols, > and|

segment_enter: >               #Symbol, only the last carriage return of the paragraph is reserved
 Mark set a major league
 home run record in 1998.
line_enter: |                #| sign, what you see is what you get, keep the carriage return of each line
 65 Home Runs
 0.278 Batting Average
#Results:
{ segment_enter: 'Mark set a major league home run record in 1998.\n',
  line_enter: '65 Home Runs\n0.278 Batting Average\n' }

 

 

8. Divide the content into multiple documents. The following example is equivalent to two files.

---
port: &webport  #Define anchor, & not available at the beginning of line
  - 8001
  - 8002
server:
  ip: 192.168.1.1
  port: *webport    #Quote
  
---
public: 
 addr: &addr1 liaoning  #Define anchor point
address:                #Quote
  - *addr1

 

9. app automation test configuration case (from the network):

# 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}


 

 

 

 

 

 

 

 

 


Tags: Python JSON Django pip

Posted on Sun, 01 Dec 2019 02:21:58 -0800 by AnsonM