当前位置:网站首页>Aardio - 通过变量名将变量值整合到一串文本中
Aardio - 通过变量名将变量值整合到一串文本中
2022-07-06 14:39:00 【卢光庆】
一:引言
假设已经声明好许多变量,如下:
// 各种变量(常量)
console.name = "张三" //成员变量
::age = 18 //全局变量
..sex = "男" //全局变量
var score = 90 //局部变量
var t = { //数组
结果 = "优秀";
评语 = "很好,nice,继续努力!";
}
_OK = "惊不惊喜?" //全局常量如果我们要将他整合到一串文本中,比如sql语句之类的,通常做法是:
var s = ..string.format('姓名:%s\n年龄:%s\n性别:%s\n成绩:%s\n结果:%s\n备注:%s'
,console.name,age,sex,score,..table.tostring(t),_OK);执行结果:

或:
var s = ..string.format(
`姓名:%s
年龄:%s
性别:%s
成绩:%s
结果:%s
备注:%s`,
console.name,age,sex,score,..table.tostring(t),_OK);执行结果:

二、基本解决方案:
貌似这样已经非常方便了。
但是python有个更加方便,最重要的是,非常直观的格式化字符串的方法:f
f-string
一种格式化字符串的方法,使用以
f开头的字符串,称之为f-string,它和普通字符串不同之处在于,字符串如果包含{xxx},就会以对应的变量替换:>>> r = 2.5 >>> s = 3.14 * r ** 2 >>> print(f'The area of a circle with radius {r} is {s:.2f}') The area of a circle with radius 2.5 is 19.62上述代码中,
{r}被变量r的值替换,{s:.2f}被变量s的值替换,并且:后面的.2f指定了格式化参数(即保留两位小数),因此,{s:.2f}的替换结果是19.62。
将变量名称放在它应该在的最合适的位置, 让易读性变得非常强。
那么,用aardio可以模仿这种语法么?
《 aardio调试库解决局部变量的有趣问题(一)_aardio笔记的博客-CSDN博客_aardio》
一文启发(在此表示感谢),我们模仿f-string,造一个 f() 函数,实现这个功能,来看看下面这个语句:
重点来了:

是不是非常接近了!!
自己构造一个语句试试:
var s = f('姓名:{console.name}\n年龄:{age}\n性别:{sex}\n成绩:{score}\n结果:{t}\n备注:{_OK}')想想执行结果是啥?看看:

或:
var s = f(
`姓名:{console.name}
年龄:{age}
性别:{sex}
成绩:{score}
结果:{t}
备注:{_OK}`)执行结果:

三、更多用法:
是不是看着很舒服?变量名称所在的位置,一目了然,阅读起来非常舒服。
再看更多详细用法:
1、格式化文本:
import console;
var t = "ABCD"
var num = 456
var num2 = 12345678
var s="({t:10s})({num:10.2f})({num2:X})"
import godking
console.dump(f(s))
console.pause(true);执行结果:

2、排除函数层次:
import console;
var t = "ABCD"
var num = 456
var num2 = 12345678
var s="({t:10s})({num:10.2f})({num2:X})"
import godking
var test = function(){
var t = "EFGH" // 优先找最近层次的变量值,找到 "EFGH" 而不是 "ABCD"
var num = 890 // 优先找最近层次的变量值,找到 890 而不是 456
console.dump("包含本层函数内变量:",f(s)); //不排除任何函数层,包含本层变量。
console.dump("不包含本层函数内变量:",f(s,,1)); //排除本层函数(test函数,1层)变量。
console.dump("排除2层函数内变量:",f(s,,2)); //排除自本层往上2层函数变量。
// 排除2层的意思是:不但不包括本层函数(test函数,1层)变量,
// 也不包括上层函数(2层)变量,这已经是最顶层了,
// 所以,都排除了,结果就是没找到变量的值。
}
test();
console.pause(true);执行结果:

3、不包括全局变量:
import console;
t = "ABCD";
::num = 1111;
..num2 = 456
var 局部变量 = "我是局部变量"
var s="({t:10s})({num:10.2f})({num2:X})({局部变量})"
import godking
console.dump("包含全局变量:",f(s));
console.dump("不包含全局变量:",f(s,false));
console.pause(true);
执行结果:

4、高级玩法:
import console;
import godking
// 如果 f 函数在“包含全局”设置(默认)下,
// 指定变量既不是局部变量,也不是全局变量,
// 则执行eval操作,将eval结果作为变量值返回。
var s="({3+6*8})({console.color.red})({..math.pi})"
console.dump(f(s));
console.pause(true);执行结果:

5、一个无聊的测试:
import console;
var s="({s})"
import godking
for(i=1;5;1){
s = f(s)
}
console.dump(s)
console.pause(true);
执行结果:

四、库下载地址:
f() 函数代码封装于光庆扩展函数库中,请此处下载:
aardio资源下载 _.rar ,解压缩后放到\lib\godking目录中。
边栏推荐
- GPS from getting started to giving up (19), precise ephemeris (SP3 format)
- 剪映+json解析将视频中的声音转换成文本
- GPS from entry to abandonment (XIV), ionospheric delay
- ZABBIX proxy server and ZABBIX SNMP monitoring
- 小常识:保险中的“保全”是什么?
- 软考高级(信息系统项目管理师)高频考点:项目质量管理
- labelimg的安装与使用
- Management background --4, delete classification
- SQL Server生成自增序号
- go多样化定时任务通用实现与封装
猜你喜欢

The nearest common ancestor of binary (search) tree ●●

UNI-Admin基础框架怎么关闭创建超级管理员入口?

Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing

Chapter 3: detailed explanation of class loading process (class life cycle)

手写ABA遇到的坑

第3章:类的加载过程(类的生命周期)详解

Attack and defense world ditf Misc

Adjustable DC power supply based on LM317

CCNA-思科网络 EIGRP协议

Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
随机推荐
[MySQL] online DDL details
What a new company needs to practice and pay attention to
MariaDB database management system learning (I) installation diagram
剪映+json解析将视频中的声音转换成文本
将MySQL的表数据纯净方式导出
【10点公开课】:视频质量评价基础与实践
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
【sciter Bug篇】多行隐藏
HDU 4912 paths on the tree (lca+)
Management background --1 Create classification
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
手写ABA遇到的坑
GPS from getting started to giving up (XV), DCB differential code deviation
Shortcut keys in the terminal
Chapter 3: detailed explanation of class loading process (class life cycle)
Learn the principle of database kernel from Oracle log parsing
插入排序与希尔排序
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
2022-07-05 使用tpcc对stonedb进行子查询测试
pytorch_YOLOX剪枝【附代码】