当前位置:网站首页>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 :

d5e665bcd2814e538f70129441e76d8d.png

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 :

c75f8ce8513a4c8bb81ebd9144fe2754.png


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 called f-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 variable r The value of ,{s:.2f} The dependent variable s The value of , also : hinder .2f Format parameters specified ( That is, keep two decimal places ), therefore ,{s:.2f} The result of the replacement is 19.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

 aardio Debugging libraries solve interesting problems with local variables ( One )_aardio Blog of notes -CSDN Blog _aardio

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 :

b006b289467b42faa4cd60a38c7e9a53.png

or :

var s = f(
` full name :{console.name}
 Age :{age}
 Gender :{sex}
 achievement :{score}
 result :{t}
 remarks :{_OK}`)

  Execution results :

cec35cc49297445cbb384cd5256421d5.png


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 :

http://chengxu.online

aardio Download resources _.rar , Decompress it and put it in \lib\godking Directory .

原网站

版权声明
本文为[Lu Guangqing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061439345794.html