最新消息:20210917 已从crifan.com换到crifan.org

【已解决】优化multipleRetry逻辑:当不成功时返回False而非None

优化 crifan 424浏览 0评论
折腾:
【未解决】百度OCR自动化操作游戏率土之滨从首充豪礼弹框页到真正支付页
期间,经过调试
之前的:
isRealPay = self.isRealPayPopupPage_multipleRetry()
返回竟然,不是以为的false,而是None
所以先去解决这个问题
发现还真的是之前函数有问题
utils/common_utils.py
    def multipleRetry(functionInfoDict, maxRetryNum=5, sleepInterval=0.1, isShowErrWhenFail=True, isRespFullRetValue=False):
。。。
            if doSuccess:
                if isRespFullRetValue:
                    finalReturnValue = respValue
                else:
                    finalReturnValue = doSuccess
                break
。。。
去改为:
            if isRespFullRetValue:
                finalReturnValue = respValue
            else:
                finalReturnValue = doSuccess


            if doSuccess:
                break
然后重新调试看看
然后就可以了:
返回False,而不是None了
最后完整代码:
    def multipleRetry(functionInfoDict, maxRetryNum=5, sleepInterval=0.1, isShowErrWhenFail=True, isRespFullRetValue=False):
        """do something, retry if single call failed, retry mutiple time until max retry number


        Args:
            functionInfoDict (dict): function info dict contain functionCallback and [optional] functionParaDict
            maxRetryNum (int): max retry number
            sleepInterval (float): sleep time (seconds) of each interval when fail
            isShowErrWhenFail (bool): show error when fail if true
            isRespFullRetValue (bool): whether return full return value of function call
        Returns:
            isRespFullRetValue=False: bool
            isRespFullRetValue=True: bool / tuple/list/...
        Raises:
        """
        finalReturnValue = None
        doSuccess = False
        functionCallback = functionInfoDict["functionCallback"]
        functionParaDict = functionInfoDict.get("functionParaDict", None)


        curRetryNum = maxRetryNum
        while curRetryNum > 0:
            if functionParaDict:
                # doSuccess = functionCallback(**functionParaDict)
                respValue = functionCallback(**functionParaDict)
            else:
                # doSuccess = functionCallback()
                respValue = functionCallback()
            
            doSuccess = False
            if isinstance(respValue, bool):
                doSuccess = bool(respValue)
            elif isinstance(respValue, tuple):
                doSuccess = bool(respValue[0])
            elif isinstance(respValue, list):
                doSuccess = bool(respValue[0])
            else:
                Exception("multipleRetry: Not support type of return value: %s" % respValue)


            if isRespFullRetValue:
                finalReturnValue = respValue
            else:
                finalReturnValue = doSuccess


            if doSuccess:
                break


            time.sleep(sleepInterval)
            curRetryNum -= 1


        if not doSuccess:
            if isShowErrWhenFail:
                functionName = str(functionCallback)
                # '<bound method DevicesMethods.switchToAppStoreSearchTab of <src.AppCrawler.AppCrawler object at 0x1053abe80>>'
                logging.error("Still fail after %d retry for %s", maxRetryNum, functionName)
        # return doSuccess
        return finalReturnValue
另外去更新之前的:
Python常用代码段
https://book.crifan.com/books/python_common_code_snippet/website/
中的代码:
multipleRetry
已更新:
通用逻辑 · Python常用代码段

转载请注明:在路上 » 【已解决】优化multipleRetry逻辑:当不成功时返回False而非None

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.183 seconds, using 23.45MB memory