python 美化输出信息的实例

yipeiwu_com5年前Python基础

如下所示:

# -*- coding: utf-8 -*-
# @Author: xiaodong
# @Date:  just hide
# @Last Modified by:  xiaodong
# @Last Modified time: just hide
# try:
#   from colorama import Fore, Style
# except ImportError:
#   class Temp:
#     def __getattr__(self, x):
#       return ''
#   Fore = Style = Temp()


STYLE = {
    'fore': {
        'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
        'blue': 34, 'purple': 35, 'cyan': 36, 'white': 37,
    },
    'back': {
        'black': 40, 'red': 41, 'green': 42, 'yellow': 43,
        'blue': 44, 'purple': 45, 'cyan': 46, 'white': 47,
    },
    'mode': {
        'bold': 1, 'underline': 4, 'blink': 5, 'invert': 7,
    },
    'default': {
        'end': 0,
    }
}


def use_style(string, mode='', fore='', back=''):
  mode = '%s' % STYLE['mode'][mode] if mode in STYLE['mode'] else ''
  fore = '%s' % STYLE['fore'][fore] if fore in STYLE['fore'] else ''
  back = '%s' % STYLE['back'][back] if back in STYLE['back'] else ''
  style = ';'.join([s for s in [mode, fore, back] if s])
  style = '\033[%sm' % style if style else ''
  end = '\033[%sm' % STYLE['default']['end'] if style else ''
  return '%s%s%s' % (style, string, end)


def gentle_show(seq, *, column=4, fontdict=None):

  if fontdict is None:
    line_color = 'red'
    font_color = 'blue'
  elif isinstance(fontdict, dict):
    line_color = fontdict.get('line_color', 'red')
    font_color = fontdict.get('font_color', 'green')

  seq = list(map(str, seq))
  max_len = len(max(seq, key=len))

  for index, ele in enumerate(seq):
    if index % column == 0:
      print(use_style('-' * max_len * column + '-' * (column - 1), fore=line_color))
      print(use_style(ele.center(max_len, ' '), mode='bold', fore=font_color), end='|')
    else:
      if (index - column + 1) % column == 0:
        print(use_style(ele.center(max_len, ' '), mode='bold', fore=font_color))
      else:
        print(use_style(ele.center(max_len, ' '), mode='bold', fore=font_color), end='|')
  print('\n')


if __name__ == "__main__":
  gentle_show(dir([]), column=6, fontdict={'line_color': 'red', 'font_color': 'green'})
  gentle_show(range(10))

python 美化输出信息

python 美化输出信息

python 美化输出信息

以上这篇python 美化输出信息的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

在Django框架中编写Context处理器的方法

写Context处理器的一些建议 编写处理器的一些建议:     使每个context处理器完成尽可能小的功能。 使用多个处理器是很容易的,所以你可以根据逻...

django配置连接数据库及原生sql语句的使用方法

前言 本文主要给大家介绍了关于django配置连接数据库及原生sql语句的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 Django配置连接数据库: 在操作...

Python 中的 global 标识对变量作用域的影响

global 标识用于在函数内部,修改全局变量的值。 我们可以通过以下规则,来判定一个变量到底是在全局作用域还是局部作用域: 变量定义在全局作用域,那就是全局变量。 变量在函数...

Python基于多线程操作数据库相关问题分析

本文实例分析了Python多线程操作数据库相关问题。分享给大家供大家参考,具体如下: python多线程并发操作数据库,会存在链接数据库超时、数据库连接丢失、数据库操作超时等问题。 解决...

python获取外网IP并发邮件的实现方法

第一步:通过ip138来爬取外网ip 第二步:通过python的smtplib模块和email来发送邮件,具体用法去网上搜索, 下面是代码示例: #!/usr/bin/env pyt...