当前位置:网站首页>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 .
边栏推荐
- const关键字
- Applet system update prompt, and force the applet to restart and use the new version
- 自定义 swap 函数
- 二分图判定
- Daily question 1: force deduction: 225: realize stack with queue
- 将MySQL的表数据纯净方式导出
- Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
- Spatial domain and frequency domain image compression of images
- Advantages of link local address in IPv6
- [linear algebra] determinant of order 1.3 n
猜你喜欢
Mysql database basic operations DML
Aardio - 封装库时批量处理属性与回调函数的方法
3DMAX assign face map
Should novice programmers memorize code?
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
Spatial domain and frequency domain image compression of images
如何用程序确认当前系统的存储模式?
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
The nearest common ancestor of binary (search) tree ●●
树的先序中序后序遍历
随机推荐
return 关键字
What are the specific steps and schedule of IELTS speaking?
空结构体多大?
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
ThreadLocal详解
Crawler obtains real estate data
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
Clip +json parsing converts the sound in the video into text
Memorabilia of domestic database in June 2022 - ink Sky Wheel
每日一题:力扣:225:用队列实现栈
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
NPDP认证|产品经理如何跨职能/跨团队沟通?
case 关键字后面的值有什么要求吗?
Assembly and interface technology experiment 5-8259 interrupt experiment
Aardio - 封装库时批量处理属性与回调函数的方法
Build op-tee development environment based on qemuv8
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
Jafka来源分析——Processor
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)