TChoice - Lightweight implementation of Django-like `TextChoices` and `IntegerChoices`.
Project description
TChoice
Lightweight implementation of Django-like TextChoices and IntegerChoices
for Python applications.
Problem Statement
When working with FastAPI or other frameworks, it's often useful to have
enumerations with labels for better readability and usability in models,
forms, and APIs. While Django provides TextChoices and IntegerChoices
for this purpose, they are tightly coupled to the Django framework.
tchoice offers a standalone solution, allowing you to use similar
functionality without requiring Django.
Installation
Install the package via pip:
pip install tchoice
How to Use
Defining Choices
You can define your choices using TextChoices or IntegerChoices:
from tchoice import TextChoices, IntegerChoices
class ColorChoices(TextChoices):
RED = "red", "Red Color"
GREEN = "green", "Green Color"
BLUE = "blue", "Blue Color"
class SizeChoices(IntegerChoices):
SMALL = 1, "Small Size"
MEDIUM = 2, "Medium Size"
LARGE = 3, "Large Size"
Accessing Labels and Values
print(ColorChoices.RED) # Output: red
print(ColorChoices.RED.label) # Output: Red Color
print(SizeChoices.SMALL) # Output: 1
print(SizeChoices.SMALL.label) # Output: Small Size
Using Choices in Models
For FastAPI and Pydantic:
from pydantic import BaseModel
from tchoice import TextChoices
class ColorChoices(TextChoices):
RED = "red", "Red Color"
GREEN = "green", "Green Color"
BLUE = "blue", "Blue Color"
class Item(BaseModel):
name: str
color: ColorChoices
# Example usage
item = Item(name="Example Item", color=ColorChoices.RED)
print(item.color) # Output: red
print(item.color.label) # Output: Red Color
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tchoice-0.0.1.tar.gz.
File metadata
- Download URL: tchoice-0.0.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6570641bd2ba2968b3e21ceee3e05c05422742782d711bef6a0a5fbac24f4be
|
|
| MD5 |
24e922457b25a8b6ec58b9315e0780b3
|
|
| BLAKE2b-256 |
028d6d9e77d315511880e11ba9ac0e3a6ce26b4cb84c8c0cdb480746f0a69469
|
File details
Details for the file tchoice-0.0.1-py3-none-any.whl.
File metadata
- Download URL: tchoice-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43743a24006dcebfa9cdd440c95f672f6ff420df5a2226e020d623b9289124c9
|
|
| MD5 |
679047f90f6d1ea7e49766fba056e581
|
|
| BLAKE2b-256 |
17f0a7b700d02bbde12d4d3a10725414ead5a26b99736ed727ee5863bb686b9e
|