不等长的 Zip 列表(dict 和 list)组成 API 个调用

Zip lists (dict & list) of unequal length to compose API calls

有两个函数:

[['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
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 

执行此操作的最简单方法是强制您的示例用户列表的长度等于您的 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 = ... 您应该拥有所需的元素,正确关联为 urlpayloaduser_typeuser_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