from terminaltables import AsciiTable, DoubleTable, SingleTable
from colorclass import Color, Windows TABLE_DATA = (
('Platform', 'Years', 'Notes'),
('Mk5', '2007-2009', 'The Golf Mk5 Variant was\nintroduced in 2007.'),
('MKVI', '2009-2013', 'Might actually be Mk5.'),
) def table_example1():
"""Main function."""
title = 'Jetta SportWagen' # AsciiTable.
table_instance = AsciiTable(TABLE_DATA, title)
table_instance.justify_columns[2] = 'right'
print(table_instance.table)
print() # SingleTable.
table_instance = SingleTable(TABLE_DATA, title)
table_instance.justify_columns[2] = 'right'
print(table_instance.table)
print() # DoubleTable.
table_instance = DoubleTable(TABLE_DATA, title)
table_instance.justify_columns[2] = 'right'
print(table_instance.table)
print() def table_server_timings():
"""Return table string to be printed."""
table_data = [
[Color('{autogreen}<10ms{/autogreen}'), '192.168.0.100, 192.168.0.101'],
[Color('{autoyellow}10ms <= 100ms{/autoyellow}'), '192.168.0.102, 192.168.0.103'],
[Color('{autored}>100ms{/autored}'), '192.168.0.105'],
]
table_instance = SingleTable(table_data)
table_instance.inner_heading_row_border = False
return table_instance.table def table_server_status():
"""Return table string to be printed."""
table_data = [
[Color('Low Space'), Color('{autocyan}Nominal Space{/autocyan}'), Color('Excessive Space')],
[Color('Low Load'), Color('Nominal Load'), Color('{autored}High Load{/autored}')],
[Color('{autocyan}Low Free RAM{/autocyan}'), Color('Nominal Free RAM'), Color('High Free RAM')],
]
table_instance = SingleTable(table_data, '192.168.0.105')
table_instance.inner_heading_row_border = False
table_instance.inner_row_border = True
table_instance.justify_columns = {0: 'center', 1: 'center', 2: 'center'}
return table_instance.table def table_abcd():
"""Return table string to be printed. Two tables on one line."""
table_instance = SingleTable([['A', 'B'], ['C', 'D']]) # Get first table lines.
table_instance.outer_border = False
table_inner_borders = table_instance.table.splitlines() # Get second table lines.
table_instance.outer_border = True
table_instance.inner_heading_row_border = False
table_instance.inner_column_border = False
table_outer_borders = table_instance.table.splitlines() # Combine.
smallest, largest = sorted([table_inner_borders, table_outer_borders], key=len)
smallest += [''] * (len(largest) - len(smallest)) # Make both same size.
combined = list()
for i, row in enumerate(largest):
combined.append(row.ljust(10) + ' ' + smallest[i])
return '\n'.join(combined) def table_example2():
"""Main function."""
Windows.enable(auto_colors=True, reset_atexit=True) # Does nothing if not on Windows. # Server timings.
print(table_server_timings())
print() # Server status.
print(table_server_status())
print() # Two A B C D tables.
print(table_abcd())
print() # Instructions.
table_instance = SingleTable([['Obey Obey Obey Obey']], 'Instructions')
print(table_instance.table)
print() from textwrap import wrap LONG_STRING = ('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore '
'et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut '
'aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum '
'dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui '
'officia deserunt mollit anim id est laborum.') def table_example3():
"""Main function."""
table_data = [
['Long String', ''], # One row. Two columns. Long string will replace this empty string.
]
table = SingleTable(table_data) # Calculate newlines.
max_width = table.column_max_width(1)
wrapped_string = '\n'.join(wrap(LONG_STRING, max_width))
table.table_data[0][1] = wrapped_string print(table.table) if __name__ == '__main__':
table_example2()

python之terminaltables的更多相关文章

  1. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  2. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  3. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  4. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  5. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  6. 使用Python保存屏幕截图(不使用PIL)

    起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...

  7. Python编码记录

    字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...

  8. Apache执行Python脚本

    由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...

  9. python开发编译器

    引言 最近刚刚用python写完了一个解析protobuf文件的简单编译器,深感ply实现词法分析和语法分析的简洁方便.乘着余热未过,头脑清醒,记下一点总结和心得,方便各位pythoner参考使用. ...

随机推荐

  1. 服务器安装python3环境

    服务器安装python3环境 先安装相关包 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel re ...

  2. “SkyReach”对“待就业六人组”和“葫芦娃”的Beta产品测试报告

    对"待就业六人组"的Beta产品测试报告 安装成功截图 第一次上手体验感觉如何?能否正常运行? 第一次上手体验,界面简洁明了,简约的风格比较迎合个人的口味:能够正常运行: 该产品具 ...

  3. Spring @Autowired 注解 学习资料

    Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...

  4. python3.7 win10配置opencv和扩展库

  5. Good Article Good sentence HDU - 4416 (后缀数组)

    Good Article Good sentence \[ Time Limit: 3000 ms\quad Memory Limit: 32768 kB \] 题意 给出一个 \(S\) 串,在给出 ...

  6. MongoDB索引存储BTree与LSM树(转载)

    1.为什么 MongoDB 使用B-树,而不是B+树 MongoDB 是一种 nosql,也存储在磁盘上,被设计用在数据模型简单,性能要求高的场合.性能要求高,我们看B-树与B+树的区别: B+树内节 ...

  7. 文件夹上传插件webupload插件

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...

  8. 交叉引用:Microsoft.NET标准异常 和错误代码对照表

    简介 此表旨在帮助将Windows运行时应用程序错误代码交叉引用到Microsoft.NET标准异常,这些异常可以作为应用程序异常处理技术的一部分. 对照表 .NET Exception (Names ...

  9. 云服务器搭建JDK+Tomcat+MySQL环境

    一.首先租赁一台云服务器(阿里云服务器或者腾讯云服务器) 其实可以在windows电脑上使用VMware workstation来安装虚拟机进行操作,毕竟云服务器低配也是很贵的.不过可以使用学生价去租 ...

  10. 部署oVirt4.2+Gluster超融合架构

    首先下载最新ovirt-node iso镜像 准备3台机器,配置为2核,16G内存,两块硬盘,1块100G装系统,1块300G做存储 node1.com(192.168.105.221) node2. ...