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"
现在可以很方便的写输出彩色文本新的函数了
# python3.10 新增的 match case 语句
def colorize(text, status='SkyBlue'):
out = ''
match status:
case 'SUCCESS': out = ' 33[42m'
case 'FAILURE': out = ' 33[41m'
case 'WARNING': out = ' 33[43m'
case 'NOTE': out = ' 33[44m'
case 'Green': out = ' 33[32m'
case 'Red': out = ' 33[31m'
case 'Yellow': out = ' 33[0;33m'
case 'SkyBlue': out = ' 33[0;36m'
case _ : out = ' 33[42m' + status +' 33[0m'
return out + text +' 33[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...
", "SUCCESS")
fl = colorize("Your command was FAILURE executed...
", "FAILURE")
wr = colorize("Your command was WARNING executed...
", "WARNING")
nt = colorize("Your command was NOTE info...
", "NOTE")
print(sc + fl + wr + nt)
df = colorize("默认文字天蓝色 SkyBlue ...
")
inf = colorize("其他匹配文字颜色 ...
", "显示信息: ")
print(df + inf)
colorize
函数使用 | (“ or ”)组合字符串和数字匹配,使用的时候更加方便
def colorize(text, status='SkyBlue'):
out = ''
match status:
case 'SUCCESS' | 0 : out = ' 33[42m'
case 'FAILURE' | 1 : out = ' 33[41m'
case 'WARNING' | 2 : out = ' 33[43m'
case 'NOTE' | 3 : out = ' 33[44m'
case 'Green' | 4 : out = ' 33[32m'
case 'Red' | 5 : out = ' 33[31m'
case 'Yellow' | 6 : out = ' 33[0;33m'
case 'SkyBlue' | 7 : out = ' 33[0;36m'
case _ : out = ' 33[42m' + status +' 33[0m'
return out + text +' 33[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 = ' 33[42m'
elif col == colors[1] or col == 1 : out = ' 33[41m'
elif col == colors[2] or col == 2 : out = ' 33[43m'
elif col == colors[3] or col == 3 : out = ' 33[44m'
elif col == colors[4] or col == 4 : out = ' 33[32m'
elif col == colors[5] or col == 5 : out = ' 33[31m'
elif col == colors[6] or col == 6 : out = ' 33[0;33m'
elif col == colors[7] or col == 7 : out = ' 33[0;36m'
else : out = ' 33[42m' + col +' 33[0m'
return out + text +' 33[0m'
for i in range(8):
print(colorize("Test Color 测试颜色: ", i), i)
sc = colorize("Your command was successfully executed...
", "SUCCESS")
fl = colorize("Your command was FAILURE executed...
", "FAILURE")
wr = colorize("Your command was WARNING executed...
", "WARNING")
nt = colorize("Your command was NOTE info...
", "NOTE")
print(sc + fl + wr + nt)
df = colorize("默认文字天蓝色 SkyBlue ...
")
inf = colorize("其他匹配文字颜色 ...
", "显示信息: ")
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: "))
0 条评论