当前位置:网站首页>目标:不排斥 yaml 语法。争取快速上手
目标:不排斥 yaml 语法。争取快速上手
2022-07-07 18:34:00 【看,未来】

导读
这三年,除了 Makefile,我就没碰过这么怪的语法。当然 Makefile 我是果断放弃了,因为我会写 CMake 嘿嘿。
怪也得上手啊,马上就要用的东西了。先通读一下语法吧,反正不多,一会儿拿 Python 来演示一下可能会感觉世界都明朗了吧(不得不说,Python 真是个好东西)
yaml ?
- 脚本语言。
- 写配置文件用的。
- 后缀为 .yml。
- 玩 docker、k8s 肯定是逃不掉这个了。
yaml 基础语法
- 1、大小写敏感。
- 2、使用缩进表示层级关系,空格数不重要,同一层级一致即可。
- 3、缩进不允许使用 tab,只能空格。
- 4、# 表注释。
yaml 数据类型
对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
纯量(scalars):单个的、不可再分的值
对象
我受不了了,我们打开 python 的编辑器。
pip install pyyaml
对象键值对使用冒号结构表示 key: value,冒号后面要加一个空格。
也可以使用 key:{key1: value1, key2: value2, …}。
还可以使用缩进表示层级关系;
key:
child-key: value
child-key2: value2
我们用 Python 来转化一下:
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
较为复杂的对象格式,可以使用问号加一个空格代表一个复杂的 key,配合一个冒号加一个空格代表一个 value:
?
- complexkey1
- complexkey2
:
- complexvalue1
- complexvalue2
意思即对象的属性是一个数组 [complexkey1,complexkey2],对应的值也是一个数组 [complexvalue1,complexvalue2]
(教程里的,但是 Python 转化不出来,所以我不确定。但是我也是第一次接触,所以就只能把话放这儿了。)
数组
以 - 开头的行表示构成一个数组:
- A
- B
- C
/yaml_test/main.py
['A', 'B', 'C']
Process finished with exit code 0
YAML 支持多维数组,可以使用行内表示:
key: [value1, value2, …]
数据结构的子成员是一个数组,则可以在该项下面缩进一个空格。
-
- A
- B
- C
yaml_test/main.py
[['A', 'B', 'C']]
Process finished with exit code 0
一个相对复杂的例子:
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
意思是 companies 属性是一个数组,每一个数组元素又是由 id、name、price 三个属性构成的对象。
数组也可以使用流式(flow)的方式表示:
companies: [{
id: 1,name: company1,price: 200W},{
id: 2,name: company2,price: 500W}]
(直接这么写不好吗?是我肤浅了吧)
复合结构
数组和对象可以构成复合结构,例:
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
转换为 json 为:
这个我就不放出来了,可以自己看。
纯量
纯量是最基本的,不可再分的值,包括:
字符串
布尔值
整数
浮点数
Null
时间
日期
使用一个例子来快速了解纯量的基本使用:
boolean:
- TRUE #true,True都可以
- FALSE #false,False都可以
float:
- 3.14
- 6.8523015e+5 #可以使用科学计数法
int:
- 123
- 0b1010_0111_0100_1010_1110 #二进制表示
null:
nodeName: 'null'
parent: ~ # 使用~表示null
# 显示出来是 None
string:
- 哈哈
- 'Hello world' #可以使用双引号或者单引号包裹特殊字符
- newline
newline2 #字符串可以拆成多行,每一行会被转化成一个空格
yaml_test/main.py
{
'string': ['哈哈', 'Hello world', 'newline newline2']}
Process finished with exit code 0
date:
- 2018-02-17 #日期必须使用ISO 8601格式,即yyyy-MM-dd
datetime:
- 2018-02-17T15:02:31+08:00 #时间使用ISO 8601格式,时间和日期之间使用T连接,最后使用+代表时区
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
引用
(这一部分在我操作 k8s 的这段时间里是还没遇到过)
& 锚点和 * 别名,可以用来引用:
defaults: &defaults
adapter: postgres
host: localhost
development:
database: myapp_development
<<: *defaults
test:
database: myapp_test
<<: *defaults
相当于:
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
& 用来建立锚点(defaults),<< 表示合并到当前数据,* 用来引用锚点。
下面是另一个例子:
- &showell Steve
- Clark
- Brian
- Oren
- *showell
转为 Json 代码如下:
[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]
那这样呢?:
-
- &showell Steve
-
- Clark
-
- Brian
-
- Oren
-
- *showell
小 tips:
如果 JSON 过长,可以打成 .json 文件拖到火狐下看。
边栏推荐
- 如何挑选基金产品?2022年7月份适合买什么基金?
- Meta Force原力元宇宙系统开发佛萨奇模式
- Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
- H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
- Écrivez une liste de sauts
- 智能软件分析平台Embold
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
- EasyGBS级联时,上级平台重启导致推流失败、画面卡住该如何解决?
- About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]
- How to choose financial products? Novice doesn't know anything
猜你喜欢

ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your

Vulnhub's funfox2

AIRIOT助力城市管廊工程,智慧物联守护城市生命线

Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
![About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]](/img/59/33381b8d45401607736f05907ee381.png)
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]

php 获取图片信息的方法

不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统

机器学习笔记 - 使用Streamlit探索对象检测数据集

AADL Inspector 故障树安全分析模块

机械臂速成小指南(十二):逆运动学分析
随机推荐
Micro service remote debug, nocalhost + rainbow micro service development second bullet
不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统
Write a jump table
CodeSonar如何帮助无人机查找软件缺陷?
Solve the problem of incomplete display around LCD display of rk3128 projector
Implement secondary index with Gaussian redis
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
静态测试工具
H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
解决/bin/sh进去的容器运行可执行文件报not found的问题
Mrs offline data analysis: process OBS data through Flink job
Is it safe to open a stock account at present? Can I open an account online directly.
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
如何满足医疗设备对安全性和保密性的双重需求?
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]
有了ST7008, 蓝牙测试完全拿捏住了
Apifox interface integrated management new artifact
VMWare中虚拟机网络配置