python - 如何使复杂的 if 条件可维护,因为 Python 不支持 if 条件中的赋值?
问题描述
我有以下 Python 代码,其中函数 print_processed_username 包含一个 if/else 构造。由于重复的正则表达式,if 行很长。如果需要对正则表达式进行修改,则必须在该正则表达式的每次出现(包括对 process_the_username 的调用)中进行相同的修改,这使得代码难以维护。
import re
def process_the_username(username):
return 'e' + username[1:]
def print_processed_username(args):
if len(args) == 1 and type(args[0]) is str and re.compile(r'[\'"]username[\'"]: ?[\'"](\S*)[\'"]', re.IGNORECASE).search(args[0]) and len(re.compile(r'[\'"]username[\'"]: ?[\'"](\S*)[\'"]', re.IGNORECASE).search(args[0]).groups()) == 1 and len(re.compile(r'[\'"]username[\'"]: ?[\'"](\S*)[\'"]', re.IGNORECASE).search(args[0]).groups()[0]) == 7 and re.compile(r'[\'"]username[\'"]: ?[\'"](\S*)[\'"]', re.IGNORECASE).search(args[0]).groups()[0][0] == '_':
# Here args is a list containing one item which is a string and the string contains 'username': '<user>' only once where <user> is 7 characters long and starts with _.
print process_the_username(re.compile(r'[\'"]username[\'"]: ?[\'"](\S*)[\'"]', re.IGNORECASE).search(args[0]).groups()[0])
else:
print "Missing or correct user name format. Nothing to do."
如果 Python 像许多其他语言一样支持 if 条件中的赋值,那么这个问题将很容易解决。但正如我们所知,Python 不支持这一点。
因此,我正在寻求有关如何以 Pythonic 方式编写 if 条件的建议,其中消除了正则表达式的重复。高度赞赏所有使代码更简单和更易于维护的建议。
以下是一些按预期处理用户名的示例执行。
>>> args = ["'location': 'Frankfurt', 'Phone': '+49 123 456789', 'UserName': '_beka01'"]
>>> print_processed_username(args)
ebeka01
>>>
>>> args = ["'UserName': '_beka01', 'location': 'Frankfurt', 'Phone': '+49 123 456789'"]
>>> print_processed_username(args)
ebeka01
>>>
>>> args = ["'UserName': '_beka01'"]
>>> print_processed_username(args)
ebeka01
>>>
>>> args = ["'USERNAME': '_beka01'"]
>>> print_processed_username(args)
ebeka01
>>>
>>> args = ['"location":"Frankfurt", "Phone":"+49 123 456789", "UserName":"_beka01"']
>>> print_processed_username(args)
ebeka01
>>>
>>> args = ['"location":"Frankfurt","Phone":"+49 123 456789","UserName":"_beka01"']
>>> print_processed_username(args)
ebeka01
>>>
以下是一些未按预期处理用户名的示例执行。
>>> args = ["'location': 'Frankfurt', 'Phone': '+49 123 456789', 'UserName': 'abeka01'"]
>>> print_processed_username(args)
Missing or correct user name format. Nothing to do.
>>>
>>> args = ["'location': 'Frankfurt', 'Phone': '+49 123 456789'"]
>>> print_processed_username(args)
Missing or correct user name format. Nothing to do.
>>>
>>> args = ["'UserName': '_beka0132'"]
>>> print_processed_username(args)
Missing or correct user name format. Nothing to do.
>>>
解决方案
第 1 步:编译一次正则表达式并将其保存在变量中。它没有变化,因此在调用该方法之前提前执行此操作。
username_regex = re.compile(r'[\'"]username[\'"]: ?[\'"](\S*)[\'"]', re.IGNORECASE)
def print_processed_username(args):
if len(args) == 1 and type(args[0]) is str and username_regex.search(args[0]) and len(username_regex.search(args[0]).groups()) == 1 and len(username_regex.search(args[0]).groups()[0]) == 7 and username_regex.search(args[0]).groups()[0][0] == '_':
print process_the_username(username_regex.search(args[0]).groups()[0])
else:
print "Missing or correct user name format. Nothing to do."
第 2 步:消除对 的重复调用search()
。
username_regex = re.compile(r'[\'"]username[\'"]: ?[\'"](\S*)[\'"]', re.IGNORECASE)
def print_processed_username(args):
if len(args) != 1 or type(args[0]) is not str:
print "Missing or correct user name format. Nothing to do."
return
result = username_regex.search(args[0])
if result and len(result.groups()) == 1 and len(result.groups()[0]) == 7 and result.groups()[0][0] == '_':
print process_the_username(result.groups()[0])
else:
print "Missing or correct user name format. Nothing to do."
第 3 步:将用户名保存在变量中。
username_regex = re.compile(r'[\'"]username[\'"]: ?[\'"](\S*)[\'"]', re.IGNORECASE)
def print_processed_username(args):
if len(args) != 1 or type(args[0]) is not str:
print "Missing or correct user name format. Nothing to do."
return
result = username_regex.search(args[0])
if not result or len(result.groups()) != 1:
print "Missing or correct user name format. Nothing to do."
return
username = result.groups()[0]
if len(username) == 7 and username[0] == '_':
print process_the_username(username)
else:
print "Missing or correct user name format. Nothing to do."
第 4 步:从处理结果的代码中提取字符串解析。编写一个纯粹解析字符串并将结果留给调用者的解析器。
username_regex = re.compile(r'[\'"]username[\'"]: ?[\'"](\S*)[\'"]', re.IGNORECASE)
def parse_username(args):
if len(args) != 1 or type(args[0]) is not str: return None
result = username_regex.search(args[0])
if not result or len(result.groups()) != 1: return None
username = result.groups()[0]
if len(username) != 7 or username[0] != '_': return None
return username
def print_processed_username(args):
username = parse_username(args)
if username:
print process_the_username(username)
else:
print "Missing or correct user name format. Nothing to do."
推荐阅读
- django - nginx重新启动后无法访问站点-AWS EC2
- scala - 需要帮助来修复 org.apache.hadoop.ipc.RemoteException - AWS EMR Spark Scala 应用程序
- selenium-webdriver - laravel 黄昏 facebook 网络驱动程序和 cookie
- html - 使用 GMail 发送 HTML 电子邮件时遇到问题
- python - 无法使用报纸3k 下载文章
- c# - 按依赖属性值对用户控件进行排序
- docker - 如何在dockerfile中隐藏用户并传入curl命令
- vba - vba 范围内的动态开始和最后一行
- php - 使用数组过滤器重写
- django - 解释内置 User 模型和 UserCreationForm