python - 由于 Python 不支持 if 条件中的赋值,如何使复杂的 if 条件可维护?

标签 python

我有以下 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 不支持这一点。

因此,我寻求有关如何以 Python 方式编写 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."

关于python - 由于 Python 不支持 if 条件中的赋值,如何使复杂的 if 条件可维护?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50715872/

相关文章:

python - 多词拼写更正

Python(新手)从 API 调用中解析 XML

Python,将函数分配给变量,更改可选参数的值

python - 如何为图像背景制作动画?

python - 从 `code.interact()` 之类的 Python shell 使用 IPython

python - 线程中的线程

python - 错误 : Out Of Memory, tensorflow cnn

python - 从 0 开始自动编号的枚举

python - 用于 python 的 HTML 敏捷包

python - urllib.request 错误魔法错误