调试 Python 条件语句
debugging Python conditional statement
我试图让 python 代码停止发送到 main_branch,而是继续搜索位于其自己区域内的确切分支。问题是代码只在分支为 'open'(或另一个简单的术语是实时的)时将它发送到分支,但是当分支为 'closed'(离线)时它会发送项目到 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: 但我不确定要改变什么else 编码了这部分而不是我。
编辑:不确定这是否有帮助,但情况是这样的。总分行总是先开门,然后其他分行才会开门。在主要分支中,可用的分支列表是南、西、东。但是,如果预期的搜索是针对离线的北分支,它会将其发送到主分支。我正在尝试尝试在一个时间间隔内寻找北分支,直到它最终显示在可用的分支列表中。
好的,我解决了这个问题,只是在试营业运行分店后才确认运行
这是最后一个条件语句,我只需要通过编辑它来替换条件语句
if not self.get_option("branch") or not branchAddress:
对此:
如果不是self.get_option("分支")
“or not branchAddress:”使它成为真实的,这导致货物被送到主要的分店,而不是将代码循环回它试图再次匹配列表的地方。
我试图让 python 代码停止发送到 main_branch,而是继续搜索位于其自己区域内的确切分支。问题是代码只在分支为 'open'(或另一个简单的术语是实时的)时将它发送到分支,但是当分支为 'closed'(离线)时它会发送项目到 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: 但我不确定要改变什么else 编码了这部分而不是我。
编辑:不确定这是否有帮助,但情况是这样的。总分行总是先开门,然后其他分行才会开门。在主要分支中,可用的分支列表是南、西、东。但是,如果预期的搜索是针对离线的北分支,它会将其发送到主分支。我正在尝试尝试在一个时间间隔内寻找北分支,直到它最终显示在可用的分支列表中。
好的,我解决了这个问题,只是在试营业运行分店后才确认运行
这是最后一个条件语句,我只需要通过编辑它来替换条件语句 if not self.get_option("branch") or not branchAddress: 对此: 如果不是self.get_option("分支")
“or not branchAddress:”使它成为真实的,这导致货物被送到主要的分店,而不是将代码循环回它试图再次匹配列表的地方。