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.7.tar.gz (2.9 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.7-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: my_ask_lib-0.1.7.tar.gz
  • Upload date:
  • Size: 2.9 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.7.tar.gz
Algorithm Hash digest
SHA256 0a979ced4e9ba65eacdb6ac699d46dd175ab55c2921c08073c6b40ccb06fbe79
MD5 82f017ba6e0d47e54bf3497f353d3a0b
BLAKE2b-256 da481d925c02c29fa0abea55456f142b2efde6517a7699834ccd7c7d59c2dff6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: my_ask_lib-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 3.4 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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 01b411986b45e2c6aba93dbe53d9145b965e236f44892a52d284fe683506b055
MD5 26610dba6486e244c0d79eb6c969ab40
BLAKE2b-256 3f18a7ee609709f659472a3b1fefc3ea7ed5fa3c14b1da838515c705a096e604

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