为面向用户的打印语句提供方便的格式、颜色和实用程序。
display-session的Python项目详细描述
display session是麻省理工学院授权的python包,它为python的内置print语句提供了简单的ansi格式和实用程序。
这个项目源于想要更好的外观、更多的信息和更吸引人的命令行界面。
有三个功能面向用户:
- 页眉-打印提供的消息,所有其他空间作为提供的justify_char。作为一种简单的方式来沟通日志中的不同部分。
- alert-将数字参数(-1,0,1)映射到bad,neutral,good的简单打印语句。作为沟通信息情感的简单方法。
- 报表-最复杂的打印报表。使用署名处理所有邮件,如果提供,则在打印时运行所有提供的函数。用作命令行的一种平视显示。见下面的例子。
简单比较和示例:
>>>print('This is how the builtin print function works') This is how the builtin print function works
显示会话最简单、最引人注目的用例:
>>>from display_session import DisplaySession >>>display = DisplaySession('This is a byline') >>>display.report('The byline proceeds any text input here') # hard to show with markdown, but byline is also separately ANSI colored. This is a byline : The byline proceeds any text input here
更复杂的示例:
>>>import datetime as dt >>>import psutil >>>from display_session import DisplaySession >>>user = 'John' >>>display = DisplaySession(byline='P R O G R A M - {}'.format(user), byline_action=[dt.datetime.now, psutil.cpu_percent] ) >>>display.report('User successfully logged in') P R O G R A M - John // 2018-09-19 21:55:29.115387 // 9.1: User successfully logged in. >>>display.report('User successfully logged out') P R O G R A M - John // 2018-09-19 21:56:14.560489 // 7.8: User successfully logged out.
其他功能:
>>># lets signify to our users that we are entering a new section >>>display.header('BEGINNING LEVEL 2', align='right') ______________________________________________________________________ BEGINNING LEVEL 2 >>># lets communicate by leveraging ANSI colors - our second arg maps to them in the method. >>>display.alert('This message is good, so it should have a green color', 1) This message is good, so it should have a green color >>> >>>display.alert('This message is bad, so it should have a red color', -1) This message is bad, so it should have a red color