python tables_python之terminaltables

from terminaltables importAsciiTable, DoubleTable, SingleTablefrom colorclass importColor, Windows

TABLE_DATA=(

('Platform', 'Years', 'Notes'),

('Mk5', '2007-2009', 'The Golf Mk5 Variant was\nintroduced in 2007.'),

('MKVI', '2009-2013', 'Might actually be Mk5.'),

)deftable_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()deftable_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=Falsereturntable_instance.tabledeftable_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'}returntable_instance.tabledeftable_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 inenumerate(largest):

combined.append(row.ljust(10) + ' ' +smallest[i])return '\n'.join(combined)deftable_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 importwrap

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.')deftable_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_stringprint(table.table)if __name__ == '__main__':

table_example2()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值