{"id":894,"date":"2022-01-12T14:54:00","date_gmt":"2022-01-12T14:54:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=894"},"modified":"2022-01-12T14:54:00","modified_gmt":"2022-01-12T14:54:00","slug":"894","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2022\/01\/12\/894\/","title":{"rendered":"python3.10 \u65b0\u589e\u7684 match case \u8bed\u53e5\uff0c\u65b9\u4fbf\u8f93\u51fa\u5f69\u8272\u6587\u672c\u4fe1\u606f"},"content":{"rendered":"<h2>python3.10 \u65b0\u589e <a href=\"https:\/\/lyvba.com\/python_doc\/tutorial\/controlflow.html#match-statements\">match \u8bed\u53e5<\/a><\/h2>\n<p>match \u8bed\u53e5\u63a5\u53d7\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5e76\u5c06\u5b83\u7684\u503c\u4e0e\u4ee5\u4e00\u4e2a\u6216\u591a\u4e2a case \u8bed\u53e5\u5757\u5f62\u5f0f\u7ed9\u51fa\u7684\u4e00\u7cfb\u5217\u6a21\u5f0f\u8fdb\u884c\u6bd4\u8f83\u3002 \u8fd9\u5728\u8868\u9762\u4e0a\u5f88\u7c7b\u4f3c C, Java \u6216 JavaScript (\u4ee5\u53ca\u8bb8\u591a\u5176\u4ed6\u8bed\u8a00) \u4e2d\u7684 switch \u8bed\u53e5\uff0c\u4f46\u5b83\u8fd8\u80fd\u591f\u4ece\u503c\u4e2d\u63d0\u53d6\u5b50\u90e8\u5206 (\u5e8f\u5217\u5143\u7d20\u6216\u5bf9\u8c61\u5c5e\u6027) \u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002<\/p>\n<p>\u6700\u7b80\u5355\u7684\u5f62\u5f0f\u662f\u5c06\u4e00\u4e2a\u76ee\u6807\u503c\u4e0e\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u9762\u503c\u8fdb\u884c\u6bd4\u8f83:<\/p>\n<pre><code>def http_error(status):\n    match status:\n        case 400:\n            return \"Bad request\"\n        case 404:\n            return \"Not found\"\n        case 418:\n            return \"I'm a teapot\"\n        case _:\n            return \"Something's wrong with the internet\"<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\u6700\u540e\u4e00\u4e2a\u4ee3\u7801\u5757: <code>\u53d8\u91cf\u540d<\/code> _ \u88ab\u4f5c\u4e3a \u901a\u914d\u7b26 \u5e76\u5fc5\u5b9a\u4f1a\u5339\u914d\u6210\u529f\u3002 \u5982\u679c\u6ca1\u6709\u4efb\u4f55 case \u8bed\u53e5\u5339\u914d\u6210\u529f\uff0c\u5219\u4efb\u4f55\u5206\u652f\u90fd\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 | \uff08\u201c or \u201d\uff09\u5728\u4e00\u4e2a\u6a21\u5f0f\u4e2d\u7ec4\u5408\u51e0\u4e2a\u5b57\u9762\u503c:<\/p>\n<pre><code>case 401 | 403 | 404:\n    return \"Not allowed\"<\/code><\/pre>\n<h3>\u73b0\u5728\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5199\u8f93\u51fa\u5f69\u8272\u6587\u672c\u65b0\u7684\u51fd\u6570\u4e86<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2022\/01\/558439762.png\" alt=\"match_case.png\" title=\"match_case.png\"><\/p>\n<pre><code># python3.10 \u65b0\u589e\u7684 match case \u8bed\u53e5\ndef colorize(text, status='SkyBlue'):\n    out = ''\n    match status:\n        case 'SUCCESS': out = '\u000033[42m'\n        case 'FAILURE': out = '\u000033[41m'\n        case 'WARNING': out = '\u000033[43m'\n        case 'NOTE':  out = '\u000033[44m'\n        case 'Green': out = '\u000033[32m'\n        case 'Red':   out = '\u000033[31m'\n        case 'Yellow':  out = '\u000033[0;33m'\n        case 'SkyBlue': out = '\u000033[0;36m'\n        case _ : out = '\u000033[42m' + status +'\u000033[0m'\n    return  out + text +'\u000033[0m'\n\nc = ['SUCCESS', 'FAILURE', 'WARNING', 'NOTE', 'Green', 'Red', 'Yellow', 'SkyBlue']\nfor i in range(len(c)):\n    print(colorize(\"Test Color \u6d4b\u8bd5\u989c\u8272: \", c[i]), i)\n\nsc = colorize(\"Your command was successfully executed...\n\", \"SUCCESS\")\nfl = colorize(\"Your command was FAILURE executed...\n\", \"FAILURE\")\nwr = colorize(\"Your command was WARNING executed...\n\", \"WARNING\")\nnt = colorize(\"Your command was NOTE    info...\n\", \"NOTE\")\nprint(sc + fl + wr + nt)\n\ndf = colorize(\"\u9ed8\u8ba4\u6587\u5b57\u5929\u84dd\u8272 SkyBlue ...\n\")\ninf = colorize(\"\u5176\u4ed6\u5339\u914d\u6587\u5b57\u989c\u8272 ...\n\", \"\u663e\u793a\u4fe1\u606f: \")\nprint(df + inf)<\/code><\/pre>\n<h2><code>colorize<\/code> \u51fd\u6570\u4f7f\u7528 | \uff08\u201c or \u201d\uff09\u7ec4\u5408\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u5339\u914d\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u66f4\u52a0\u65b9\u4fbf<\/h2>\n<pre><code>def colorize(text, status='SkyBlue'):\n    out = ''\n    match status:\n        case 'SUCCESS' | 0 : out = '\u000033[42m'\n        case 'FAILURE' | 1 : out = '\u000033[41m'\n        case 'WARNING' | 2 : out = '\u000033[43m'\n        case 'NOTE'    | 3 : out = '\u000033[44m'\n        case 'Green'   | 4 : out = '\u000033[32m'\n        case 'Red'     | 5 : out = '\u000033[31m'\n        case 'Yellow'  | 6 : out = '\u000033[0;33m'\n        case 'SkyBlue' | 7 : out = '\u000033[0;36m'\n        case _ : out = '\u000033[42m' + status +'\u000033[0m'\n    return  out + text +'\u000033[0m'\n\nfor i in range(8):\n    print(colorize(\"Test Color \u6d4b\u8bd5\u989c\u8272: \", i), i)<\/code><\/pre>\n<h2>\u4f4e\u7248\u672cPython\u4e2d\u6ca1\u6709switch\u6216\u8005case\u8bed\u53e5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>if  elif  else<\/code> \u6765\u4ee3\u66ff <code>switch case<\/code> \u8bed\u53e5<\/h2>\n<pre><code>def colorize(text, col='SkyBlue'):\n    colors = ['SUCCESS', 'FAILURE', 'WARNING', 'NOTE', 'Green', 'Red', 'Yellow', 'SkyBlue']\n    if col == colors[0] or col == 0 : out = '\u000033[42m'\n    elif col == colors[1] or col == 1 : out = '\u000033[41m'\n    elif col == colors[2] or col == 2 : out = '\u000033[43m'\n    elif col == colors[3] or col == 3 : out = '\u000033[44m'\n    elif col == colors[4] or col == 4 : out = '\u000033[32m'\n    elif col == colors[5] or col == 5 : out = '\u000033[31m'\n    elif col == colors[6] or col == 6 : out = '\u000033[0;33m'\n    elif col == colors[7] or col == 7 : out = '\u000033[0;36m'\n    else : out = '\u000033[42m' + col +'\u000033[0m'\n    return  out + text +'\u000033[0m'\n\nfor i in range(8):\n    print(colorize(\"Test Color \u6d4b\u8bd5\u989c\u8272: \", i), i)\n\nsc = colorize(\"Your command was successfully executed...\n\", \"SUCCESS\")\nfl = colorize(\"Your command was FAILURE executed...\n\", \"FAILURE\")\nwr = colorize(\"Your command was WARNING executed...\n\", \"WARNING\")\nnt = colorize(\"Your command was NOTE    info...\n\", \"NOTE\")\nprint(sc + fl + wr + nt)\n\ndf = colorize(\"\u9ed8\u8ba4\u6587\u5b57\u5929\u84dd\u8272 SkyBlue ...\n\")\ninf = colorize(\"\u5176\u4ed6\u5339\u914d\u6587\u5b57\u989c\u8272 ...\n\", \"\u663e\u793a\u4fe1\u606f: \")\nprint(df + inf)\n\nc = ['SUCCESS', 'FAILURE', 'WARNING', 'NOTE', 'Green', 'Red', 'Yellow', 'SkyBlue']\nfor i in range(len(c)):\n    print(colorize(\"Test Color \u6d4b\u8bd5\u989c\u8272: \", c[i]), i, end = ' ')\n\nprint()\nprint(colorize(\"docker run --rm -it hongwenjun\/xmseed\", \"Usage: \"))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>python3.10 \u65b0\u589e match \u8bed\u53e5 match \u8bed\u53e5\u63a5\u53d7\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5e76\u5c06\u5b83\u7684\u503c\u4e0e\u4ee5\u4e00\u4e2a\u6216\u591a\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-894","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=894"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/894\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}