https://github.com/ispaneli/tchoice
Lightweight implementation of Django-like `TextChoices` and `IntegerChoices`.
https://github.com/ispaneli/tchoice
backend dataclass django enum fastapi pydantic python
Last synced: 4 months ago
JSON representation
Lightweight implementation of Django-like `TextChoices` and `IntegerChoices`.
- Host: GitHub
- URL: https://github.com/ispaneli/tchoice
- Owner: ispaneli
- License: mit
- Created: 2025-01-21T18:40:00.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-21T19:08:27.000Z (over 1 year ago)
- Last Synced: 2025-11-08T01:22:17.290Z (7 months ago)
- Topics: backend, dataclass, django, enum, fastapi, pydantic, python
- Language: Python
- Homepage: https://pypi.org/project/tchoice/
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```bash
pip install tchoice
```
## How to Use
### Defining Choices
You can define your choices using `TextChoices` or `IntegerChoices`:
```python
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
```python
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:
```python
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](https://github.com/ispaneli/tchoice/blob/master/LICENSE).