当前位置:网站首页>Goal: do not exclude yaml syntax. Try to get started quickly
Goal: do not exclude yaml syntax. Try to get started quickly
2022-07-07 21:18:00 【Look, the future】
Reading guide
The three years , except Makefile, I've never touched such a strange grammar . Of course Makefile I decided to give up , Because I can write CMake Hey .
You have to do it , Something to be used soon . Read through the grammar first , Not much anyway , Take it later Python Let's demonstrate that the world may feel clear ( Have to say ,Python What a good thing )
yaml ?
- Scripting language .
- For writing configuration files .
- The suffix is .yml.
- play docker、k8s There must be no escape from this .
yaml Basic grammar
- 1、 Case sensitive .
- 2、 Use indentation to indicate hierarchy , The number of spaces is not important , The same level can be consistent .
- 3、 Indenting is not allowed tab, Only spaces .
- 4、# Table annotation .
yaml data type
object : Set of key value pairs , Also known as mapping (mapping)/ Hash (hashes) / Dictionaries (dictionary)
Array : A set of values in order , Also called sequence (sequence) / list (list)
Pure quantity (scalars): A single 、 Nonseparable value
object
I can't bear it , We turn on python Editor for .
pip install pyyaml
Object key value pairs are represented by colons key: value, Add a space after the colon .
You can also use key:{key1: value1, key2: value2, …}.
You can also use indentation to represent hierarchical relationships ;
key:
child-key: value
child-key2: value2
We use it Python To transform :
import yaml
f = open("yaml1.yaml")
res = yaml.safe_load(f)
print(res)
/yaml_test/main.py
{
'key': {
'child-key': 'value', 'child-key2': 'value2'}}
Process finished with exit code 0
More complex object formats , You can use a question mark and a space to represent a complex key, A colon with a space represents a value:
?
- complexkey1
- complexkey2
:
- complexvalue1
- complexvalue2
This means that the property of an object is an array [complexkey1,complexkey2], The corresponding value is also an array [complexvalue1,complexvalue2]
( In the tutorial , however Python Can't be transformed , So I'm not sure . But it's also my first contact , So I can only put my words here .)
Array
With - The first row represents an array :
- A
- B
- C
/yaml_test/main.py
['A', 'B', 'C']
Process finished with exit code 0
YAML Support multidimensional array , You can use in line representation :
key: [value1, value2, …]
A child member of a data structure is an array , You can indent a space under the item .
-
- A
- B
- C
yaml_test/main.py
[['A', 'B', 'C']]
Process finished with exit code 0
A relatively complex example :
companies:
-
id: 1
name: company1
price: 200W
-
id: 2
name: company2
price: 500W
/yaml_test/main.py
{
'companies': [{
'id': 1, 'name': 'company1', 'price': '200W'}, {
'id': 2, 'name': 'company2', 'price': '500W'}]}
Process finished with exit code 0
intend companies Property is an array , Each array element is made up of id、name、price An object composed of three attributes .
Arrays can also use streaming (flow) How to express :
companies: [{
id: 1,name: company1,price: 200W},{
id: 2,name: company2,price: 500W}]
( Wouldn't it be nice to write that directly ? I'm shallow )
Composite structure
Arrays and objects can form a composite structure , example :
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
Convert to json by :
I won't let this out , You can see for yourself .
Pure quantity
Pure quantity is the most basic , Nonseparable value , Include :
character string
Boolean value
Integers
Floating point numbers
Null
Time
date
Use an example to quickly understand the basic use of scalar :
boolean:
- TRUE #true,True Fine
- FALSE #false,False Fine
float:
- 3.14
- 6.8523015e+5 # You can use scientific counting
int:
- 123
- 0b1010_0111_0100_1010_1110 # Binary representation
null:
nodeName: 'null'
parent: ~ # Use ~ Express null
# It shows that None
string:
- ha-ha
- 'Hello world' # Special characters can be wrapped in double quotes or single quotes
- newline
newline2 # Strings can be split into multiple lines , Each line is converted to a space
yaml_test/main.py
{
'string': [' ha-ha ', 'Hello world', 'newline newline2']}
Process finished with exit code 0
date:
- 2018-02-17 # Date must use ISO 8601 Format , namely yyyy-MM-dd
datetime:
- 2018-02-17T15:02:31+08:00 # Time use ISO 8601 Format , Use... Between time and date T Connect , Finally using + Represents the time zone
yaml_test/main.py
{
'date': [datetime.date(2018, 2, 17)], 'datetime': [datetime.datetime(2018, 2, 17, 15, 2, 31, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800)))]}
Process finished with exit code 0
quote
( This part is operated by me k8s I haven't met in this period of time )
& Anchor points and * Alias , Can be used to quote :
defaults: &defaults
adapter: postgres
host: localhost
development:
database: myapp_development
<<: *defaults
test:
database: myapp_test
<<: *defaults
amount to :
defaults:
adapter: postgres
host: localhost
development:
database: myapp_development
adapter: postgres
host: localhost
test:
database: myapp_test
adapter: postgres
host: localhost
yaml_test/main.py
{
'defaults': {
'adapter': 'postgres', 'host': 'localhost'}, 'development': {
'adapter': 'postgres', 'host': 'localhost', 'database': 'myapp_development'}, 'test': {
'adapter': 'postgres', 'host': 'localhost', 'database': 'myapp_test'}}
Process finished with exit code 0
& Used to build anchors (defaults),<< Represents merging into the current data ,* Used to reference anchor points .
Here's another example :
- &showell Steve
- Clark
- Brian
- Oren
- *showell
To Json The code is as follows :
[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]
What about that ?:
-
- &showell Steve
-
- Clark
-
- Brian
-
- Oren
-
- *showell
Small tips:
If JSON Too long , You can make it .json Drag the file to Firefox to see .
边栏推荐
- How to choose fund products? What fund is suitable to buy in July 2022?
- [function recursion] do you know all five classic examples of simple recursion?
- npm uninstall和rm直接删除的区别
- 【矩阵乘】【NOI 2012】【cogs963】随机数生成器
- MinGW MinGW-w64 TDM-GCC等工具链之间的差别与联系「建议收藏」
- HOJ 2245 浮游三角胞(数学啊 )
- 智能软件分析平台Embold
- 阿里云有奖体验:如何通过ECS挂载NAS文件系统
- 开户必须往账户里面赚钱吗,资金安全吗?
- What are the official stock trading apps in the country? Is it safe to use
猜你喜欢
SQL injection error report injection function graphic explanation
Cantata9.0 | new features
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
软件缺陷静态分析 CodeSonar 5.2 新版发布
Static analysis of software defects codesonar 5.2 release
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
Small guide for rapid formation of manipulator (12): inverse kinematics analysis
神兵利器——敏感文件发现工具
Tensorflow2.x下如何运行1.x的代码
随机推荐
openGl超级宝典学习笔记 (1)第一个三角形「建议收藏」
How to choose fund products? What fund is suitable to buy in July 2022?
Codeforces Round #296 (Div. 2) A. Playing with Paper[通俗易懂]
Make this crmeb single merchant wechat mall system popular, so easy to use!
程序猿赚的那点钱算个P啊!
恶魔奶爸 A0 英文零基础的自我提升路
EasyUI date control emptying value
object-c编程tips-timer「建议收藏」
sqlHelper的增删改查
Word inversion implements "suggestions collection"
Cocos2d-x 游戏存档[通俗易懂]
反诈困境,国有大行如何破局?
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
Numerical method for solving optimal control problem (0) -- Definition
Codesonar enhances software reliability through innovative static analysis
C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
FTP steps for downloading files from Huawei CE switches
Codesonar Webinar
What stocks can a new account holder buy? Is the stock trading account safe
私募基金在中国合法吗?安全吗?