当前位置:网站首页>Aardio - integrate variable values into a string of text through variable names
Aardio - integrate variable values into a string of text through variable names
2022-07-06 22:31:00 【Lu Guangqing】
One : introduction
Suppose you have declared many variables , as follows :
// Various variables ( Constant )
console.name = " Zhang San " // Member variables
::age = 18 // Global variables
..sex = " male " // Global variables
var score = 90 // local variable
var t = { // Array
result = " good ";
comment = " very good ,nice, Continue to work hard !";
}
_OK = " Isn't surprise ?" // Global constants
If we want to integrate it into a string of texts , such as sql Statements and so on , The usual way is :
var s = ..string.format(' full name :%s\n Age :%s\n Gender :%s\n achievement :%s\n result :%s\n remarks :%s'
,console.name,age,sex,score,..table.tostring(t),_OK);
Execution results :
or :
var s = ..string.format(
` full name :%s
Age :%s
Gender :%s
achievement :%s
result :%s
remarks :%s`,
console.name,age,sex,score,..table.tostring(t),_OK);
Execution results :
Two 、 Basic solution :
It seems that this is already very convenient .
however python There is a more convenient , most important of all , Very intuitive The method of formatting string :f
f-string
A method of formatting strings , Use to
f
Starting string , be calledf-string
, It differs from ordinary strings in that , If the string contains{xxx}
, Will be replaced by the corresponding variable :>>> 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
In the above code ,
{r}
The dependent variabler
The value of ,{s:.2f}
The dependent variables
The value of , also:
hinder.2f
Format parameters specified ( That is, keep two decimal places ), therefore ,{s:.2f}
The result of the replacement is19.62
.
Put the variable name in the most appropriate place it should be , Make it very readable .
that , use aardio Can you imitate this grammar ?
suffer sunjichuancsdn The blog of
A piece of inspiration ( Thank you ), We imitate f-string, Make one f() function , Implement this function , Take a look at the following sentence :
The key is coming. :
Is it very close !!
Try to construct a statement by yourself :
var s = f(' full name :{console.name}\n Age :{age}\n Gender :{sex}\n achievement :{score}\n result :{t}\n remarks :{_OK}')
Think about the implementation results ? have a look :
or :
var s = f(
` full name :{console.name}
Age :{age}
Gender :{sex}
achievement :{score}
result :{t}
remarks :{_OK}`)
Execution results :
3、 ... and 、 More usage :
Does it look comfortable ? Where the variable name is located , Be clear at a glance , It's very comfortable to read .
See more detailed usage :
1、 Formatting text :
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);
Execution results :
2、 Exclude function hierarchy :
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" // First find the variable value of the nearest level , find "EFGH" instead of "ABCD"
var num = 890 // First find the variable value of the nearest level , find 890 instead of 456
console.dump(" Contains variables in the function of this layer :",f(s)); // Do not exclude any function layer , Include variables of this layer .
console.dump(" It does not include variables in the function of this layer :",f(s,,1)); // Exclude functions at this level (test function ,1 layer ) Variable .
console.dump(" exclude 2 Variables in layer function :",f(s,,2)); // Exclude from this layer up 2 Layer function variables .
// exclude 2 Layer means : Not only does it not include functions at this level (test function ,1 layer ) Variable ,
// Nor does it include upper level functions (2 layer ) Variable , This is already the top floor ,
// therefore , All excluded , The result is that the value of the variable is not found .
}
test();
console.pause(true);
Execution results :
3、 Global variables are not included :
import console;
t = "ABCD";
::num = 1111;
..num2 = 456
var local variable = " I'm a local variable "
var s="({t:10s})({num:10.2f})({num2:X})({ local variable })"
import godking
console.dump(" Contains global variables :",f(s));
console.dump(" Does not contain global variables :",f(s,false));
console.pause(true);
Execution results :
4、 Advanced play :
import console;
import godking
// If f Function in “ Including the whole ” Set up ( Default ) Next ,
// The specified variable is neither a local variable , It's not a global variable ,
// execute eval operation , take eval The result is returned as a variable value .
var s="({3+6*8})({console.color.red})({..math.pi})"
console.dump(f(s));
console.pause(true);
Execution results :
5、 A boring test :
import console;
var s="({s})"
import godking
for(i=1;5;1){
s = f(s)
}
console.dump(s)
console.pause(true);
Execution results :
Four 、 Library download address :
f() The function code is encapsulated in Guangqing extended function library , Please download here :
aardio Download resources _.rar , Decompress it and put it in \lib\godking Directory .
边栏推荐
- OpenCV VideoCapture. Get() parameter details
- 自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
- [linear algebra] determinant of order 1.3 n
- Management background --5, sub classification
- Classification, function and usage of MySQL constraints
- sizeof关键字
- 柔性数组到底如何使用呢?
- 【LeetCode】19、 删除链表的倒数第 N 个结点
- poj 1094 Sorting It All Out (拓扑排序)
- signed、unsigned关键字
猜你喜欢
[线性代数] 1.3 n阶行列式
[leetcode daily clock in] 1020 Number of enclaves
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
Assembly and interface technology experiment 5-8259 interrupt experiment
Web APIs DOM 时间对象
网络基础入门理解
NPDP认证|产品经理如何跨职能/跨团队沟通?
新手程序员该不该背代码?
PVL EDI project case
随机推荐
NetXpert XG2帮您解决“布线安装与维护”难题
sizeof关键字
Chapter 4: talk about class loader again
LeetCode 练习——剑指 Offer 26. 树的子结构
二分图判定
volatile关键字
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
GD32F4XX串口接收中断和闲时中断配置
Comparison between variable and "zero value"
手写ABA遇到的坑
MySQL ---- first acquaintance with MySQL
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
做接口测试都测什么?有哪些通用测试点?
hdu 5077 NAND(暴力打表)
What are the interface tests? What are the general test points?
ThreadLocal详解
Clip +json parsing converts the sound in the video into text
Puppeteer连接已有Chrome浏览器
memcached
Plafond du tutoriel MySQL, bien collecté, regardez lentement