我有以下python脚本:
query_parser.py
#!/usr/bin/env python
def count_resources_per_category(resource_names):
APPS_MAP = {
'billing': ["paypal", "checkout", "cart", "order-service"],
'browse': ["content", "localization", "pricing", "search",],
'some_category': ["random-1", "random-2"],
}
results_map = {}
for key, _ in APPS_MAP.items():
results_map[key] = 0
results_map['other'] = 0
# now we have:
# results_map = {
# 'checkout': 0,
# 'browse': 0,
# 'MCM': 0,
# 'other': 0
# }
for resource_name in resource_names:
FOUND_IT = False
for key, app_names in APPS_MAP.items():
if FOUND_IT:
break
for app_name in app_names:
if app_name in resource_name:
results_map[key] += 1
FOUND_IT = True
break
if not FOUND_IT:
results_map['other'] += 1
return results_map
def determine_if_resource_count_issue(results_map):
RESULT_FOUND = False
for key, count in results_map.items():
if count:
if RESULT_FOUND:
print("The script isn't gonna work as planned")
return True
RESULT_FOUND = True
print("All good :)")
return False
#--------------------------------------------------------------------
""" TESTS """
# test_count_resources_per_category():
test_input = [
'some-prefix-pricing', # browse
'some-prefix2-cart', # billing
'content', # browse
'some-random-thing-not-found' # other
]
expectation = {
'browse': 2,
'billing': 1,
'some_category': 0,
'other': 1
}
result = count_resources_per_category(test_input)
assert result == expectation
#--------------------------------------------------------------------
# test_determine_resource_count_issue_passes():
test_input = {
'browse': 234234234,
'billing': 0,
'some_category': 0,
'other': 0
}
assert False == determine_if_resource_count_issue(test_input)
#--------------------------------------------------------------------
# test_determine_resource_count_issue_fails():
test_input = {
'browse': 2,
'billing': 1,
'some_category': 0,
'other': 1
}
assert True == determine_if_resource_count_issue(test_input)
#--------------------------------------------------------------------
我想知道处理这些嵌套循环的干净,诡计的方法是什么,没有FOUND_IT
和RESULT_FOUND
标志。谢谢