当前位置:网站首页>目标:不排斥 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 文件拖到火狐下看。
边栏推荐
猜你喜欢
Small guide for rapid formation of manipulator (12): inverse kinematics analysis
Splicing and splitting of integer ints
Tensorflow2. How to run under x 1 Code of X
Apifox 接口一体化管理新神器
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
网络原理(1)——基础原理概述
Implement secondary index with Gaussian redis
Network principle (1) - overview of basic principles
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
随机推荐
有了ST7008, 蓝牙测试完全拿捏住了
【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
OneSpin | 解决IC设计中的硬件木马和安全信任问题
Creation of kubernetes mysql8
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
Splicing and splitting of integer ints
Force buckle 912 Sort array
Écrivez une liste de sauts
Guava multithreading, futurecallback thread calls are uneven
使用高斯Redis实现二级索引
使用 BR 备份 TiDB 集群数据到 Azure Blob Storage
软件缺陷静态分析 CodeSonar 5.2 新版发布
4G设备接入EasyGBS平台出现流量消耗异常,是什么原因?
OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验
[solution] package 'XXXX' is not in goroot
解决/bin/sh进去的容器运行可执行文件报not found的问题
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
TS quick start - Generic
不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统