Printing and debugging with color
Project description
cprintf
一个增强版的彩色终端打印与日志格式化输出工具库。
安装
pip install cprintf
主要功能
- 日志级别控制:提供
DEBUG、INFO、WARNING、ERROR(或简写ERR)、FATAL五种日志级别控制。通过set_log_level()动态调整过滤级别。 - 原生多参数支持:支持像原生
print一样传入多个参数(*args),并支持使用sep控制分隔符,支持prefix和suffix参数。 - 中文字符表格对齐:智能识别中文字符(CJK)终端实际显示宽度,支持左对齐、右对齐与居中对齐,支持不规则列数据安全补齐。
- 线程安全:内部采用线程互斥锁,多线程并发时输出不乱序、不插断。
- 进度条:优雅且健壮的进度渲染,支持除零与溢出防护,以及自定义颜色。
- 自动格式化:智能检测 dict/list 复杂数据结构并自动进行 JSON 缩进美化或 pprint 呈现,并进行了 JSON 首字符快速前置过滤以提高性能。
- 时间戳输出:一键开启时间戳日志前缀(
timestamp=True)。
使用示例
1. 基础彩色日志与多参数支持
import cprintf
# 设置日志级别(默认是 INFO)
cprintf.set_log_level('DEBUG')
# 基础打印
cprintf.debug("这是一条调试信息")
cprintf.info("普通字符串")
cprintf.ok("这是一条成功或确认信息")
cprintf.warn("这是一条警告信息")
cprintf.err("这是一条错误信息")
# 支持多参数打印(类似于内置 print)
cprintf.info("参数1", "参数2", "参数3", sep=" | ", prefix="[APP] ", suffix=" [END]")
cprintf.ok("解析结果:", {"status": "success", "code": 200}, sep=" ==> ")
2. 带时间戳的日志
cprintf.info("这是一条带时间戳的信息", timestamp=True)
# 输出: [2026-06-24 17:30:10] 这是一条带时间戳的信息
3. 表格打印(支持中文字符与对齐方式)
print_table 完美支持中英文字符混排、显示宽度对齐,并支持 left(左对齐,默认)、right(右对齐)、center(居中对齐)。
headers = ["项目名称", "当前进度", "运行状态"]
data = [
["项目A(核心系统)", "75.5%", "进行中"],
["项目B(辅助工具)", "100.0%", "已完成"],
["项目C", "0.0%", "未开始"]
]
# 居中对齐表格
cprintf.print_table(data, headers=headers, color='OK', align='center')
4. 进度条
import time
for i in range(100):
time.sleep(0.05)
color = 'OK' if i < 50 else 'WARNING' if i < 80 else 'ERR'
cprintf.progress_bar(i + 1, 100, prefix='处理中:', suffix=f'阶段 {i//20 + 1}', color=color)
5. 分割线与自定义颜色
# 打印一条分割线
cprintf.line(char='=', length=60, color='OK')
# 自定义 ANSI 颜色码输出
cprintf.custom("This is a custom color message in cyan!", "\033[96m")
# 仅获取带 ANSI 颜色代码的字符串而不进行打印
colored_str = cprintf.get_colored_string("这是一个带颜色的字符串", color='WARNING')
print(colored_str)
格式化模式
可以使用 format_mode 显式控制复杂对象格式化:
'auto'(默认):如果是 dict/list 且符合 JSON 规范,则转为美化 JSON;若不是则使用pprint。'json':强制转换为美化 JSON。'pprint':强制使用pprint.pformat格式化。'raw':强转为原始字符串,如str()或repr()。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cprintf-0.0.12.tar.gz
(7.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cprintf-0.0.12.tar.gz.
File metadata
- Download URL: cprintf-0.0.12.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e17386d7a8207285288eb1913b2e01e14291baed7530d4c4f007f69703cd23f
|
|
| MD5 |
0266197f0a282d9c3b8271b579bf027a
|
|
| BLAKE2b-256 |
8d3d07314f760f74d62f54215e6752bf05525f1e874b74e7cdf18cd757041fdb
|
File details
Details for the file cprintf-0.0.12-py3-none-any.whl.
File metadata
- Download URL: cprintf-0.0.12-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc8605670309250c8d39cb7fc3f960ff2d0284cfe2d52e2ca2d32c90933605a8
|
|
| MD5 |
676998132138400eb0100a7fbc85ea2e
|
|
| BLAKE2b-256 |
05367a0fc4dc9c9feb28f3d75ccc0c9ac635d244aa09bad5ff1700ab547a7901
|