【问题标题】:Calling a function in a loop of another function在另一个函数的循环中调用一个函数
【发布时间】:2022-01-20 12:32:43
【问题描述】:

背景 - 我有一个函数从另一个函数 (api_response = api_call()) 获取 API 响应 (api_response)。 Code Block #1 继续迭代,直到 meta 出现在响应中。一旦metaapi_response 中,那么Code Block #2 将返回api_response

问题 - 虽然我的代码正在为 meta 测试 api_response,但我还没有找到一种方法来包括连续调用另一个函数(api_response = api_call() 在我的 while loop 中,它将允许确定meta 何时仍在api_response 中。

功能 -

def unpack_response():
    # Code Block # 1
    api_response = api_call()
    while "meta" not in api_response:
        id_value = "id"
        res1 = [val[id_value] for key, val in api_response.items() if id_value in val]
        id_value = "".join(res1)
        percent_value = "percent_complete"
        res2 = (tuple(api_response["data"]["attributes"].get("percent_complete", '') for key, val in api_response.items()))
        print(f' Your data requested, associated with ID: {id_value} is {res2} complete!')
    # Code Block # 2
    if "meta" in api_response:
        return api_response
unpack_response()

问题 - 有没有人有任何指示,关于我如何将api_response 集成到我的循环中,所以它在每次循环迭代时都会被调用?

【问题讨论】:

  • 将调用放入循环中,使用else,您的函数不会返回任何内容
  • 使用海象运算符
  • 感谢@MadPhysicist - 我已将函数更正为return api_response。 Inc. 循环内的调用是有道理的(谢谢)。但是else 在哪里发挥作用?
  • if not x: ... 后跟if x: ... 通常更好地表述为if not x: ... else: ...
  • 如果我的回答让事情变得更清楚,请告诉我

标签: python while-loop calling-convention


【解决方案1】:

不确定我是否理解正确。 你能做到吗?

def unpack_response():
    # Code Block # 1
    while True:
        api_response = api_call()
        if "meta" not in api_response:
            id_value = "id"
            res1 = [val[id_value] for key, val in api_response.items() if id_value in val]
            id_value = "".join(res1)
            percent_value = "percent_complete"
            res2 = (api_response["data"]["attributes"].get("percent_complete", '') for key, val in api_response.items())
            print(f' Your data requested, associated with ID: {id_value} is {percent_value} complete!')
            time.sleep(5)
        # Code Block #2
        else:
            print(api_response)
            break

【讨论】:

  • 这里的调用显然不在循环中...
  • 我只是意识到我粘贴了错误的脚本;)
  • 现在你的代码缩进不正确
  • 你为什么建议睡 5 秒?
  • sleep 在原始问题中,但已被删除。
【解决方案2】:

这里有几个选项。如果你有python 3.8+,你可以使用walrus operator (:=)api_call直接放在循环的条件中:

def unpack_response():
    while "meta" not in (api_response := api_call()):
        id_value = "id"
        res1 = [val[id_value] for key, val in api_response.items() if id_value in val]
        id_value = "".join(res1)
        percent_value = "percent_complete"
        res2 = (tuple(api_response["data"]["attributes"].get("percent_complete", '') for key, val in api_response.items()))
        print(f' Your data requested, associated with ID: {id_value} is {res2} complete!')
    return api_response

一种更传统但同样惯用的方法是永远运行循环并在达到正确条件时break

def unpack_response():
    while True:
        api_response = api_call()
        if "meta" in api_response:
            return api_response
        id_value = "id"
        res1 = [val[id_value] for key, val in api_response.items() if id_value in val]
        id_value = "".join(res1)
        percent_value = "percent_complete"
        res2 = (tuple(api_response["data"]["attributes"].get("percent_complete", '') for key, val in api_response.items()))
        print(f' Your data requested, associated with ID: {id_value} is {res2} complete!')

在这两种情况下,您都可以考虑在睡眠或类似情况下降低 API 调用的速度。

【讨论】:

    猜你喜欢
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多