kkinput
终端里收集用户输入,输入不规范就一直问,直到拿到合法的值。
写终端程序时,取输入最烦的不是 input() 本身,而是它从来不挑食。
用户随手敲个回车、把年龄填成 abc、把日期写成「明天」,你还得自己套一层
while True 加一堆 try / except 去兜,兜不完全还得重来。
kkinput 把这件事收进一个调用:传进去一个「值对象」,它负责生成提示语、
校验输入、不合格就再问一遍。
安装
pip install kkinput
30 秒上手
from kkinput import kk_input, IntValue, StrValue, ChoiceValue, BooleanValue, DatetimeValue
from datetime import datetime
age = kk_input(IntValue('年龄', min_value=1, max_value=120), loop=True)
nick = kk_input(StrValue('昵称', default='游客', max_length=8), loop=True)
fruit = kk_input(ChoiceValue('水果', choices=['苹果', '香蕉', '橘子']), loop=True)
again = kk_input(BooleanValue('再来一次', default=True), loop=True)
deadline = kk_input(DatetimeValue('截止时间', end=datetime(2027, 1, 1)), loop=True)
跑起来就是这个样子——用户填错,程序只是把要求再说一遍,不会崩:
请输入年龄:abc
年龄必须是数字
请输入年龄:200
年龄必须在1~120之间
请输入年龄:30
请从下列选项中,选择相应水果:
0: 苹果
1: 香蕉
2: 橘子
请输入水果的选项:9
水果必须在可选范围内
请输入水果的选项:2
内置值类型
| 类型 | 校验内容 | 额外参数 | 合法时返回 |
|---|---|---|---|
StrValue |
字符串,可限制长度 | max_length |
str |
IntValue |
整数,可限制区间 | min_value / max_value |
int |
FloatValue |
浮点数,可限制区间 | min_value / max_value |
float |
BooleanValue |
只认 y / n |
— | bool |
ChoiceValue |
必须落在备选项里 | choices |
int(选项序号) |
DatetimeValue |
能被解析的日期时间,可限制区间 | start / end |
datetime |
三种公共参数所有类型都支持:
name:字段名,用在提示语和报错里;default:默认值,用户直接敲回车就采用它;placeholder:完全自定义提示语,给了就不再自动生成。
ChoiceValue返回的是选项序号,不是选项本身。要拿到选项文字, 用choices[result]。
关于 loop
loop=True:值不合法就一直重问,直到问出一个合法的。
loop=False(默认):只问一次,不合法就返回 None。
两种模式都不会抛异常——包括用户直接按了 Ctrl+C 之外的各种奇怪输入。
所以调用方只需要判断返回值是不是 None,不需要写 try / except:
port = kk_input(IntValue('端口', min_value=1, max_value=65535))
if port is None:
print('没取到端口,用默认的 8080')
port = 8080
自己加一种值类型
继承 BaseValue,实现 valid() 就够了。约定是:校验不过就 print 提示并返回
None,通过就返回转换后的值。
from kkinput import BaseValue, kk_input
class IpValue(BaseValue):
def valid(self, result):
super_result = super().valid(result) # 先走公共逻辑:空值取默认值
if super_result is not None:
return super_result
parts = result.split('.')
if len(parts) != 4 or not all(p.isdigit() and 0 <= int(p) <= 255 for p in parts):
print(f'{self.name}必须是合法的 IPv4 地址')
return None
return result
ip = kk_input(IpValue('设备地址'), loop=True)
依赖
- Python >= 3.9
- python-dateutil
许可
MIT
关于作者
微信公众号:Python卡皮巴拉
🌟【Python卡皮巴拉】—— 你的Python修炼秘籍,代码界的“神兽”驾到!🌟
Metadata
Release files for kkinput 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kkinput-0.1.0.tar.gz | 6.0 kB | Without provenance | Details for kkinput-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kkinput-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for kkinput-0.1.0-py3-none-any.whl |
Total release size: 12.1 kB
Release files / kkinput-0.1.0.tar.gz
| Download URL | kkinput-0.1.0.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9a4710a5a842023a55822839779ad9e109d1775dc3e292515fd0a3c7d1d90491
|
|
BLAKE2b-256 checksum How to use checksums |
326d567ef50e8b5b681bc228be670beb29b72663429edbb5c0e45e85b26b7aaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.2
|
Release files / kkinput-0.1.0-py3-none-any.whl
| Download URL | kkinput-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6359464559b9e2a34613cbf92d78543d846788eaa4ab6f4a58b9445f79a248f8
|
|
BLAKE2b-256 checksum How to use checksums |
bfe3a079684d802984a32adf3eacb16d7ed9e453b1b50cf1ba141018c2e70d3e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.2
|
Security for kkinput 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.