python3.10 新增的 match case 语句,方便输出彩色文本信息

python3.10 新增 match 语句

match 语句接受一个表达式并将它的值与以一个或多个 case 语句块形式给出的一系列模式进行比较。 这在表面上很类似 C, Java 或 JavaScript (以及许多其他语言) 中的 switch 语句,但它还能够从值中提取子部分 (序列元素或对象属性) 并赋值给变量。

最简单的形式是将一个目标值与一个或多个字面值进行比较:

def http_error(status):
    match status:
        case 400:
            return "Bad request"
        case 404:
            return "Not found"
        case 418:
            return "I'm a teapot"
        case _:
            return "Something's wrong with the internet"

请注意最后一个代码块: “变量名” _ 被作为 通配符 并必定会匹配成功。 如果没有任何 case 语句匹配成功,则任何分支都不会被执行。

你可以使用 | (“ or ”)在一个模式中组合几个字面值:

case 401 | 403 | 404:
    return "Not allowed"

现在可以很方便的写输出彩色文本新的函数了

match_case.png

# python3.10 新增的 match case 语句
def colorize(text, status='SkyBlue'):
    out = ''
    match status:
        case 'SUCCESS': out = '\033[42m'
        case 'FAILURE': out = '\033[41m'
        case 'WARNING': out = '\033[43m'
        case 'NOTE':  out = '\033[44m'
        case 'Green': out = '\033[32m'
        case 'Red':   out = '\033[31m'
        case 'Yellow':  out = '\033[0;33m'
        case 'SkyBlue': out = '\033[0;36m'
        case _ : out = '\033[42m' + status +'\033[0m'
    return  out + text +'\033[0m'

c = ['SUCCESS', 'FAILURE', 'WARNING', 'NOTE', 'Green', 'Red', 'Yellow', 'SkyBlue']
for i in range(len(c)):
    print(colorize("Test Color 测试颜色: ", c[i]), i)

sc = colorize("Your command was successfully executed...\n", "SUCCESS")
fl = colorize("Your command was FAILURE executed...\n", "FAILURE")
wr = colorize("Your command was WARNING executed...\n", "WARNING")
nt = colorize("Your command was NOTE    info...\n", "NOTE")
print(sc + fl + wr + nt)

df = colorize("默认文字天蓝色 SkyBlue ...\n")
inf = colorize("其他匹配文字颜色 ...\n", "显示信息: ")
print(df + inf)

colorize 函数使用 | (“ or ”)组合字符串和数字匹配,使用的时候更加方便

def colorize(text, status='SkyBlue'):
    out = ''
    match status:
        case 'SUCCESS' | 0 : out = '\033[42m'
        case 'FAILURE' | 1 : out = '\033[41m'
        case 'WARNING' | 2 : out = '\033[43m'
        case 'NOTE'    | 3 : out = '\033[44m'
        case 'Green'   | 4 : out = '\033[32m'
        case 'Red'     | 5 : out = '\033[31m'
        case 'Yellow'  | 6 : out = '\033[0;33m'
        case 'SkyBlue' | 7 : out = '\033[0;36m'
        case _ : out = '\033[42m' + status +'\033[0m'
    return  out + text +'\033[0m'

for i in range(8):
    print(colorize("Test Color 测试颜色: ", i), i)

低版本Python中没有switch或者case语句,可以使用 if elif else 来代替 switch case 语句

def colorize(text, col='SkyBlue'):
    colors = ['SUCCESS', 'FAILURE', 'WARNING', 'NOTE', 'Green', 'Red', 'Yellow', 'SkyBlue']
    if col == colors[0] or col == 0 : out = '\033[42m'
    elif col == colors[1] or col == 1 : out = '\033[41m'
    elif col == colors[2] or col == 2 : out = '\033[43m'
    elif col == colors[3] or col == 3 : out = '\033[44m'
    elif col == colors[4] or col == 4 : out = '\033[32m'
    elif col == colors[5] or col == 5 : out = '\033[31m'
    elif col == colors[6] or col == 6 : out = '\033[0;33m'
    elif col == colors[7] or col == 7 : out = '\033[0;36m'
    else : out = '\033[42m' + col +'\033[0m'
    return  out + text +'\033[0m'

for i in range(8):
    print(colorize("Test Color 测试颜色: ", i), i)

sc = colorize("Your command was successfully executed...\n", "SUCCESS")
fl = colorize("Your command was FAILURE executed...\n", "FAILURE")
wr = colorize("Your command was WARNING executed...\n", "WARNING")
nt = colorize("Your command was NOTE    info...\n", "NOTE")
print(sc + fl + wr + nt)

df = colorize("默认文字天蓝色 SkyBlue ...\n")
inf = colorize("其他匹配文字颜色 ...\n", "显示信息: ")
print(df + inf)

c = ['SUCCESS', 'FAILURE', 'WARNING', 'NOTE', 'Green', 'Red', 'Yellow', 'SkyBlue']
for i in range(len(c)):
    print(colorize("Test Color 测试颜色: ", c[i]), i, end = ' ')

print()
print(colorize("docker run --rm -it hongwenjun/xmseed", "Usage: "))
© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论