我尝试实现与“ Simplify a if - if ladder - python”相同的逻辑,该逻辑先前曾要求简化“ if else”梯形图。但是对于我的一生,我无法简化这一过程。
for each_component in component_objects:
if each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'pe' and each_component['name'] == 'PE1':
env_data['PE1_HOSTNAME'] = each_component['hostname'].split('.')[0]
elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'pe' and each_component['name'] == 'PE2':
env_data['PE2_HOSTNAME'] = each_component['hostname'].split('.')[0]
elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versadirector' and each_component['name'] == 'VersaDirector':
env_data['VD_HOSTNAME'] = each_component['hostname'].split('.')[0]
elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'jump' and each_component['name'] == 'Jump':
jump_ips = json.loads(each_component['ip_addr'])
elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versaanalytics' and each_component['name'] == 'VersaAnalytics1':
analytics_1 = each_component['hostname'].split('.')[0]
elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versaanalytics' and each_component['name'] == 'VersaAnalytics2':
analytics_2 = each_component['hostname'].split('.')[0]
elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versaanalytics' and each_component['name'] == 'VersaAnalytics3':
analytics_3 = each_component['hostname'].split('.')[0]
elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versaanalytics' and each_component['name'] == 'VersaAnalytics4':
analytics_4 = each_component['hostname'].split('.')[0]
我正在尝试生成 env_data 词典和变量 analytics_X 。 现在,发出此请求的原因是因为这段代码在通过代码质量工具运行时会发出严重警告,并且需要进一步简化以通过代码质量检查。 有任何想法吗?在这里帮助
答案 0 :(得分:0)
您重复了很多检查。不要那样做为简化起见,只需在重复的if
语句下嵌套唯一的if
:
for each_component in component_objects:
if each_component['region_type'] == deploy_json['env_type'].upper():
if each_component['component_type'] == 'pe':
if each_component['name'] == 'PE1':
env_data['PE1_HOSTNAME'] = each_component['hostname'].split('.')[0]
elif each_component['name'] == 'PE2':
env_data['PE2_HOSTNAME'] = each_component['hostname'].split('.')[0]
elif each_component['component_type'] == 'versadirector' and each_component['name'] == 'VersaDirector':
env_data['VD_HOSTNAME'] = each_component['hostname'].split('.')[0]
elif each_component['component_type'] == 'jump' and each_component['name'] == 'Jump':
jump_ips = json.loads(each_component['ip_addr'])
elif each_component['component_type'] == 'versaanalytics':
if each_component['name'] == 'VersaAnalytics1':
analytics_1 = each_component['hostname'].split('.')[0]
elif each_component['name'] == 'VersaAnalytics2':
analytics_2 = each_component['hostname'].split('.')[0]
elif each_component['name'] == 'VersaAnalytics3':
analytics_3 = each_component['hostname'].split('.')[0]
elif each_component['name'] == 'VersaAnalytics4':
analytics_4 = each_component['hostname'].split('.')[0]
这几乎可以简化。如果要进一步简化,应该使用函数,因为当前有很多重复的任务。任何重复的内容都可以用一个函数代替。