优化随机数生成 数字可以生成float,int格式和str格式

This commit is contained in:
jing song 2022-05-07 09:54:30 +08:00
parent d1859a46ae
commit 7367243e1b
5 changed files with 39 additions and 19 deletions

View File

@ -178,7 +178,8 @@ QQ 529548204
### 1.4 生成随机数据介绍
部分数据采用faker库生成
```python
int_num = "$RandomPosInt(1,333)$" # 267
int_num = "$RandomInt(1,333)$" # 267 int格式 不可和字符串拼接
int_num2 = "$RandomPosInt(1,333)$" # '267' 字符串格式
str_num = '$RandomString($RandomPosInt(2,23)$)$$RandomPosInt(1,333)$' # AbE3c14580f29aDFe5
float_num = '$RandomFloat($RandomPosInt(2,13)$,$RandomPosInt(2,13)$,$RandomPosInt(2,13)$)$' # 11.84864
time_num = '$GetTime(time_type=else,layout=%Y-%m-%d %H:%M:%S,unit=0,0,0,0,0)$' # 当前时间 2022-04-14 13:27:01

View File

@ -167,6 +167,7 @@ class Cache(object):
def locallcache(self, data, bodys=None, res=None, cookie=None):
"""
本地缓存方法
:param cookie:
:param data:
:param bodys:
:param res:

View File

@ -1,6 +1,7 @@
# coding:utf-8
import datetime
import hashlib
import json
import random
import re
import time
@ -230,9 +231,10 @@ def replace_random(value, res=None, param=None):
:return:
"""
posint_list = re.findall(r"\$RandomPosInt\(([0-9]*,[0-9]*?)\)\$", value)
int_list = re.findall(r'\$RandomInt\((-[0-9]*,[0-9]*?)\)\$', value)
int_list = re.findall(r'\"\$RandomInt\(([0-9]*,[0-9]*?)\)\$\"', value)
string_list = re.findall(r'\$RandomString\(([0-9]*?)\)\$', value)
float_list = re.findall(r'\$RandomFloat\(([0-9]*,[0-9]*,[0-9]*)\)\$', value)
float_list = re.findall(r'\"\$RandomFloat\(([0-9]*,[0-9]*,[0-9]*)\)\$\"', value)
posfloat_list = re.findall(r'\$RandomPosFloat\(([0-9]*,[0-9]*,[0-9]*)\)\$', value)
time_list = re.findall(r"\$GetTime\(time_type=(.*?),layout=(.*?),unit=([0-9],[0-9],[0-9],[0-9],[0-9])\)\$", value)
choice_list = re.findall(r"\$Choice\((.*?)\)\$", value)
sqljson_list = re.findall(r"\$json\((.*?)\)\$", value)
@ -242,14 +244,15 @@ def replace_random(value, res=None, param=None):
relevance_list = re.findall(r"\$relevance\((.*?)\)\$", value)
if len(int_list):
# 获取整型数据替换
# 获取整型数据替换 结果是int
for i in int_list:
pattern = re.compile(r'\$RandomInt\(' + i + r'\)\$')
pattern = re.compile(r'\"\$RandomInt\(' + i + r'\)\$\"')
key = str(random_int(i))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value, res ,param)
elif len(posint_list):
# 获取整型数据替换
# 获取整型数据替换 结果是字符串
for i in posint_list:
pattern = re.compile(r'\$RandomPosInt\(' + i + r'\)\$')
key = str(random_int(i))
@ -264,13 +267,20 @@ def replace_random(value, res=None, param=None):
value = replace_random(value, res,param)
elif len(float_list):
# 获取浮点数数据替换
# 获取浮点数数据替换 返回浮点
for i in float_list:
pattern = re.compile(r'\$RandomFloat\(' + i + r'\)\$')
pattern = re.compile(r'\"\$RandomFloat\(' + i + r'\)\$\"')
key = str(random_float(i))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value, res,param)
elif len(posfloat_list):
# 获取浮点数数据替换 返回str
for i in posfloat_list:
pattern = re.compile(r'\$RandomFloat\(' + i + r'\)\$')
key = str(random_float(i))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value, res,param)
elif len(time_list):
# 获取时间替换
for i in time_list:
@ -338,7 +348,7 @@ def replace_random(value, res=None, param=None):
if __name__ == '__main__':
int_num = "$RandomPosInt(1,333)$"
int_num = "'{'id':'$RandomInt(1,333)$}'"
str_num = '$RandomString($RandomPosInt(2,23)$)$$RandomPosInt(1,333)$'
float_num = '$RandomFloat($RandomPosInt(2,13)$,$RandomPosInt(2,13)$,$RandomPosInt(2,13)$)$'
time_num = '$GetTime(time_type=else,layout=%Y-%m-%d %H:%M:%S,unit=0,0,0,0,0)$'
@ -350,7 +360,7 @@ if __name__ == '__main__':
{"haha":"123"}
]
}
print(replace_random(jsons,res=d))
# print(replace_random(jsons,res=d))
js = """
select count(*) '$json($.data[-$RandomPosInt(1,5)$:])$' from (
select
@ -379,18 +389,26 @@ from alarm
"""
# a = json.dumps(ini_yaml("homePageData.yml")["companyAlarm"])
# print(type(ini_yaml("家庭详情.yml")[0]["data"]))
print(replace_random(int_num))
print(replace_random(str_num))
print(replace_random(float_num))
print(replace_random(time_num))
print(replace_random(choice_num))
t1 = "{'login': {'name': '登录', 'order': 1, 'token': False, 'file': False, 'case': [{'address': '/v1/oauth/login/password/', 'assert': {'jsonpath': None, 'sqlassert': None, 'time': 2, 'code': 200}, 'data': {'file': None, 'param': {'username': 'dl010', 'password': '$RandomInt(1,333)$'}, 'urlparam': None}, 'headers': {'Content-Type': 'application/json'}, 'host': 'HB', 'info': '代理账号登录', 'method': 'POST', 'cookie': True, 'cache': None, 'relevance': None}]}}"
d1 = replace_random(t1)
d2 = eval(d1)
print(t1)
print(d2)
print(type(d2["login"]["case"][0]["data"]["param"]["password"]))
print(d2["login"]["case"][0]["data"]["param"]["password"])
# print(replace_random(str_num))
# print(replace_random(float_num))
# print(replace_random(time_num))
# print(replace_random(choice_num))
res1 = {'code': 0, 'msg': 'ok',
'data': [{'time': '2021-08-18', 'number': None}, {'time': '2021-08-19', 'number': None},
{'time': '2021-08-20', 'number': 1}, {'time': '2021-08-21', 'number': None},
{'time': '2021-08-22', 'number': None}, {'time': '2021-08-23', 'number': 9}]}
idc = "$faker(name)$"
print(replace_random(idc))
# print(replace_random(idc))
t = "$GetTime(time_type=past,layout=13timestampDAY,unit=0,0,0,1,4)$"
# print(replace_random(choice_num))
# print(replace_random(urls,param={"home_id":"$caches(haha)$"}))
@ -414,4 +432,4 @@ from alarm
# a = replace_random(str(d),param=p)
# print(a)
time_num2 = '$GetTime(time_type=past,layout=%Y-%m-%d %H:%M:%S,unit=0,0,0,3,0)$' # 当前时间 2022-04-14 13:27:01
print(replace_random(time_num2))
print(replace_random(time_num2))

View File

@ -55,7 +55,7 @@ if __name__ == '__main__':
# print(datapath)
# get_yaml_data(r"F:\api2.0\config\runConfig.yml")
d = ini_allyaml()
print(d)
print(type(d))
# # case_level = runConfig_dict[0]["address"].format(**{"home_id": "123"})
# print(runConfig_dict)

View File

@ -5,7 +5,7 @@ from email.header import Header
from email.mime.text import MIMEText
from config.confManage import mail_manage
from scripts.log import Log
from util.tools.log import Log
Log()