Think yaml with python

What is yaml?

Preface

When setting up a cloud-native environment such as kubernetes, yaml format files are often read. I thought it would be a good idea to use yaml without knowing it well, so this time I actually studied while moving my hands.

Origin of yaml

From yaml official website 「“YAML Ain’t Markup Language” 」

It is a way of writing a data structure represented by a recurrence acronym meaning "yaml is not a markup language".

Design goal

YAML has the following seven design goals.

  1. YAML is easy and easy for people to read
  2. YAML data is portable between program languages
  3. YAML matches native data structures in agile languages
  4. YAML has a consistent model to support generic tools
  5. YAML supports one-pass processing
  6. YAML is expressive and extensible
  7. Easy to implement and use YAML

load and dump

When dealing with yaml data, there are load and dump. load is the flow in which the program understands yaml data, while dump is the flow in which the program converts it to yaml data. In other words, it's okay if you understand that the yaml input is "load" and the yaml output is "dump" when viewed from the app. 公式サイト「3.1 Processing Overview」より From the official website "3.1 Processing Overview"

Try to implement with python

Actually move your hands and think. This time, I will actually handle yaml using python3 as a program language.

Install pyyaml

There is a package called pyyaml to use yaml with python. Install using pip. pip install pyyaml

Actually write the code

Write and check the code for load that reads yaml and dump that outputs yaml. The environment I tried is as follows.

1. Load yaml data with python (load)

Prepare a yaml file for test

Create a yaml file (test.yaml) as follows.

test.yaml


env:
  python:3.7.3
  pyYAML:5.3.1

Get the version information written in yaml with python.

Write code in python

Create python code (data-reader.py) to read yaml in the same folder as test.yaml.

data-reader.py


#!/usr/bin/env python3
from yaml import load, dump
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

with open('test.yaml', 'r') as yml:
    config = load(yml, Loader=Loader)

print("#### data_chcek ####")
print(config)
print("####################")

print("software/package name:Obtained version")
print("python: {}".format(config['env']['python']))
print("pyYAML: {}".format(config['env']['pyYAML']))
Execution result

I opened the terminal and ran the python code as below. It was confirmed that the data acquired from yaml was converted into a multi-layered dictionary type and the desired version information could be retrieved.

$ python3 data-reader.py 
#### data_chcek ####
{'env': {'python': '3.7.3', 'pyYAML': '5.3.1'}}
####################
software/package name:Obtained version
python: 3.7.3
pyYAML: 5.3.1

2. Output the data created by python to a yaml file (dump)

Write code in python

Create dictionary type data as a sample, convert the created data to yaml format and output it. I created python code (data-writer.py) as follows.

data-writer.py


#!/usr/bin/env python3
from yaml import load, dump
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

# making data for yaml
item_list = ['apple', 'banana', 'orange']
amounts = [2, 3, 4]
sample_data = {item:value for item, value in zip(item_list, amounts)}

# data output to yaml
output = dump(sample_data, Dumper=Dumper)

with open("output_file.yaml", 'w') as yml:
    yml.write(output)
Execution result

Open terminal and run the python code as below. As a result of execution, it was confirmed that the dictionary type data created in the sample was converted to yaml format.

$ python3 data-writer.py
#### data_check ####
apple: 2
banana: 3
orange: 4

####################
output to output_file.yaml

You can check the contents of the output file below.

$ cat output_file.yaml                        
apple: 2
banana: 3
orange: 4

Summary

About yaml, I read the document and confirmed the behavior while actually writing the code. In the future, I would like to deepen my knowledge about yaml while paying attention to both load and dump.

Recommended Posts

Think yaml with python
Handling yaml with python
Auto-complete YAML content with Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Learning Python with ChemTHEATER 03
Sequential search with Python
Run Python with VBA
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Python starting with Windows 7
Heatmap with Python + matplotlib
Multi-process asynchronously with python
Python programming with Atom
Learning Python with ChemTHEATER 02