除了不理解功能外,你还试过吗?

2024-06-08 14:00:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在预处理某些数据时遇到此有效错误:

 9:46:56.323 PM default_model Function execution took 6008 ms, finished with status: 'crash'
 9:46:56.322 PM default_model Traceback (most recent call last):
  File "/user_code/main.py", line 31, in default_model
    train, endog, exog, _, _, rawDf = preprocess(ledger, apps)
  File "/user_code/Wrangling.py", line 73, in preprocess
    raise InsufficientTimespanError(args=(appDf, locDf))

发生在这里:

async def default_model(request):
    request_json = request.get_json()
    if not request_json:
        return '{"error": "empty body." }'
    if 'transaction_id' in request_json:
        transaction_id = request_json['transaction_id']

        apps = []  # array of apps whose predictions we want, or uempty for all
        if 'apps' in request_json:
            apps = request_json['apps']

        modelUrl = None
        if 'files' in request_json:
            try:
                files = request_json['files']
                modelUrl = getModelFromFiles(files)
            except:
                return package(transaction_id, error="no model to execute")
        else:
            return package(transaction_id, error="no model to execute")

        if 'ledger' in request_json:
            ledger = request_json['ledger']

            try:
                train, endog, exog, _, _, rawDf = preprocess(ledger, apps)
            # ...
            except InsufficientTimespanError as err:
                return package(transaction_id, error=err.message, appDf=err.args[0], locDf=err.args[1])

预处理正确地抛出了我的自定义错误:

def preprocess(ledger, apps=[]):
    """
    convert ledger from the server, which comes in as an array of csv entries.
    normalize/resample timeseries, returning dataframes
    """
    appDf, locDf = splitLedger(ledger)

    if len(appDf) < 3 or len(locDf) < 3:
        raise InsufficientDataError(args=(appDf, locDf))

    endog = appDf['app_id'].unique().tolist()
    exog = locDf['location_id'].unique().tolist()

    rawDf = normalize(appDf, locDf)
    trainDf = cutoff(rawDf.copy(), apps)
    rawDf = cutoff(rawDf.copy(), apps, trim=False)

    # TODO - uncomment when on realish data
    if len(trainDf) < 2 * WEEKS:
        raise InsufficientTimespanError(args=(appDf, locDf))

问题是,它位于try``except块中,正是因为我想捕获错误并返回带有错误的有效负载,而不是因为500个错误而崩溃。但它在我的自定义错误崩溃,在尝试块,无论如何。就在电话那头preprocess。你知道吗

对于我来说,这一定是一个失败,无法符合正确的python代码。但我不确定我做错了什么。环境是python3.7

这就是错误的定义,在争吵.py地址:

class WranglingError(Exception):
    """Base class for other exceptions"""
    pass


class InsufficientDataError(WranglingError):
    """insufficient data to make a prediction"""

    def __init__(self, message='insufficient data to make a prediction', args=None):
        super().__init__(message)
        self.message = message
        self.args = args


class InsufficientTimespanError(WranglingError):
    """insufficient timespan to make a prediction"""

    def __init__(self, message='insufficient timespan to make a prediction', args=None):
        super().__init__(message)
        self.message = message
        self.args = args

下面是如何主.py声明(导入)它:

from Wrangling import preprocess, InsufficientDataError, InsufficientTimespanError, DataNotNormal, InappropriateValueToPredict

Tags: appsinidjsonmessagemodelifrequest
2条回答

错误中的行号与代码中的行号匹配吗?如果不是,那么在添加try…except之前,是否有可能从某个版本的代码中看到错误?你知道吗

您的preprocess函数被声明为async。这意味着它中的代码实际上不是在调用preprocess的地方运行的,而是在它最终被await编辑或传递到主循环(如asyncio.run)的时候运行的。因为运行异常的位置不再位于default_model中的try块中,所以不会捕获异常。你知道吗

您可以通过以下几种方式解决此问题:

  • 使preprocess不异步
  • 使default_model也异步,并且awaitpreprocess上。你知道吗

相关问题 更多 >