01 代码美学
Less than 1 minute
字典映射
def identify_attribute(s):
if re.search('Day1', s):
return 'Day1'
elif re.search('Day2', s):
return 'Day2'
else:
return 'None'
which_template = identify_attribute(attendee.business_matching_meeting)
if which_template == 'Day1':
template = 'Confirmation Letter + Day 1 Meeting.html'
elif which_template == 'Day2':
template = 'Confirmation Letter - Day 2 Meeting.html'
else:
template = 'Confirmation Letter - Exhibition.html'
帮我优化下这段代码优化后
def identify_attribute(s):
patterns = {'Day1': 'Day1', 'Day2': 'Day2'}
for pattern, attribute in patterns.items():
if re.search(pattern, s):
return attribute
return 'None'
template_mapping = {
'Day1': 'Confirmation Letter + Day 1 Meeting.html',
'Day2': 'Confirmation Letter - Day 2 Meeting.html',
'None': 'Confirmation Letter - Exhibition.html'
}
which_template = identify_attribute(attendee.business_matching_meeting)
template = template_mapping[which_template]
await fm.send_message(message, template_name=template)switch 不用也罢! 字典看的不是更舒服吗
