Skip to main content

A simple custom input validation utility

Project description

my-ask-lib

A simple, robust, and beautiful custom input validation utility for Python, built on top of the rich library.

เคยไหมที่ต้องคอยเขียนลูป while True และ try-except ซ้ำๆ ทุกครั้งที่ต้องการรับค่าจากผู้ใช้และตรวจสอบความถูกต้อง? Library ตัวนี้จะช่วยรวบรวมตรรกะเหล่านั้นให้เหลือเพียงการเรียกใช้งานฟังก์ชันเดียว!


✨ Features

  • Auto-Validation: ตรวจสอบประเภทข้อมูล (str, int, float, ฯลฯ) ให้อัตโนมัติ
  • Custom Condition: รองรับการเช็คเงื่อนไขเพิ่มเติมผ่าน Lambda หรือ Function
  • Rich Logging: แสดงข้อความแจ้งเตือนเมื่อกรอกข้อมูลผิดพลาดอย่างสวยงามด้วย rich.logging
  • Empty Check: ป้องกันการกด Enter ผ่านไปเฉยๆ โดยไม่ได้กรอกข้อมูล (เปิด/ปิด ได้)

🚀 Installation

ติดตั้งผ่าน pip ได้ง่ายๆ:

pip install my-ask-lib

🎯 Usage Examples

เรียกใช้งานโดยการนำเข้าฟังก์ชัน ask จาก package my_ask:

from my_ask import ask

# 1. รับค่าข้อความทั่วไป (ห้ามปล่อยว่าง)
name = ask("Enter your name: ")

# 2. รับค่าตัวเลขจำนวนเต็ม (int) พร้อมดักจับ Format
age = ask("Enter your age: ", type_fn=int)

# 3. รับค่าพร้อมตรวจสอบเงื่อนไข (เช่น อายุต้องห้ามติดลบ)
score = ask(
    prompt="Enter your score (0-100): ",
    type_fn=int,
    validate=lambda x: 0 <= x <= 100,
    error_msg="Score must be between 0 and 100!"
)

# 4. ยอมให้ผู้ใช้กด Enter ผ่านได้ (Allow Empty)
nickname = ask("Enter your nickname (Optional): ", allow_empty=True)

⚙️ Parameters

Parameter Type Default Description
prompt str Required ข้อความที่ต้องการแสดงให้ผู้ใช้เห็นตอนรับ Input
type_fn Callable str ฟังก์ชันที่ใช้แปลงประเภทข้อมูล เช่น int, float
validate Callable None ฟังก์ชันหรือ Lambda ที่คืนค่าเป็น True/False เพื่อเช็คเงื่อนไขเพิ่ม
error_msg str None ข้อความเตือนที่จะแสดงเมื่อไม่ผ่านเงื่อนไขใน validate
allow_empty bool False หากเป็น False จะไม่ยอมให้ผู้ใช้กด Enter โดยไม่พิมพ์อะไรเลย

📝 License

MIT License


---

### 💡 คำแนะนำเพิ่มเติม
* สังเกตตรงวิธีพิมพ์เรียกใช้ในส่วนของ Usage ตัวอย่างจะเป็น `from my_ask import ask` (อ้างอิงตามโฟลเดอร์ `src/my_ask` ที่เราจัดไว้ในขั้นตอนแรกสุด)
* อย่าลืมนำไฟล์นี้ไปวางไว้ที่โฟลเดอร์นอกสุดของโปรเจกต์ (`my_ask_lib/`) คู่กับไฟล์ `pyproject.toml` นะครับ จากนั้นตอนคุณ Rebuild รอบหน้า ตัวโปรเจกต์บนหน้าเว็บ PyPI ก็จะมีคำอธิบายนี้ไปโชว์ให้คนอื่นอ่านได้ทันทีครับ!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

my_ask_lib-0.1.5.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

my_ask_lib-0.1.5-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file my_ask_lib-0.1.5.tar.gz.

File metadata

  • Download URL: my_ask_lib-0.1.5.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for my_ask_lib-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2c011a098424d601583619ead473fe78a4ab6a67eec9a1ce4034e4d86bacfbe9
MD5 b4ccc07daa8163358596db0c591a41d0
BLAKE2b-256 aeb4ca93cd1801959a7e03def5e1b78b9709c161ae04f028745d4bbfae335c6b

See more details on using hashes here.

File details

Details for the file my_ask_lib-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: my_ask_lib-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for my_ask_lib-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6f4f7d20fe37564aed5f2ff3c53092be68496a35f4ff1c0f5ec347e153e02274
MD5 43c2b4d2e3627b94e51551a173f3ecaa
BLAKE2b-256 bda2e0b186e0fdd82d3b32c280057326623c8aa99bb6751bc55574fb790f77cf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page