当前位置:网站首页>TOML configuration file format, YAML's top contender
TOML configuration file format, YAML's top contender
2022-08-04 02:35:00 【Zeze says test】
活动地址:CSDN21天学习挑战赛
今天我们一起来学习tomlThis concise configuration file format.
TOML是在 2013 年发布的配置文件格式.距今虽然有8年历史了,但是之前一直没有在大项目中见过的.我是前段时间在看python的pep规范,无意中看到了这种配置文件格式,稍微了解了一下,才发现现在有很多新的明星项目都喜欢用它来做配置文件.
1、有rust语言的包管理工具cargo;
2、类似于docker的容器工具containerd;
3、go语言很多项目都喜欢用.比如静态网站生成工具Hugo、数据库InfluxDB、GitLab CI.
4、python项目也开始接纳.包管理工具 pip、pipenv和Poetry都是用的TOML,[1]现在连官方的metadata管理也开始支持toml.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nA6uUtbF-1659431944359)(image-20210106151231025.png)]
1. 一个小例子
为什么新项目这么喜欢 TOML 呢? 让我们先来看一个TOML的小例子吧,看完了说不定你就知道了.
[project]
name = "spam"
version = "2020.0.0"
description = "Lovely Spam! Wonderful Spam!"
readme = "README.rst"
requires-python = ">=3.8"
license = {file = "LICENSE.txt"}
keywords = ["egg", "bacon", "sausage"]
有没有感觉很熟悉呢?它和ini格式的配置文件几乎一模一样.要知道互联网发展到今天,大多数有影响力的项目都是采用ini这种配置文件格式,如果你之前有接触过ini,切换到toml几乎是0成本.
那为什么不直接用ini呢?因为ini格式支持的数据类型非常少,配置项的值都会被默认当成字符串,如果想表示数字、数组,还需要自己进行额外的解析.
但是在TOML中,数据类型非常丰富:
字符串
注释
数字
布尔值
数组
哈希表
甚至连时间格式都支持
2. 字符串
字符串我们当然不会陌生,但是如果你是从ini格式转过来的,一定要注意字符串值要加引号.
键默认会当成字符串处理,可以加也可以不加. 加引号的键叫引号键(Quoted Key),不加引号的键叫裸键(Bare Key).
TOML 格式非常灵活,对于灵活的语言,最好的方式是给自己设置规范和纪律,一直用好一种用法,不然很容易学废.官方推荐我们尽量用裸键,当裸键不能正确表达意思的时候,再用引号键.
name = "spam" #OK
name_1 = spam #错误
"name_2" = "spam" #OK
裸键只能包含 ASCII 字母,ASCII 数字,下划线和短横线(A-Za-z0-9_-),引号键则可以支持特殊字符,甚至是空格.当键中包含特殊字符的时候,使用引号键.
'[email protected]! erf:' = 'spam' # OK
[email protected]! erf: = 'spam' # 错误
当字符串为多行字符串时使用三引号,会自动换行:
name = """
this is a
new line
command"""
得到的结果是:
this is a
new line
command
如果不想自动换行,只是想增强可读性,可以使用折行符号\:
name = """
this is a \
new line \
command"""
得到结果是不换行的:
this is a new line command
3. 数字
如果你都会编程了,数字应该会知道怎么表示.
age1 = 18
age2 = -2
age3 = +16
age4 = 11.2
toml除了支持10进制表示,还支持二进制(0b)、八进制(0o)、十六进制(0x)
age1 = 0b11
age2 = 0o12
age3 = 0xb
上面的解析结果为:
{
'age1': 3,
'age2': 10,
'age3': 11
}
4. 布尔
bool1 = true
bool2 = false
5. 时间
时间类型是toml的一大特色,因为绝大多数的配置文件格式都是不支持时间类型的.toml 遵循的是 RFC 3339 时间格式,只要照着格式写,解析出来会自动转成编程语言的时间类型.
ts = 2021-01-06 07:30:00
ts1 = 2021-01-06 07:30:00.1234
time1 = 07:30:00
得到的结果:
{
'ts': datetime.datetime(2021, 1, 6, 7, 30),
'ts1': datetime.datetime(2021, 1, 6, 7, 30, 0, 123000),
'time1': datetime.time(7, 30)
}
6. 数组
数组可以是同类型数据,可以换行,也可以嵌套,和python当中列表简直一模一样.但是要注意,尽量保持元素是同类型的数据.
users = [
['iswy', '男'],
['you', '女'],
]
得到结果:
{
'users': [
['iswy', '男'],
['you', '女']
]
}
7. 哈希表
表有点类似于python中的字典:
user = {name='iswy', gender='男'}
得到的结果:
{
'user': {
'gender': '男',
'name': 'iswy'
}
}
这种写在key后面的表叫内联表.还有一种用法,类似于ini格式中的section,用于分组管理,暂且把它叫做外联表.比如上面的例子,可以换一种形式,得到的结果是完全一样的.
[user]
gender = '男'
name = 'iswy'
表支持嵌套,子表可以缩进也可以不缩进:
[user]
cat = ['admin', 'normal']
[user.student]
name='iswy'
age=19
[user.teacher]
name='you'
age=32
如果你觉得这种风格你不喜欢,也可以换成下面的风格:
[user]
cat = ['admin', 'normal']
# student
student.name='iswy'
student.age=19
# teacher
teacher.name='you'
teacher.age=328
一定要注意,当键包含. 号时,一定要加引号:
site.'x.com' = 'http://x.com'
我个人更倾向于子表嵌套的风格,因为这样的表述看起来更加简洁.
表和数组的配套使用
最后我们要考虑的问题是当有多个用户需要配置,我们要使用表数组进行分组呢?
[[user]]
[user.student]
name='iswy'
age=19
[user.teacher]
name='you'
age=328
[[user]]
[user.student]
name='iswy2'
age=192
[user.teacher]
name='you2'
age=3282
得到的结果:
{
'user': [
{
'student': {
'age': 19, 'name': 'iswy'},
'teacher': {
'age': 328, 'name': 'you'}
},
{
'student': {
'age': 192, 'name': 'iswy2'},
'teacher': {
'age': 3282, 'name': 'you2'}
}
]
}
[^1] https://github.com/toml-lang/toml/wiki#projects-using-toml
边栏推荐
- 返回字符串中的最大回文数
- 第08章 索引的创建与设计原则【2.索引及调优篇】【MySQL高级】
- Simple sorting (summer vacation daily question 14)
- 织梦响应式酒店民宿住宿类网站织梦模板(自适应手机端)
- Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
- Example 039: Inserting elements into an ordered list
- 实例040:逆序列表
- Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).
- The browser
- DHCP服务详解
猜你喜欢

There are n steps in total, and you can go up to 1 or 2 steps each time. How many ways are there?

自制蓝牙手机app控制stm8/stm32/C51板载LED

Ant - the design of the Select component using a custom icon (suffixIcon attribute) suffixes, click on the custom ICONS have no reaction, will not display the drop-down menu

DDTL: Domain Transfer Learning at a Distance

关联接口测试

【云原生】DevOps(六):Jenkins流水线

董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白

2022广东省安全员A证第三批(主要负责人)考试题库及模拟考试

MallBook联合人民交通出版社,推动驾培领域新发展,开启驾培智慧交易新生态

Flink原理流程图简单记录
随机推荐
uni-app 从零开始-基础模版(一)
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
2022焊工(初级)上岗证题目模拟考试平台操作
Kubernetes:(十一)KubeSphere的介绍和安装(华丽的篇章)
esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
出海季,互联网出海锦囊之本地化
Deep Learning (3) Classification Theory Part
Utilities of Ruineng Micrometer Chip RN2026
Homemade bluetooth mobile app to control stm8/stm32/C51 onboard LED
Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
C program compilation and predefined detailed explanation
云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
Engineering drawing review questions (with answers)
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
STM8S105k4t6c--------------点亮LED
activiti流程执行过程中,数据库表的使用关系
Presto中broadcast join和partition join执行计划的处理过程
SAP SD module foreground operation
深度学习(三)分类 理论部分
Dong mingzhu live cold face away, when employees frequency low-level mistakes, no one can understand their products