Recently, I encountered a problem in the test , When doing the performance test, we need to simulate a large number of orders to carry out the previous data accumulation , In the process of generating a large number of orders , because
The system requires that the order code cannot be repeated , In line with the principle of good prosecution that the number of orders can be marked , We give up random generation , I wrote a small function to encode by changing the base .
data_len=6 #change Function to modify the original decimal data to multi base , Can be 2-62, It can also be expanded , It needs to be modified dic Quantity contained in , Returns the string type
#data: Decimal data a: Convert to decimal , According to alphanumeric combination 2-62 Base system def change(data,a): dic = ['0', '1', '2',
'3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] data_len=len(dic) if a<2:
a=2 elif a>data_len: a=data_len if data < 0: print(" Input number error !!!") else: mid_data
= [] while True: mid_data.append(data%a) data = data//a if data==0: break
# If only the number is not repeated , You can delete this step , If scalability is considered , Keep this step mid_data.reverse() new_data="" for c in mid_data:
new_data+=dic[c] return new_data #data function : Transfer old data (old_data) Generates a string of specified digits (data_len) def
data(old_data,data_len): data = old_data + 1 new_data = change(data,32)
new_data_len= len(new_data) if new_data_len>data_len: print(" The data is too large to generate ")
finish_data="err" elif new_data_len==data_len: finish_data=new_data else:
finish_data= new_data.rjust(data_len,"0") return data,finish_data # Check it out for a in
range(352161460,352161468): print(data(a,data_len)) print(f" In total, it can be generated
{36**data_len} Data ")
We just need to make sure in advance data_len, The system to be converted a, That is, the length of the string to be output , We can get the data we want .
be careful , This method needs to record the previous data to generate the next data , If you need to modify it, you can modify it by yourself
Technology