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. python正则表达式(8)--分组、后向引用、前(后)向断言

    无名.有名分组 (1)正则表达式—无名分组 从正则表 达式的左边开始看,看到的第一个左括号“(”表示表示第一个分组,第二个表示第二个分组, 依次类推. 需要注意的是,有一个隐含的全局分组(就是索引号为 ...

  2. python小案例-计算输入两个数的最大公约数与最小公倍数

    # 计算最大公约数 def gcd(x,y): """ 计算最大公约数 :param x:一个正整数 :param y:一个正整数 :return:x,y的最大公约数 & ...

  3. Consul 的 Docker 镜像使用

    1.镜像官方网址:https://hub.docker.com/_/consul 2.pull 镜像: docker pull consul:1.6.0 3.创建容器(默认http管理端口:8500) ...

  4. G6 知识点

    Viser 一个基于 G2 实现的,为数据可视化工程师量身定制的工具. Viser-Graph 一个基于 G6 实现的,为呈现关系型数据的定制化工具. Mode 是 G6 提供的图上事件的管理机制. ...

  5. 我的洛谷签名——Pale Blue Dot

    We succeeded in taking that picture [from deep space], and, if you look at it, you see a dot. That's ...

  6. Worldview in Context

    Worldview in Context Figures 1 and 2 provide a basis for a deeper understanding of worldview. The se ...

  7. 第3章 Spring AOP

    3.1 Spring AOP简介 3.11什么是AOP? AOP的全称是Aspect-Oriented Programming,即面向切面编程(也称面向方面编程).它是面向对象编程(OOP)的一种补充 ...

  8. C程序运行原理

    计算机不能直接识别和执行高级语言写的命令,必须用编译程序(也称编译器)把C源程序翻译成二进制形式的目标程序,然后再将该目标程序与系统的函数库以及其他目标程序连接起来,形成可执行的目标程序 C语言的编译 ...

  9. 洛谷 P1825 【[USACO11OPEN]玉米田迷宫Corn Maze】

    P1825 传送门 简单的题意 就是一个有传送门的迷宫问题(我一开始以为是只有1个传送门,然后我就凉了). 大体思路 先把传送门先存起来,然后跑一下\(BFS\). 然后,就做完了. 代码鸭 #inc ...

  10. PHP Record the number of login users

    Function to record how many times the user logs in Connect to the database first: you can create a n ...