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

Uploaded Python 3

File details

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

File metadata

  • Download URL: my_ask_lib-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 0de8eb8f2d38fe98195c10d1b62ccdb655da31ad436c24e4530ae8cc6d397420
MD5 3cf3e7345d1cf904102d192803faf3af
BLAKE2b-256 bfa439c9e4e2793a9e896d0192a7ba8b532981064ec7f8bad9684964175de85b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: my_ask_lib-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ea1b907c41b3a05451105d35d9d6da21d617d88a22b60c00425b27fec965ace0
MD5 82fb6dcdf2cd15a318b7ef7e35540336
BLAKE2b-256 13f4cc32c8d21858d85b1beb836fa697c8cf03e7aafc52d9e7a17b9cda5491f5

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