当前位置:网站首页>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
fStarting 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.62In the above code ,
{r}The dependent variablerThe value of ,{s:.2f}The dependent variablesThe value of , also:hinder.2fFormat 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 .
边栏推荐
- PVL EDI project case
- 关于声子和热输运计算中BORN电荷和non-analytic修正的问题
- Build op-tee development environment based on qemuv8
- Gd32f4xx serial port receive interrupt and idle interrupt configuration
- 【LeetCode】19、 删除链表的倒数第 N 个结点
- 在IPv6中 链路本地地址的优势
- 0 basic learning C language - interrupt
- China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
- Should novice programmers memorize code?
- leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
猜你喜欢

树的先序中序后序遍历

如何用程序确认当前系统的存储模式?

Build op-tee development environment based on qemuv8

Senior soft test (Information System Project Manager) high frequency test site: project quality management

(18) LCD1602 experiment

软考高级(信息系统项目管理师)高频考点:项目质量管理

剪映+json解析将视频中的声音转换成文本
Learn the principle of database kernel from Oracle log parsing

NPDP认证|产品经理如何跨职能/跨团队沟通?

Chapter 3: detailed explanation of class loading process (class life cycle)
随机推荐
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
ThreadLocal详解
GD32F4XX串口接收中断和闲时中断配置
SQL Server生成自增序号
case 关键字后面的值有什么要求吗?
12、 Start process
[线性代数] 1.3 n阶行列式
NetXpert XG2帮您解决“布线安装与维护”难题
Should novice programmers memorize code?
Aardio - 不声明直接传float数值的方法
config:invalid signature 解决办法和问题排查详解
Jafka来源分析——Processor
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
UDP编程
Web APIs DOM time object
小程序系统更新提示,并强制小程序重启并使用新版本
Comparison between variable and "zero value"
MySQL数据库基本操作-DML
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
npm无法安装sharp