安全输入工具库,支持彩色输出,KeyboardInterrupt保护等
Project description
Luci_Code_Tools
介绍:
给初学者制作的 Python 工具库, 基于 Python 3.13.3 制作
安装 & 导入
pip install Luci-Code-Tools
from Luci_code_tools import *
# 或者
import Luci_code_tools
内容
函数
safe_* = safe_input, safe_int, safe_float
| 名称 | 作用 | 参数 |
|---|---|---|
| color | 输出ANSI彩色字符 | data, text_color, bg_color, mode, newline |
| color_rgb | 输出RGB彩色字符 | data, r, g, b, mode, newline |
| clear_screen | 可跨平台的清屏 | None (没有参数) |
| safe_* | 安全的输入, 防止Ctrl + C和输入流关闭等报错 | prompt, text_color, bg_color, mode |
提示: 参数需要按照顺序填
用法
color()
color 函数有 5 个参数, 你可以选填
1.data, data 参数就仅仅是输出的内容而已
2.text_color, text_color 参数可以让输入有颜色, 参数表见其他部分段
3.bg_color, bg_color 全称 background_color, 用于管理输出的背景颜色
4.mode, mode 参数用于管理输出的格式, 参数表见其他部分段
5.newline, newline 参数只有 True 和 False, 默认为 True, 测试:
""" newline 参数测试 """
# newline = False
"""
输出内容-1
输出内容-2
输入: a
if 跳转的输出
"""
# newline = True
"""
输出内容-1
输出内容-2
输入:a
if 跳转的输出
"""
# newline 让输出变得更加美观, 可分辨
color_rgb()
color_rgb 有 5 个参数, 分别是data, r, g, b, newline
newline 和 data 的用法和之前的一样, 这里不过多赘述
r, g, b, r, g, b 参数使用 RGB 标准显示彩色, r, g, b 是字面意思, 让你填入 rgb 值, 按顺序填
clear_screen()
clear_screen源码
def clear_screen() -> None: # 清屏: 兼容版
""" 跨平台兼容清屏 """
try: # 检测环境
if platform == "win32": # 环境为 Windows
run(['cmd', '/c', 'cls']) # 安全: 使用列表命令, 防注入
else: # 包括 MacOS, Linux (Android 也是 Linux 列)
run(['clear'], check=False) # check = 遇到错误我自己来, 别报错
except Exception: # 非交互式环境 (.py 打包.exe类环境等没有cmd命令的环境)
print("\n" * 200) # 手动清屏
clear_screen 可以直接使用, 无需参数, 自动适配平台并运行相应的 清屏命令
清屏命令:
""" Windows系统: **cls**, MacOS / Linux: **clear** """
safe_input, safe_int, safe_float
这三条命令我会逐条讲解:
- safe_input
顾名思义: 安全的输入字符, 用处为防止用户 Ctrl + C 和 输入流停止 而造成的报错
用法: prompt, text_color, bg_color, mode
此处的 text_color, bg_color, mode 的用法和之前的 color 函数没有区别, prompt 就相当于 data
- safe_int / safe_float
由于这两条命令的用法相同我就放在一起讲了
参数: 和 safe_input 的相同
作用: 既然是输入数字我们就要防止用户输入字符, 不然会触发 ValueError, 所以代码中有 try - except ValueError, KeyboardInterrupt, EOFError 的防范, ValueError 输入错误会重新输入, KeyboardInterrupt / EOFError 就和之前一样
其他
ANSI常用序列 提示: 90-97 为亮色前景色,100-107 为亮色背景色
| 前景色 | 效果 |
|---|---|
| 30 | 黑色 |
| 31 | 红色 |
| 32 | 绿色 |
| 33 | 黄色 |
| 34 | 蓝色 |
| 35 | 紫色 |
| 36 | 青色 |
| 37 | 白色 |
| 显示方式 | 效果 |
|---|---|
| 0 | 默认 |
| 1 | 粗体 + 高亮 |
| 2 | 暗淡 |
| 3 | 斜体(部分终端不支持) |
| 4 | 下划线 |
| 5 | 闪烁 |
| 6 | 快闪(部分终端不支持) |
| 7 | 反色(前景色与背景色互换) |
| 8 | 隐藏 |
| 9 | 删除线 |
| 背景色 | 效果 |
|---|---|
| 40 | 黑色 |
| 41 | 红色 |
| 42 | 绿色 |
| 43 | 黄色 |
| 44 | 蓝色 |
| 45 | 紫色 |
| 46 | 青色 |
| 47 | 白色 |
制作者 & 感谢名单
| 作者 | 分工 |
|---|---|
| DeepSeek | 检查 |
| DeepSeek | 改进 |
| Luci_Code | 代码 |
| Luci_Code | 文档 |
| Luci_Code | 思路 |
| Luci_Code | 上传 |
感谢
<感谢您的阅读,我是Luci_Code, 一名2014年出生的开发者 >
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
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 luci_code_tools-2.0.0.tar.gz.
File metadata
- Download URL: luci_code_tools-2.0.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
009bff7b811d45f6262036b5e5183671e29004bbc496a0a98a6743f7751ac785
|
|
| MD5 |
d683dad989d89bb9eaeeac2311713803
|
|
| BLAKE2b-256 |
4d46318d7d4e3b6f7bd9d016ba5acc272641efedab1435d295cb5702aaa35c89
|
File details
Details for the file luci_code_tools-2.0.0-py3-none-any.whl.
File metadata
- Download URL: luci_code_tools-2.0.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
200cf8472ba0ff699463342f00a417c89491648362bd35f5fcc42881d7181fd8
|
|
| MD5 |
94ef5cdd6543df1d4172a954e22d1b69
|
|
| BLAKE2b-256 |
f56fdaaa3e93db03a22dc6c70bf6a47fc307449a69a06658fd02b8875c33aeae
|