tchoice


Nametchoice JSON
Version 0.0.1 PyPI version JSON
download
home_pagehttps://github.com/ispaneli/tchoice
SummaryTChoice - Lightweight implementation of Django-like `TextChoices` and `IntegerChoices`.
upload_time2025-01-21 19:03:32
maintainerNone
docs_urlNone
authorIlya Antonov
requires_python<4,>=3.11
licenseNone
keywords django textchoices intchoices backend fastapi microservice typing enum dataclass dataclasses pydantic
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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).


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ispaneli/tchoice",
    "name": "tchoice",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.11",
    "maintainer_email": null,
    "keywords": "django, textchoices, intchoices, backend, fastapi, microservice, typing, enum, dataclass, dataclasses, pydantic",
    "author": "Ilya Antonov",
    "author_email": "ispanelki@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/02/8d/6d9e77d315511880e11ba9ac0e3a6ce26b4cb84c8c0cdb480746f0a69469/tchoice-0.0.1.tar.gz",
    "platform": null,
    "description": "# TChoice\n\nLightweight implementation of Django-like `TextChoices` and `IntegerChoices` \nfor Python applications.\n\n## Problem Statement\n\nWhen working with FastAPI or other frameworks, it's often useful to have \nenumerations with labels for better readability and usability in models, \nforms, and APIs. While Django provides `TextChoices` and `IntegerChoices` \nfor this purpose, they are tightly coupled to the Django framework. \n`tchoice` offers a standalone solution, allowing you to use similar \nfunctionality without requiring Django.\n\n## Installation\n\nInstall the package via pip:\n\n```bash\npip install tchoice\n```\n\n## How to Use\n\n### Defining Choices\nYou can define your choices using `TextChoices` or `IntegerChoices`:\n\n```python\nfrom tchoice import TextChoices, IntegerChoices\n\nclass ColorChoices(TextChoices):\n    RED = \"red\", \"Red Color\"\n    GREEN = \"green\", \"Green Color\"\n    BLUE = \"blue\", \"Blue Color\"\n\nclass SizeChoices(IntegerChoices):\n    SMALL = 1, \"Small Size\"\n    MEDIUM = 2, \"Medium Size\"\n    LARGE = 3, \"Large Size\"\n```\n\n### Accessing Labels and Values\n\n```python\nprint(ColorChoices.RED)          # Output: red\nprint(ColorChoices.RED.label)    # Output: Red Color\n\nprint(SizeChoices.SMALL)         # Output: 1\nprint(SizeChoices.SMALL.label)   # Output: Small Size\n```\n\n### Using Choices in Models\n\nFor FastAPI and Pydantic:\n\n```python\nfrom pydantic import BaseModel\nfrom tchoice import TextChoices\n\nclass ColorChoices(TextChoices):\n    RED = \"red\", \"Red Color\"\n    GREEN = \"green\", \"Green Color\"\n    BLUE = \"blue\", \"Blue Color\"\n\nclass Item(BaseModel):\n    name: str\n    color: ColorChoices\n\n# Example usage\nitem = Item(name=\"Example Item\", color=ColorChoices.RED)\nprint(item.color)           # Output: red\nprint(item.color.label)     # Output: Red Color\n```\n\n---\n\n## License\n\nThis project is licensed under the terms of the [MIT license](https://github.com/ispaneli/tchoice/blob/master/LICENSE).\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "TChoice - Lightweight implementation of Django-like `TextChoices` and `IntegerChoices`.",
    "version": "0.0.1",
    "project_urls": {
        "Documentation": "https://github.com/ispaneli/tchoice",
        "Homepage": "https://github.com/ispaneli/tchoice",
        "Issue Tracker": "https://github.com/ispaneli/tchoice/issues",
        "Say Thanks!": "http://saythanks.io/to/ispanelki",
        "Source": "https://github.com/ispaneli/tchoice/tree/master/src/tchoice"
    },
    "split_keywords": [
        "django",
        " textchoices",
        " intchoices",
        " backend",
        " fastapi",
        " microservice",
        " typing",
        " enum",
        " dataclass",
        " dataclasses",
        " pydantic"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "17f0a7b700d02bbde12d4d3a10725414ead5a26b99736ed727ee5863bb686b9e",
                "md5": "679047f90f6d1ea7e49766fba056e581",
                "sha256": "43743a24006dcebfa9cdd440c95f672f6ff420df5a2226e020d623b9289124c9"
            },
            "downloads": -1,
            "filename": "tchoice-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "679047f90f6d1ea7e49766fba056e581",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.11",
            "size": 4682,
            "upload_time": "2025-01-21T19:03:30",
            "upload_time_iso_8601": "2025-01-21T19:03:30.686001Z",
            "url": "https://files.pythonhosted.org/packages/17/f0/a7b700d02bbde12d4d3a10725414ead5a26b99736ed727ee5863bb686b9e/tchoice-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "028d6d9e77d315511880e11ba9ac0e3a6ce26b4cb84c8c0cdb480746f0a69469",
                "md5": "24e922457b25a8b6ec58b9315e0780b3",
                "sha256": "c6570641bd2ba2968b3e21ceee3e05c05422742782d711bef6a0a5fbac24f4be"
            },
            "downloads": -1,
            "filename": "tchoice-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "24e922457b25a8b6ec58b9315e0780b3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.11",
            "size": 4405,
            "upload_time": "2025-01-21T19:03:32",
            "upload_time_iso_8601": "2025-01-21T19:03:32.700469Z",
            "url": "https://files.pythonhosted.org/packages/02/8d/6d9e77d315511880e11ba9ac0e3a6ce26b4cb84c8c0cdb480746f0a69469/tchoice-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-21 19:03:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ispaneli",
    "github_project": "tchoice",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "tchoice"
}
        
Elapsed time: 0.43346s