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

Uploaded Python 3

File details

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

File metadata

  • Download URL: my_ask_lib-0.1.8.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.8.tar.gz
Algorithm Hash digest
SHA256 87800670ae185719a5303a8b2b9ad51ce0419e8153bff2476a164b90796edbbd
MD5 d103ff726ee5f6345446e368fe2dc4e6
BLAKE2b-256 c9ce9dafcd75100bd8d9095fd7073d499657d22d7ecd2ce1277f194cd32ab192

See more details on using hashes here.

File details

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

File metadata

  • Download URL: my_ask_lib-0.1.8-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.8-py3-none-any.whl
Algorithm Hash digest
SHA256 da9daba0358c062a28c6fc4d3449cbab2c8ca9ca2b6a5986fdd71e395aaf03df
MD5 0c50ee428a9e9a60fcfda144ca7630f3
BLAKE2b-256 01f9fcba8a6c506390129ae68194e731bda4524c1ce85d5f61a6c22d5a21616a

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