網站首頁 編程語言 正文
在開發過程中經常碰到在本地環境無法完成聯調測試的情況,必須到統一的聯機環境對接其他系統測試。往往是出現了BUG難以查找數據記錄及時定位到錯誤出現的位置。
面對這種情況可能情況可能是一個簡單的BUG導致的,但是定位問題往往就需要很長的時間。在python編程中推薦非標準庫tabulate,它可以將程序運行過程中產生的數據記錄格式化的打印出來很方便我們定位問題。
通過結合簡單的日志非標準庫loguru可以很快的打印出又美觀又實用的日志記錄,loguru非標準庫其實在一些文章的源碼示例中我們已經在使用了。
安裝過程還是通過pip的方式直接安裝,由于loguru、tabulate都是python的非標準庫,因此,沒有安裝的話需要安裝一下。默認我們都使用的清華大學的python鏡像站,大家可以選擇其他的鏡像站都可以。
pip install loguru -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip install tabulate - i https://pypi.tuna.tsinghua.edu.cn/simple/
做好準備工作以后將loguru、tabulate模塊都導入進來就OK了,沒有其他復雜的操作!
# It's a shortcut to create a logger with the default configuration.
from loguru import logger
# It's importing the function `tabulate` from the module `tabulate`.
from tabulate import tabulate
關于非標準庫tabulate,它的打印模式其實有很多,我們平常使用到的可能就是幾種比較常見的,下面將tabulate所有的打印模式全部列舉出來,有需要的大佬可以參考。
'''
"plain"
"simple"
"github"
"grid"
"fancy_grid"
"pipe"
"orgtbl"
"jira"
"presto"
"psql"
"rst"
"mediawiki"
"moinmoin"
"youtrack"
"html"
"latex"
"latex_raw"
"latex_booktabs"
"textile"
'''
我們選擇其中的一種'grid'模式來看看效果如何,因為這種模式打印的數據記錄也是比較常見的。下面創建一個函數tab_grid_log打印一段數組形式的數據記錄。
def tab_grid_log():
"""
> This function takes a list of lists and returns a list of lists with the log of each element
"""
# It's defining the header of the table.
header = [u'姓名', u'年齡', u'班級', u'成績', u'表現']
# It's defining a list of lists.
table = [('Python', 20, 1710, 98, 5.0), ('Java', 22, 1810, 98, 4.9)]
# It's printing the table with the `grid` format.
logger.info(tabulate(table, headers=header, tablefmt='grid'))
tab_grid_log()
# 2022-09-17 18:33:00.472 | INFO | __main__:tab_grid_log:66 - +--------+--------+--------+--------+--------+
# | 姓名 | 年齡 | 班級 | 成績 | 表現 |
# +========+========+========+========+========+
# | Python | 20 | 1710 | 98 | 5 |
# +--------+--------+--------+--------+--------+
# | Java | 22 | 1810 | 98 | 4.9 |
# +--------+--------+--------+--------+--------+
#
# Process finished with exit code 0
使用grid模式的打印的數據記錄就是宮格形式很便于查找日志中的數據記錄,也是我們經常在日志記錄中使用的一種打印方法。
接下來我們隨便選擇一種模式再次進行打印,這里就選擇html模式來看看效果吧,這種模式之前沒有使用過我很好奇它會打印出什么樣的效果。
def tab_html_log():
"""
> This function takes a log file and returns a html table of the log file
"""
# It's defining the header of the table.
header = [u'姓名', u'年齡', u'班級', u'成績', u'表現']
# It's defining a list of lists.
table = [('Python', 20, 1710, 98, 5.0), ('Java', 22, 1810, 98, 4.9)]
# It's printing the table with the `html` format.
logger.info(tabulate(table, headers=header, tablefmt='html'))
tab_html_log()
# 2022-09-17 18:37:50.383 | INFO | __main__:tab_html_log:87 - <table>
# <thead>
# <tr><th>姓名 </th><th style="text-align: right;"> 年齡</th><th style="text-align: right;"> 班級</th><th style="text-align: right;"> 成績</th><th style="text-align: right;"> 表現</th></tr>
# </thead>
# <tbody>
# <tr><td>Python</td><td style="text-align: right;"> 20</td><td style="text-align: right;"> 1710</td><td style="text-align: right;"> 98</td><td style="text-align: right;"> 5 </td></tr>
# <tr><td>Java </td><td style="text-align: right;"> 22</td><td style="text-align: right;"> 1810</td><td style="text-align: right;"> 98</td><td style="text-align: right;"> 4.9</td></tr>
# </tbody>
# </table>
從打印結果可以看出來了,使用html模式的打印時實際上是生成html的源碼,這還是很智能的包括style的樣式屬性也填充了。
原文鏈接:https://www.cnblogs.com/lwsbc/p/16773668.html
相關推薦
- 2022-09-08 Docker容器使用方法詳解_docker
- 2022-07-31 一文理解Goland協程調度器scheduler的實現_Golang
- 2022-06-19 微信小程序前端如何調用python后端的模型詳解_python
- 2023-02-08 Pytorch中torch.repeat_interleave()函數使用及說明_python
- 2022-04-25 jquery實現表格行的上下移動和置頂_jquery
- 2022-11-22 Redis?BloomFilter布隆過濾器原理與實現_Golang
- 2022-07-11 docker搭建redis 主從哨兵集群
- 2022-11-19 Python?numpy?ndarray屬性,索引,切片_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支