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.
Metadata
Release files for tchoice 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tchoice-0.0.1.tar.gz | 4.4 kB | Without provenance | Details for tchoice-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tchoice-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for tchoice-0.0.1-py3-none-any.whl |
Total release size: 9.1 kB
Release files / tchoice-0.0.1.tar.gz
| Download URL | tchoice-0.0.1.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c6570641bd2ba2968b3e21ceee3e05c05422742782d711bef6a0a5fbac24f4be
|
|
BLAKE2b-256 checksum How to use checksums |
028d6d9e77d315511880e11ba9ac0e3a6ce26b4cb84c8c0cdb480746f0a69469
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / tchoice-0.0.1-py3-none-any.whl
| Download URL | tchoice-0.0.1-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
43743a24006dcebfa9cdd440c95f672f6ff420df5a2226e020d623b9289124c9
|
|
BLAKE2b-256 checksum How to use checksums |
17f0a7b700d02bbde12d4d3a10725414ead5a26b99736ed727ee5863bb686b9e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Security for tchoice 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.