首页 > 解决方案 > 调试 Python 条件语句

问题描述

我正在尝试使 python 代码停止发送到 main_branch,而是继续搜索位于其自己区域内的确切分支。问题是代码仅在分支“打开”(或另一个简单的术语是活动的)时将其发送到分支,但是当分支“关闭”(离线)时,它将使其将项目发送到 main_branch 但是目的是每隔 10 秒继续搜索确切的分支,直到分支打开(上线)。这是我现在拥有的:

address_data = self.get_address_data()
branchAddress = address_data.get("branch")
log.trace("{0!r}".format(address_data))
if branchAddress:
    branch_name = []
    # create a list of all available branch addresses
    for b in branchAddress:
        branch_name += [(b["company"]["postal_code"], b["company"]["shipment_item"])]

    log.info("Available addresses: {0}".format(", ".join(
        ["{0} ({1})".format(k, v) for k, v in branch_name])))

    # check if the address is valid,
    # if not search for address from available branches in the list
    if (self.get_option("branch")
            and not self.get_option("branch") in [v[0] for v in branch_name]):

        # print the main branch as 0
        log.info("0 - {0} ({1})".format(
            address_data["main_branch"]["company"]["postal_code"],
            address_data["main_branch"]["company"]["shipment_item"]))
        # print all other branches
        for i, item in enumerate(branch_name, start=1):
            log.info("{0} - {1} ({2})".format(i, item[0], item[1]))
        ###############################
        #What needs to be done: keep a constant look for the exact branch.
        #Problem: when branch is still 'closed', it automatically ships it back
        #to the Main branch instead of keeping the search on the branches for an exact match
        for c in branch_name:
            if c["company"]["postal_code"] == self.get_option("branch"):
                # if someone recently added during the time the Shipment software
                # was used, the item might not be in the JSON data
                item_purchase = b.get("item_purchase")
                if item_purchase:
                return self.item_address(item_purchase["warehouse"])
            else:
                log.info("no match found, will begin searching in 10 seconds")
                time.sleep(10)
                continue
        log.info("Wait 10 seconds")
        time.sleep(10)

        ###############################
# ship to branch address
if branchAddress and self.get_option("branch"):
    for b in branchAddress:
        if b["company"]["postal_code"] == self.get_option("branch"):
            # if someone recently added during the time the Shipment software
            # was used, the item might not be in the JSON data
            item_purchase = b.get("item_purchase")
            if item_purchase:
                return self.item_address(item_purchase["warehouse"])

# ignore the main branch stream, if a branch is selected
# or use it when there are no other branchAddress
if not self.get_option("branch") or not branchAddress:
      return self.item_address(address_data["main_branch"]["item_purchase"]["warehouse"])

我的尝试是########## 行之间的代码块,但过了一会儿,我意识到也许我不需要它,因为问题可能位于这部分代码的其他地方。我认为问题是条件语句之一,主要是最后一个如果不是 self.get_option("branch") 或不是 branchAddress:但我不确定要更改什么,因为其他人编写了本节而不是我。

编辑:不确定这是否会有所帮助,但这就是发生的事情。主分支总是在其他分支打开之前先打开。在主分支中可用的分支列表是南、西、东。但是,如果预期的搜索是针对离线的北分支,它会将其发送到主分支。我试图改为尝试在一段时间内寻找北分支,直到它最终显示在可用的分支列表中。

标签: pythonfor-loopif-statementreturnconditional-statements

解决方案


好的,我解决了这个问题,并且只能在下订单后开张的分店试运行后才能确认。

这是最后一个条件语句,我只需要通过编辑 this if not self.get_option("branch") or not branchAddress: to this: if not self.get_option("branch")来替换条件语句

“or not branchAddress:” 使货件进入主分店而不是将代码循环回它尝试再次匹配列表的位置。


推荐阅读