【发布时间】:2021-12-08 13:48:15
【问题描述】:
我正在尝试使 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"])
我的尝试是 ########## 行之间的代码块,但过了一会儿,我意识到也许我不需要它,因为问题可能位于本节的其他地方代码。我认为问题是条件语句之一,主要是最后一个 if not self.get_option("branch") or not branchAddress: 但我不确定在其他人编写本节代码时要更改什么不是我。
编辑:不确定这是否会有所帮助,但这就是发生的情况。主分支总是在其他分支打开之前先打开。在主分支中可用的分支列表是南、西、东。但是,如果预期的搜索是针对离线的北分支,它会将其发送到主分支。我试图改为尝试在一段时间内寻找北分支,直到它最终显示在可用的分支列表中。
【问题讨论】:
-
仅供参考:循环结束时不需要
continue。这就是默认情况下发生的情况。 -
return self.item_address(item_purchase["warehouse"])退出整个函数,剩余的if语句不执行。您可能希望break结束循环,而不是整个函数。 -
您在循环的每次迭代中都打印
no match found,直到找到匹配的分支。只有在循环结束但没有找到匹配项时才应该打印它。 -
代码中没有任何内容引用
open或closed分支。 -
“继续”是因为我有“返回”只是为了让我看到它确实继续。我也忘了在我刚刚为“return self.item_address(item_purchase["warehouse"])”添加的退货上添加缩进。 “未找到匹配项”位于循环内部的原因只是为了在我解决解决方案之前进行确认,我计划稍后将其删除。 “开”和“关”分店实际上是指分店开张并有软件运行的一面。 @Barmar
标签: python for-loop if-statement return conditional-statements