调试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:但我不确定要改变什么,因为其他人编写了这一部分,而不是我。

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

好的,我解决了这个问题,只是在分店开业后的试运行之后才能够确认,而不是发货订单。

这是最后一个条件语句,我只需要通过编辑这个来替换条件语句如果没有self.get_option("branch")或没有branchAddress::如果没有self.get_option("branch")

> branchAddress:"使其为真,从而导致货物转到主要分支商店,而不是循环返回代码,以再次尝试匹配列表。

最新更新