ttable
1 ttable
from xlib.util.ascii_art import ttable
1.1 普通表格
code
x = Ttable("Test title", 4)
x.header("column1", "column2", "column3", "column4")
x.append("t1", "t2", "very long entry", "test")
x.append(("r", "r3"), ("l", "l2"), "also long entry", "test")
print(x)
output
+-----------------------------------------------+
| Test title |
+---------+---------+-----------------+---------+
| column1 | column2 | column3 | column4 |
+---------+---------+-----------------+---------+
| t1 | t2 | very long entry | test |
| r | l | also long entry | test |
+---------+---------+-----------------+---------+
1.2 Very long ttable title
code
x = Ttable("Very long ttable title", 2)
x.defattr("l", "r")
x.append("key", "value")
x.append("other key", 123)
y = []
y.append(("first", "1"))
y.append(("second", "4"))
x.append(*y)
print(x)
output
+------------------------+
| Very long ttable title |
+-------------+----------+
| key | value |
| other key | 123 |
| first | second |
+-------------+----------+
1.3 ttable with complicated header
code
x = Ttable("ttable with complicated header", 14, "r")
x.header(("", "", 3), ("I/O stats last min", "", 8), ("", "", 3))
x.header(("info", "", 3), ("---", "", 8), ("space", "", 3))
x.header(("", "", 3), ("transfer", "", 2),
("max time", "", 3), ("# of ops", "", 3), ("", "", 3))
x.header(("---", "", 14))
x.header("IP", "last error", "status", "read", "write", "read", "write",
"fsync", "read", "write", "fsync", "used", "total", "used %")
x.append("192.168.1.1", "no errors", "ok", "19 MiB/s", "27 MiB/s", "263625 us",
"43116 us", "262545 us", 3837, 3295, 401, "1.0 TiB", "1.3 TiB", "76.41%")
x.append("192.168.1.2", "no errors", "ok", "25 MiB/s", "29 MiB/s", "340303 us",
"89168 us", "223610 us", 2487, 2593, 366, "1.0 TiB", "1.3 TiB", "75.93%")
x.append("192.168.1.3", ("2012-10-12 07:27", "2"), ("damaged", "1"), "-",
"-", "-", "-", "-", "-", "-", "-", "1.2 TiB", "1.3 TiB", "87.18%")
x.append("192.168.1.4", "no errors", ("marked for removal", "4"), "-",
"-", "-", "-", "-", "-", "-", "-", "501 GiB", "1.3 TiB", "36.46%")
x.append("192.168.1.5", "no errors", "ok", "17 MiB/s", "11 MiB/s", "417292 us",
"76333 us", "1171903 us", "2299", "2730", "149", "1.0 TiB", "1.3 TiB", "76.61%")
print(x)
output
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ttable with complicated header |
+-----------------------------------------------------+--------------------------------------------------------------------------------+----------------------------+
| | I/O stats last min | |
| info +---------------------+-----------------------------------+----------------------+ space |
| | transfer | max time | # of ops | |
+-------------+------------------+--------------------+----------+----------+-----------+----------+------------+------+-------+-------+---------+---------+--------+
| IP | last error | status | read | write | read | write | fsync | read | write | fsync | used | total | used % |
+-------------+------------------+--------------------+----------+----------+-----------+----------+------------+------+-------+-------+---------+---------+--------+
| 192.168.1.1 | no errors | ok | 19 MiB/s | 27 MiB/s | 263625 us | 43116 us | 262545 us | 3837 | 3295 | 401 | 1.0 TiB | 1.3 TiB | 76.41% |
| 192.168.1.2 | no errors | ok | 25 MiB/s | 29 MiB/s | 340303 us | 89168 us | 223610 us | 2487 | 2593 | 366 | 1.0 TiB | 1.3 TiB | 75.93% |
| 192.168.1.3 | 2012-10-12 07:27 | damaged | - | - | - | - | - | - | - | - | 1.2 TiB | 1.3 TiB | 87.18% |
| 192.168.1.4 | no errors | marked for removal | - | - | - | - | - | - | - | - | 501 GiB | 1.3 TiB | 36.46% |
| 192.168.1.5 | no errors | ok | 17 MiB/s | 11 MiB/s | 417292 us | 76333 us | 1171903 us | 2299 | 2730 | 149 | 1.0 TiB | 1.3 TiB | 76.61% |
+-------------+------------------+--------------------+----------+----------+-----------+----------+------------+------+-------+-------+---------+---------+--------+
1.4 Colors
code
x = Ttable("Colors", 1, "r")
x.append(("white", "0"))
x.append(("red", "1"))
x.append(("orange", "2"))
x.append(("yellow", "3"))
x.append(("green", "4"))
x.append(("cyan", "5"))
x.append(("blue", "6"))
x.append(("magenta", "7"))
x.append(("gray", "8"))
print(x)
output
+---------+
| Colors |
+---------+
| white |
| red |
| orange |
| yellow |
| green |
| cyan |
| blue |
| magenta |
| gray |
+---------+
1.5 Adjustments
code
x = Ttable("Adjustments", 1)
x.append(("left", "l"))
x.append(("right", "r"))
x.append(("center", "c"))
print(x)
output
+-------------+
| Adjustments |
+-------------+
| left |
| right |
| center |
+-------------+
1.6 Special entries
code
x = Ttable("Special entries", 3)
x.defattr("l", "r", "r")
x.header("entry", "effect", "extra column")
x.append("-- ", "--", "")
x.append("--- ", "---", "")
x.append("('--','',2)", ('--', '', 2))
x.append("('','',2)", ('', '', 2))
x.append("('---','',2)", ('---', '', 2))
x.append("('red','1')", ('red', '1'), '')
x.append("('orange','2')", ('orange', '2'), '')
x.append("('yellow','3')", ('yellow', '3'), '')
x.append("('green','4')", ('green', '4'), '')
x.append("('cyan','5')", ('cyan', '5'), '')
x.append("('blue','6')", ('blue', '6'), '')
x.append("('magenta','7')", ('magenta', '7'), '')
x.append("('gray','8')", ('gray', '8'), '')
x.append(('---', '', 3))
x.append("('left','l',2)", ('left', 'l', 2))
x.append("('right','r',2)", ('right', 'r', 2))
x.append("('center','c',2)", ('center', 'c', 2))
print(x)
output
+-------------------------------------------+
| Special entries |
+------------------+---------+--------------+
| entry | effect | extra column |
+------------------+---------+--------------+
| -- | ------- | |
| --- +---------+ |
| ('--','',2) | ---------------------- |
| ('','',2) | |
| ('---','',2) +---------+--------------+
| ('red','1') | red | |
| ('orange','2') | orange | |
| ('yellow','3') | yellow | |
| ('green','4') | green | |
| ('cyan','5') | cyan | |
| ('blue','6') | blue | |
| ('magenta','7') | magenta | |
| ('gray','8') | gray | |
+------------------+---------+--------------+
| ('left','l',2) | left |
| ('right','r',2) | right |
| ('center','c',2) | center |
+------------------+------------------------+
1.7 host info
code
# loop
x = Ttable("host info", 2)
x.defattr("l", "l")
x.header("host_group", "host")
elements = {
"host_group1": ["127.0.0.1", "127.0.0.2", "127.0.0.3"],
"host_group2": ["127.0.0.1", "127.0.0.2"]
}
lastpos = len(elements) - 1
for i, host_group in enumerate(elements):
ip_list = elements[host_group]
for j, ip in enumerate(ip_list):
# 填充数据
if j == 0:
x.append(host_group, ip)
else:
x.append("", ip)
# 分割线
if j < (len(ip_list) - 1):
x.append("", "---")
# 分割线
if i < lastpos:
x.append("---", "---")
print(x)
output
+-------------------------+
| host info |
+-------------+-----------+
| host_group | host |
+-------------+-----------+
| host_group1 | 127.0.0.1 |
| +-----------+
| | 127.0.0.2 |
| +-----------+
| | 127.0.0.3 |
+-------------+-----------+
| host_group2 | 127.0.0.1 |
| +-----------+
| | 127.0.0.2 |
+-------------+-----------+
1.8 换行
code
# 换行
x = Ttable("Test title", 4)
x.header("column1", "column2", "column3", "column4")
x.append_wrap("t1", None, "very long entry", ["test", "test1", "test2"])
x.append_wrap(("r", "r3"), ("l", "l2"), "also long entry", "test")
print(x)
output
+-----------------------------------------------+
| Test title |
+---------+---------+-----------------+---------+
| column1 | column2 | column3 | column4 |
+---------+---------+-----------------+---------+
| t1 | | very long entry | test |
| | | | test1 |
| | | | test2 |
| r | l | also long entry | test |
+---------+---------+-----------------+---------+
1.9 rowdict
code
# rowdict
x = Ttable("Test rodict title", 4)
x.header("k1", "k2", "k3", "k4")
x.append_dict({"k1": "v11", "k2": "v12", "k3": "v13", "k4": "v14"})
x.append_dict({"k1": "v22", "k2": "v22", "k3": "v23", "k4": "v24"}, "1")
x.append_dict({"k1": "v32", "k2": "v32", "k3": "v33", "k4": "v34"}, "3")
x.append_dict({"k1": "v42", "k2": "v42", "k3": "v43", "k4": ["v44_1", "v44_2"]}, "3", {"k2": "4"})
x.append_dict({"k1": "v42", "k2": "v42", "k3": "v43", "k4": ["v44_1", "v44_2"]}, "3", {"k2": "4"},
is_wrapline=True)
print(x)
output
+--------------------------------------+
| Test rodict title |
+-----+-----+-----+--------------------+
| k1 | k2 | k3 | k4 |
+-----+-----+-----+--------------------+
| v11 | v12 | v13 | v14 |
| v22 | v22 | v23 | v24 |
| v32 | v32 | v33 | v34 |
| v42 | v42 | v43 | ['v44_1', 'v44_2'] |
| v42 | v42 | v43 | v44_1 |
| | | | v44_2 |
+-----+-----+-----+--------------------+
1.10 中文
code
# 中文
x = Ttable("Test title", 4)
x.header("column1", "column2", "column3", "column4")
x.append("t1", "t2", "very long entry", "中文展示一下")
x.append("t1", "t2", "very long entry", "中文展示一下A")
x.append("t1", "t2", "very long entry", "中文展示一下B")
x.append("t1", "t2", "very long entry", "中文展示一下BC")
x.append("t1", "t2", "very long entry", "中文展示一下BCD")
x.append(("r", "r3"), ("中文展示A", "l2"), "also long entry", "test")
x.append(("r", "r3"), ("中文展示AB", "l2"), "also long entry", "test")
print(x)
output
+----------------------------------------------------------+
| Test title |
+---------+------------+-----------------+-----------------+
| column1 | column2 | column3 | column4 |
+---------+------------+-----------------+-----------------+
| t1 | t2 | very long entry | 中文展示一下 |
| t1 | t2 | very long entry | 中文展示一下A |
| t1 | t2 | very long entry | 中文展示一下B |
| t1 | t2 | very long entry | 中文展示一下BC |
| t1 | t2 | very long entry | 中文展示一下BCD |
| r | 中文展示A | also long entry | test |
| r | 中文展示AB | also long entry | test |
+---------+------------+-----------------+-----------------+
2 ttable_shortcuts
2.1 横版表格
from xlib.util.ascii_art import ttable_shortcuts
#!/usr/bin/python
# coding=utf8
"""
# Author: meetbill
# Created Time : 2022-11-06 14:46:15
# File Name: api_unit_list.py
# Description:
"""
from xlib import retstat
from xlib.db import peewee
from xlib.db import shortcuts
from xlib.middleware import funcattr
from xlib.httpgateway import Request
from xlib.util.ascii_art import ttable_shortcuts
from handlers.x1 import model_scs
__info = "unit_list"
__version = "1.0.1"
@funcattr.api
def unit_list(req, ip, display="table", region=None):
"""
Args:
ip: String
"""
assert isinstance(req, Request)
if region:
req.idc = region
unit_model = model_scs.CacheInstance
query_cmd = unit_model.select()
expressions = []
expressions.append(peewee.NodeList((unit_model.floating_ip, peewee.SQL('='), ip)))
query_cmd = query_cmd.where(*expressions)
record_list = query_cmd
record_dict_list = []
for record in record_list:
record_dict_list.append(shortcuts.model_to_dict(record))
if display == "table":
ttable_shortcuts.table_show(data=record_dict_list,
title="unit",
headers=["id", "shard_id", "port", "res_flavor", "status"],
)
return retstat.OK, {"data": {}}, [(__info), __version]
return retstat.OK, {"data": {"list": record_dict_list}}, [(__info), __version]
Last updated