不等长的 Zip 列表(dict 和 list)组成 API 个调用
Zip lists (dict & list) of unequal length to compose API calls
有两个函数:
ReadInp() -> Dict[str, str]
这会读取一个 csv 和 returns 一个字典,其中 url 作为键,有效载荷作为值。
ReadTUsr() -> List[List[str, str]]
这会读取一个单独的 csv 和 returns 'sample users' 作为列表的列表。每个内部列表都是一个 id 类型和一个 id 值。例如:
[['uId', 'T1'], ['srId', 'T2'], ['siId', 'T3'], ['siId', 'T4'], ['srId', 'T5']...]
我需要进行 url 连接这两个值的调用。调用的 url 将来自第一个 return 值,但它必须从第二个列表中拼接样本用户。这些列表的大小可能不同,如果第二个在第一个之前用完,它应该再次遍历列表直到第一个用完。
例如,调用结果如下:
http://localhost:8080/cabApp/callcab?uId=T1
然后 POST 正文:
{'location': '', 'ppl': '2', 'text': 'MyCab'}
或:
http://localhost:8080/cabApp/call3cab?srId=T2
然后 POST 正文:{'display': 'RMP', 'time': '', 'location': 'LA'}
这是我试过的:
def makeAcall():
r = ReadInp() # gives dict with endpoint as key and payload as Value
t = ReadTUsr() # give nested user list
print(t)
print(type(r))
#for k1,v1 in r.items():
# for i,j in t:
for (k1,v1), (i,j) in zip(r.items(), t):
url = f"{AURL}(k1)}"
rj = v1
#uid = {"uId": "T1"}
headers = {'Accept': "application/json"}
res = requests.post(url, json=rj, headers=headers, params=(i,j))
print("Req Resp Code:",res.status_code)
#api_req_res_text.append(res.text)
api_req_res_code.append(res.status_code)
return api_req_res_code
- Note1: 这就是我的字典的样子
{'cabApp/callcab1': {'location': '', 'ppl': '2', 'text': 'MyCab'}, 'cab2App/call2cab2': {'ppl': '', 'text': 'Cab2'}, 'cabApp/call3cab': {'display': 'RMP', 'time': '', 'location': 'LA'}...}
这里 cabApp/callcab1, cab2App/call2cab2 & cabApp/call3cab 是键,它们是端点[在代码中迭代为k1],其余的都是它们各自的有效载荷[分别迭代为v1]。 http://localhost:8080 部分存储在 AURL 中,是静态的。我无法根据我发出 post 请求的指令 [res 更新构建 API 的代码。我正在寻找的是迭代列表并将其用作 param. 之后的值的方法
- Note2:我们可以改变它读取样本用户csv的方式。现在我正在使用这段代码来读取示例用户 csv,它给出了嵌套列表。
def ReadTUsr():
with open(T_U_csv, 'r') as file:
csv_file = csv.reader(file)
data = [it for it in csv_file]
#print(data)
return data
- Note3:之前的 ReadTUsr() 是 dict 并且按预期工作(能够为每个迭代并调用新的示例用户请求)但是最新的代码在字典中有重复的键(多次 siId & srId)所以它只从重复的键中获取一条记录,这是默认的 python行为。
- Que 如何迭代 样本用户 (i,j) 以及 端点和有效负载 (k1,v1) 同时为 res 变量调用每个 post 请求。
执行此操作的最简单方法是强制您的示例用户列表的长度等于您的 url 列表:
def equalize_lists(users: list, desired_length) -> list:
if len(users) > desired_length:
return users[:desired_length]
else: # We need to extend the user list
repeat, extra = (desired_length // len(users), desired_length % len(users))
for _ in range(repeat):
users.extend(users)
users.extend(users[:extra]
return users
def create_test_calls():
urls = ReadInp()
test_users = ReadTUsr()
users = equalize_lists(test_users, len(urls)
for ((url, payload), (user_type, user_id)) in zip(urls.items(), users):
endpoint = f"{AURL}{url}"
... etc.
有点不清楚您是如何构建端点的,但是在此处 endpoint = ...
您应该拥有所需的元素,正确关联为 url
、payload
、user_type
和 user_id
。我建议使用描述性名称!它可以使调试更容易。
您可以使用itertools.cycle循环用户列表:
from itertools import cycle
# 20 uris with different ports usable for str.format() by supplying
# with u=uri and v=value
uris = [f"http://localhost:{port}/cabApp/callcab?{{u}}={{v}}"
for port in range(10000,10020)]
# u,v lists / tuples whatever
users = [['uId', 'T1'], ['srId', 'T2'], ['siId', 'T3'],
['siId', 'T4'], ['srId', 'T5']]
# cycles around if at end
user_cycle = cycle(users)
# zip exhausts to the smaller list - wich is always uris now
# as user_cycle is "endless"
for idx, (uri, user) in enumerate( zip(uris,user_cycle), 1 ):
print(f"{idx:>2})", uri.format(u=user[0],v=user[1]))
输出:
1) http://localhost:10000/cabApp/callcab?uId=T1
2) http://localhost:10001/cabApp/callcab?srId=T2
3) http://localhost:10002/cabApp/callcab?siId=T3
4) http://localhost:10003/cabApp/callcab?siId=T4
5) http://localhost:10004/cabApp/callcab?srId=T5
6) http://localhost:10005/cabApp/callcab?uId=T1
7) http://localhost:10006/cabApp/callcab?srId=T2
8) http://localhost:10007/cabApp/callcab?siId=T3
9) http://localhost:10008/cabApp/callcab?siId=T4
10) http://localhost:10009/cabApp/callcab?srId=T5
11) http://localhost:10010/cabApp/callcab?uId=T1
12) http://localhost:10011/cabApp/callcab?srId=T2
13) http://localhost:10012/cabApp/callcab?siId=T3
14) http://localhost:10013/cabApp/callcab?siId=T4
15) http://localhost:10014/cabApp/callcab?srId=T5
16) http://localhost:10015/cabApp/callcab?uId=T1
17) http://localhost:10016/cabApp/callcab?srId=T2
18) http://localhost:10017/cabApp/callcab?siId=T3
19) http://localhost:10018/cabApp/callcab?siId=T4
20) http://localhost:10019/cabApp/callcab?srId=T5
有两个函数:
ReadInp() -> Dict[str, str]
这会读取一个 csv 和 returns 一个字典,其中 url 作为键,有效载荷作为值。ReadTUsr() -> List[List[str, str]]
这会读取一个单独的 csv 和 returns 'sample users' 作为列表的列表。每个内部列表都是一个 id 类型和一个 id 值。例如:
[['uId', 'T1'], ['srId', 'T2'], ['siId', 'T3'], ['siId', 'T4'], ['srId', 'T5']...]
我需要进行 url 连接这两个值的调用。调用的 url 将来自第一个 return 值,但它必须从第二个列表中拼接样本用户。这些列表的大小可能不同,如果第二个在第一个之前用完,它应该再次遍历列表直到第一个用完。
例如,调用结果如下:
http://localhost:8080/cabApp/callcab?uId=T1
然后 POST 正文:
{'location': '', 'ppl': '2', 'text': 'MyCab'}
或:
http://localhost:8080/cabApp/call3cab?srId=T2
然后 POST 正文:{'display': 'RMP', 'time': '', 'location': 'LA'}
这是我试过的:
def makeAcall():
r = ReadInp() # gives dict with endpoint as key and payload as Value
t = ReadTUsr() # give nested user list
print(t)
print(type(r))
#for k1,v1 in r.items():
# for i,j in t:
for (k1,v1), (i,j) in zip(r.items(), t):
url = f"{AURL}(k1)}"
rj = v1
#uid = {"uId": "T1"}
headers = {'Accept': "application/json"}
res = requests.post(url, json=rj, headers=headers, params=(i,j))
print("Req Resp Code:",res.status_code)
#api_req_res_text.append(res.text)
api_req_res_code.append(res.status_code)
return api_req_res_code
- Note1: 这就是我的字典的样子
{'cabApp/callcab1': {'location': '', 'ppl': '2', 'text': 'MyCab'}, 'cab2App/call2cab2': {'ppl': '', 'text': 'Cab2'}, 'cabApp/call3cab': {'display': 'RMP', 'time': '', 'location': 'LA'}...}
这里 cabApp/callcab1, cab2App/call2cab2 & cabApp/call3cab 是键,它们是端点[在代码中迭代为k1],其余的都是它们各自的有效载荷[分别迭代为v1]。 http://localhost:8080 部分存储在 AURL 中,是静态的。我无法根据我发出 post 请求的指令 [res 更新构建 API 的代码。我正在寻找的是迭代列表并将其用作 param. 之后的值的方法
- Note2:我们可以改变它读取样本用户csv的方式。现在我正在使用这段代码来读取示例用户 csv,它给出了嵌套列表。
def ReadTUsr():
with open(T_U_csv, 'r') as file:
csv_file = csv.reader(file)
data = [it for it in csv_file]
#print(data)
return data
- Note3:之前的 ReadTUsr() 是 dict 并且按预期工作(能够为每个迭代并调用新的示例用户请求)但是最新的代码在字典中有重复的键(多次 siId & srId)所以它只从重复的键中获取一条记录,这是默认的 python行为。
- Que 如何迭代 样本用户 (i,j) 以及 端点和有效负载 (k1,v1) 同时为 res 变量调用每个 post 请求。
执行此操作的最简单方法是强制您的示例用户列表的长度等于您的 url 列表:
def equalize_lists(users: list, desired_length) -> list:
if len(users) > desired_length:
return users[:desired_length]
else: # We need to extend the user list
repeat, extra = (desired_length // len(users), desired_length % len(users))
for _ in range(repeat):
users.extend(users)
users.extend(users[:extra]
return users
def create_test_calls():
urls = ReadInp()
test_users = ReadTUsr()
users = equalize_lists(test_users, len(urls)
for ((url, payload), (user_type, user_id)) in zip(urls.items(), users):
endpoint = f"{AURL}{url}"
... etc.
有点不清楚您是如何构建端点的,但是在此处 endpoint = ...
您应该拥有所需的元素,正确关联为 url
、payload
、user_type
和 user_id
。我建议使用描述性名称!它可以使调试更容易。
您可以使用itertools.cycle循环用户列表:
from itertools import cycle
# 20 uris with different ports usable for str.format() by supplying
# with u=uri and v=value
uris = [f"http://localhost:{port}/cabApp/callcab?{{u}}={{v}}"
for port in range(10000,10020)]
# u,v lists / tuples whatever
users = [['uId', 'T1'], ['srId', 'T2'], ['siId', 'T3'],
['siId', 'T4'], ['srId', 'T5']]
# cycles around if at end
user_cycle = cycle(users)
# zip exhausts to the smaller list - wich is always uris now
# as user_cycle is "endless"
for idx, (uri, user) in enumerate( zip(uris,user_cycle), 1 ):
print(f"{idx:>2})", uri.format(u=user[0],v=user[1]))
输出:
1) http://localhost:10000/cabApp/callcab?uId=T1
2) http://localhost:10001/cabApp/callcab?srId=T2
3) http://localhost:10002/cabApp/callcab?siId=T3
4) http://localhost:10003/cabApp/callcab?siId=T4
5) http://localhost:10004/cabApp/callcab?srId=T5
6) http://localhost:10005/cabApp/callcab?uId=T1
7) http://localhost:10006/cabApp/callcab?srId=T2
8) http://localhost:10007/cabApp/callcab?siId=T3
9) http://localhost:10008/cabApp/callcab?siId=T4
10) http://localhost:10009/cabApp/callcab?srId=T5
11) http://localhost:10010/cabApp/callcab?uId=T1
12) http://localhost:10011/cabApp/callcab?srId=T2
13) http://localhost:10012/cabApp/callcab?siId=T3
14) http://localhost:10013/cabApp/callcab?siId=T4
15) http://localhost:10014/cabApp/callcab?srId=T5
16) http://localhost:10015/cabApp/callcab?uId=T1
17) http://localhost:10016/cabApp/callcab?srId=T2
18) http://localhost:10017/cabApp/callcab?siId=T3
19) http://localhost:10018/cabApp/callcab?siId=T4
20) http://localhost:10019/cabApp/callcab?srId=T5