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.2.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.2-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: my_ask_lib-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 1588cea395309c062a93b681dc127568be936b9ba0fcead49f8d9aa90fd1b595
MD5 adacbef764b93c5491b07d24df86dbfc
BLAKE2b-256 90f189c6fe2d5ffefdd194716d3107be75f1ed354145c3228651a141d438ce27

See more details on using hashes here.

File details

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

File metadata

  • Download URL: my_ask_lib-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.3 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 75212890394f6b5b1d7fc28726e061ffdbb1b11d6537cedc788073928baa53c2
MD5 0a12d328ebd6aad77196f341ecec18df
BLAKE2b-256 f93f675f1c46cf39d81d586ec99b9c97bcd06b97930219d6be2fef5119dee159

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