如何在复杂的if条件中维护,因为在Python中不支持if条件中的赋值?

时间:2018-06-06 08:40:20

标签: 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-conditions中的赋值,那么这个问题很容易解决。但正如我们所知,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 个答案:

答案 0 :(得分:1)

步骤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."