【发布时间】:2022-01-21 19:37:53
【问题描述】:
我有一个 API 可以返回 Item 的商店可用性,如果任何商店返回 200,则应将产品视为 Ready To Purchase,并将 Not Ready 视为所有其他状态代码(包括 404)。
{
"status": 200,
"sku": "svk__21w",
"store_data": [
{
"status": 404,
"reason": "no product found, or product is not available, or not in inventory"
},
{
"status": 404,
"reason": "no product found, or product is not available, or not in inventory"
},
{
"status": 404,
"reason": "no product found, or product is not available, or not in inventory"
}
]
}
现在,在我的代码中,要检查这个产品是否确实是Ready To Purchase,我使用for 循环:
is_product_available = False
for store in data['store_data']:
if 'data' in store and store['status'] == 200:
print("Product state is ready to purchase")
is_product_available = True
else:
print("Product state is not ready to purchase")
这行得通,但我认为这只是糟糕的设计,未来,随着更多商店的开业......
我想要什么:
- 更好的方法来检查是否有任何商店有
data,它们的代码是200,如果是,产品是Ready To Purchase。
即买即用产品示例:
store_data:
store_1: 404
store_2: 404
store_3 200
未准备好购买的产品示例:
store_data:
store_1: 404
store_2: 404
store_3 404
【问题讨论】:
-
if 'data' in store的目的是什么?没有一家商店有'data'密钥。这对你有什么用? -
@Tomerikoo 有时数据在存储中,但状态码不同。
-
@Tomerikoo 我知道,就像我说的那样,有时其中一家商店会报告可用性并在其中包含
data
标签: python