{"id":896,"date":"2022-01-14T04:39:00","date_gmt":"2022-01-14T04:39:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=896"},"modified":"2022-01-14T04:39:00","modified_gmt":"2022-01-14T04:39:00","slug":"896","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2022\/01\/14\/896\/","title":{"rendered":"Python \u547d\u4ee4\u884c(CLI)\u811a\u672c\u4f7f\u7528\u5f69\u8272\u7740\u8272\u7c7b Python Class for Coloring Python Command Line (CLI) Scripts Output"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2022\/01\/4063028989.png\" alt=\"123.png\" title=\"123.png\"><\/p>\n<h2>Python \u547d\u4ee4\u884c(CLI)\u811a\u672c\u4f7f\u7528\u5f69\u8272\u7740\u8272\u7c7b <code>colors.py<\/code> <a href=\"https:\/\/github.com\/hongwenjun\/srgb\/blob\/master\/python\/Colors\/colors.py\">\u6e90\u7801<\/a><\/h2>\n<pre><code>''' Python Class for Coloring Python Command Line (CLI) Scripts Output '''\nclass Colors:\n    colorize = ''\n    text = ''\n    def __init__(self, text='', color = 0, bg_color = 0):\n        self.text = text\n        if bg_color and color :\n            self.colorize = f'\u000033[{bg_color}m\u000033[{color}m{text}\u000033[0m'\n        elif color:\n            self.colorize = f'\u000033[{color}m{text}\u000033[0m'\n        elif bg_color:\n            self.colorize = f'\u000033[{bg_color}m{text}\u000033[0m'\n\nclass foreground_colors:\n    black = 30; dark_gray = '1;30'; blue = 34; light_blue = '1;34'\n    green = 32; light_green = '1;32'; cyan = 36; light_cyan = '1;36'\n    red = 31; light_red = '1;31'; purple = 35; light_purple = '1;35'\n    brown = 33; yellow = '1;33'; light_gray = 37; white = '1;37'\n\n    dir = ['black', 'blue', 'brown', 'cyan', 'dark_gray', 'green', 'light_blue', 'light_cyan', 'light_gray',\n            'light_green', 'light_purple', 'light_red', 'purple', 'red', 'white', 'yellow']\n\nclass background_colors:\n    black = 40;    red = 41; green = 42; yellow = 43\n    blue = 44; magenta = 45; cyan = 46; light_gray = 47\n    SUCCESS = 42; FAILURE = 41; WARNING = 43; NOTE = 44\n\n    dir = ['FAILURE', 'NOTE', 'SUCCESS', 'WARNING', 'black', 'blue', 'cyan', 'green', 'light_gray', 'magenta', 'red', 'yellow']\n\ncol = foreground_colors    # \u6587\u5b57\u989c\u8272\u8868         Text color table\nbg = background_colors     # \u80cc\u666f\u8272\u989c\u8272\u8868       background color table\ncolor = Colors             # \u521d\u59cb\u5316\u6587\u672c\u7740\u8272\u7c7b   Initialize the text coloring class\n\n##  Python \u547d\u4ee4\u884c(CLI)\u811a\u672c\u4f7f\u7528\u5f69\u8272\u7740\u8272\u7c7b Python Class for Coloring Python Command Line (CLI) Scripts Output\ndef test_colorize_class():\n    for i in range(len(col.dir)):\n        text = color('Test Foreground colors: ' + col.dir[i], vars(col)[col.dir[i]] )\n        print(text.colorize)\n\n    for i in range(len(bg.dir)):\n        text = color('Test Background colors: ' + bg.dir[i],  bg_color = vars(bg)[bg.dir[i]] )\n        print(text.colorize)\n\n    for i in range(len(col.dir)):\n        for j in range(len(bg.dir)):\n            text = color('Test Colors', vars(col)[col.dir[i]], bg_color = vars(bg)[bg.dir[j]] )\n            print(text.colorize, end = ' ')\n        print()\n\nif __name__ == '__main__':\n    test_colorize_class()\n    text = color(\"Python Class for Coloring Python Command Line (CLI) Scripts Output\", col.green, bg.blue)\n    print(text.text, text.colorize)<\/code><\/pre>\n<h2>\u6d4b\u8bd5\u4f7f\u7528\u793a\u4f8b\u4ee3\u7801 <code>test.py<\/code><\/h2>\n<pre><code>import colors\n\ncol = colors.foreground_colors    # \u6587\u5b57\u989c\u8272\u8868         Text color table\nbg = colors.background_colors     # \u80cc\u666f\u8272\u989c\u8272\u8868       background color table\ncolor = colors.Colors             # \u521d\u59cb\u5316\u6587\u672c\u7740\u8272\u7c7b   Initialize the text coloring class\n\nfor i in range(len(col.dir)):\n    text = color('Test Foreground colors: ' + col.dir[i], vars(col)[col.dir[i]] )\n    print(text.colorize)\n\nfor i in range(len(bg.dir)):\n    text = color('Test Background colors: ' + bg.dir[i],  bg_color = vars(bg)[bg.dir[i]] )\n    print(text.colorize)\n\nfor i in range(len(col.dir)):\n    for j in range(8):\n        text = color(col.dir[i] + bg.dir[j], vars(col)[col.dir[i]], bg_color = vars(bg)[bg.dir[j]] )\n        print(text.colorize, end = ' ')\n    print()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python \u547d\u4ee4\u884c(CLI)\u811a\u672c\u4f7f\u7528\u5f69\u8272\u7740\u8272\u7c7b colors.py \u6e90\u7801 &#8221;&#8217; Python C [&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-896","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\/896","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=896"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/896\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}