# Pyngo :snake:
<p align="center">
<em>Utils to help integrate pydantic into Django projects</em>
</p>
<p align="center">
<a href="https://github.com/yezz123/pyngo/actions/workflows/ci.yml" target="_blank">
<img src="https://github.com/yezz123/pyngo/actions/workflows/ci.yml/badge.svg" alt="Test">
</a>
<a href="https://codecov.io/gh/yezz123/pyngo">
<img src="https://codecov.io/gh/yezz123/pyngo/branch/main/graph/badge.svg"/>
</a>
<a href="https://pypi.org/project/pyngo" target="_blank">
<img src="https://img.shields.io/pypi/v/pyngo?color=%2334D058&label=pypi%20package" alt="Package version">
</a>
<a href="https://pypi.org/project/pyngo" target="_blank">
<img src="https://img.shields.io/pypi/pyversions/pyngo.svg?color=%2334D058" alt="Supported Python versions">
</a>
<a href="https://pepy.tech/project/pyngo" target="_blank">
<img src="https://static.pepy.tech/badge/pyngo" alt="Test">
</a>
</p>
---
## Installation
You can add pyngo in a few easy steps. First of all, install the dependency:
```sh
$ pip install pyngo
---> 100%
Successfully installed pyngo
```
---
## Features ๐
- Using Pydantic to Build your Models in Django Project.
- Using `OpenAPI` utilities to build params from a basic model.
- using `QueryDictModel` to build `Pydantic` models from a `QueryDict` object.
- propagate any errors from Pydantic in Django Rest Framework.
- Tested in Python 3.10 and up.
## Examples ๐
### OpenAPI
- `pyngo.openapi_params()` can build params from a basic model
```py
from pydantic import BaseModel
from pyngo import openapi_params
class Model(BaseModel):
bingo: int
print(openapi_params(Model))
```
- `pyngo.ParameterDict.required` is set according to the type of the variable
```py
from typing import Optional
from pydantic import BaseModel
from pyngo import openapi_params
class Model(BaseModel):
required_param: int
optional_param: Optional[int]
print(openapi_params(Model))
```
Other fields can be set through the fieldโs info:
```py
from pydantic import BaseModel, Field
from pyngo import openapi_params
class WithDescription(BaseModel):
described_param: str = Field(
description="Hello World Use Me!"
)
class InPath(BaseModel):
path_param: str = Field(location="path")
class WithDeprecated(BaseModel):
deprecated_field: bool = Field(deprecated=True)
class WithNoAllowEmpty(BaseModel):
can_be_empty: bool = Field(allowEmptyValue=False)
print(openapi_params(WithDescription)[0]["description"])
print(openapi_params(InPath)[0]["in"])
print(openapi_params(WithDeprecated)[0]["deprecated"])
print(openapi_params(WithNoAllowEmpty)[0]["allowEmptyValue"])
```
### Django
- `pyngo.querydict_to_dict()` and `pyngo.QueryDictModel` are conveniences for building a `pydantic.BaseModel` from a `django.QueryDict`.
```py
from typing import List
from django.http import QueryDict
from pydantic import BaseModel
from pyngo import QueryDictModel, querydict_to_dict
class Model(BaseModel):
single_param: int
list_param: List[str]
class QueryModel(QueryDictModel):
single_param: int
list_param: List[str]
query_dict = QueryDict("single_param=20&list_param=Life")
print(Model.model_validate(querydict_to_dict(query_dict, Model)))
print(QueryModel.model_validate(query_dict))
```
> **Note:** Don't forget to Setup the Django Project.
### Django Rest Framework
- `pyngo.drf_error_details()` will propagate any errors from Pydantic.
```py
from pydantic import BaseModel, ValidationError
from pyngo import drf_error_details
class Model(BaseModel):
foo: int
bar: str
data = {"foo": "Cat"}
try:
Model.model_validate(data)
except ValidationError as e:
print(drf_error_details(e))
```
Errors descend into nested fields:
```py
from typing import List
from pydantic import BaseModel, ValidationError
from pyngo import drf_error_details
class Framework(BaseModel):
frm_id: int
class Language(BaseModel):
framework: List[Framework]
data = {"framework": [{"frm_id": "not_a_number"}, {}]}
expected_details = {
"framework": {
"0": {"frm_id": ["value is not a valid integer"]},
"1": {"frm_id": ["field required"]},
}
}
try:
Language.model_validate(data)
except ValidationError as e:
print(drf_error_details(e))
```
## Development ๐ง
### Setup environment ๐ฆ
You should create a virtual environment and activate it:
```bash
python -m venv venv/
```
```bash
source venv/bin/activate
```
And then install the development dependencies:
```bash
# Install dependencies
pip install -e .[test,lint]
```
### Run tests ๐
You can run all the tests with:
```bash
bash scripts/test.sh
```
> Note: You can also generate a coverage report with:
```bash
bash scripts/test_html.sh
```
### Format the code ๐
Execute the following command to apply `pre-commit` formatting:
```bash
bash scripts/format.sh
```
Execute the following command to apply `mypy` type checking:
```bash
bash scripts/lint.sh
```
## License ๐ป
This project is licensed under the terms of the MIT license.
Raw data
{
"_id": null,
"home_page": null,
"name": "pyngo",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "django, django-rest-framework, pydantic, pydantic-django, pydantic-models",
"author": null,
"author_email": "Yasser Tahiri <hello@yezz.me>",
"download_url": "https://files.pythonhosted.org/packages/3b/25/8bd0b54fa8508b2d171a01d857e5f9a254e71ad2eae24fcc61e9aba867ac/pyngo-2.3.0.tar.gz",
"platform": null,
"description": "# Pyngo :snake:\n\n<p align=\"center\">\n <em>Utils to help integrate pydantic into Django projects</em>\n</p>\n\n<p align=\"center\">\n<a href=\"https://github.com/yezz123/pyngo/actions/workflows/ci.yml\" target=\"_blank\">\n <img src=\"https://github.com/yezz123/pyngo/actions/workflows/ci.yml/badge.svg\" alt=\"Test\">\n</a>\n<a href=\"https://codecov.io/gh/yezz123/pyngo\">\n <img src=\"https://codecov.io/gh/yezz123/pyngo/branch/main/graph/badge.svg\"/>\n</a>\n<a href=\"https://pypi.org/project/pyngo\" target=\"_blank\">\n <img src=\"https://img.shields.io/pypi/v/pyngo?color=%2334D058&label=pypi%20package\" alt=\"Package version\">\n</a>\n<a href=\"https://pypi.org/project/pyngo\" target=\"_blank\">\n <img src=\"https://img.shields.io/pypi/pyversions/pyngo.svg?color=%2334D058\" alt=\"Supported Python versions\">\n</a>\n<a href=\"https://pepy.tech/project/pyngo\" target=\"_blank\">\n <img src=\"https://static.pepy.tech/badge/pyngo\" alt=\"Test\">\n</a>\n</p>\n\n---\n\n## Installation\n\nYou can add pyngo in a few easy steps. First of all, install the dependency:\n\n```sh\n$ pip install pyngo\n\n---> 100%\n\nSuccessfully installed pyngo\n```\n\n---\n\n## Features \ud83c\udf89\n\n- Using Pydantic to Build your Models in Django Project.\n- Using `OpenAPI` utilities to build params from a basic model.\n- using `QueryDictModel` to build `Pydantic` models from a `QueryDict` object.\n- propagate any errors from Pydantic in Django Rest Framework.\n- Tested in Python 3.10 and up.\n\n## Examples \ud83d\udcda\n\n### OpenAPI\n\n- `pyngo.openapi_params()` can build params from a basic model\n\n```py\nfrom pydantic import BaseModel\nfrom pyngo import openapi_params\n\nclass Model(BaseModel):\n bingo: int\n\nprint(openapi_params(Model))\n```\n\n- `pyngo.ParameterDict.required` is set according to the type of the variable\n\n```py\nfrom typing import Optional\nfrom pydantic import BaseModel\nfrom pyngo import openapi_params\n\nclass Model(BaseModel):\n required_param: int\n optional_param: Optional[int]\n\nprint(openapi_params(Model))\n```\n\nOther fields can be set through the field\u2019s info:\n\n```py\nfrom pydantic import BaseModel, Field\nfrom pyngo import openapi_params\n\nclass WithDescription(BaseModel):\n described_param: str = Field(\n description=\"Hello World Use Me!\"\n )\n\nclass InPath(BaseModel):\n path_param: str = Field(location=\"path\")\n\nclass WithDeprecated(BaseModel):\n deprecated_field: bool = Field(deprecated=True)\n\nclass WithNoAllowEmpty(BaseModel):\n can_be_empty: bool = Field(allowEmptyValue=False)\n\nprint(openapi_params(WithDescription)[0][\"description\"])\nprint(openapi_params(InPath)[0][\"in\"])\nprint(openapi_params(WithDeprecated)[0][\"deprecated\"])\nprint(openapi_params(WithNoAllowEmpty)[0][\"allowEmptyValue\"])\n```\n\n### Django\n\n- `pyngo.querydict_to_dict()` and `pyngo.QueryDictModel` are conveniences for building a `pydantic.BaseModel` from a `django.QueryDict`.\n\n```py\nfrom typing import List\nfrom django.http import QueryDict\nfrom pydantic import BaseModel\nfrom pyngo import QueryDictModel, querydict_to_dict\n\nclass Model(BaseModel):\n single_param: int\n list_param: List[str]\n\nclass QueryModel(QueryDictModel):\n single_param: int\n list_param: List[str]\n\nquery_dict = QueryDict(\"single_param=20&list_param=Life\")\n\nprint(Model.model_validate(querydict_to_dict(query_dict, Model)))\nprint(QueryModel.model_validate(query_dict))\n```\n\n> **Note:** Don't forget to Setup the Django Project.\n\n### Django Rest Framework\n\n- `pyngo.drf_error_details()` will propagate any errors from Pydantic.\n\n```py\nfrom pydantic import BaseModel, ValidationError\nfrom pyngo import drf_error_details\n\nclass Model(BaseModel):\n foo: int\n bar: str\n\ndata = {\"foo\": \"Cat\"}\n\ntry:\n Model.model_validate(data)\nexcept ValidationError as e:\n print(drf_error_details(e))\n```\n\nErrors descend into nested fields:\n\n```py\nfrom typing import List\nfrom pydantic import BaseModel, ValidationError\nfrom pyngo import drf_error_details\n\nclass Framework(BaseModel):\n frm_id: int\n\nclass Language(BaseModel):\n framework: List[Framework]\n\ndata = {\"framework\": [{\"frm_id\": \"not_a_number\"}, {}]}\nexpected_details = {\n \"framework\": {\n \"0\": {\"frm_id\": [\"value is not a valid integer\"]},\n \"1\": {\"frm_id\": [\"field required\"]},\n }\n}\n\ntry:\n Language.model_validate(data)\nexcept ValidationError as e:\n print(drf_error_details(e))\n```\n\n## Development \ud83d\udea7\n\n### Setup environment \ud83d\udce6\n\nYou should create a virtual environment and activate it:\n\n```bash\npython -m venv venv/\n```\n\n```bash\nsource venv/bin/activate\n```\n\nAnd then install the development dependencies:\n\n```bash\n# Install dependencies\npip install -e .[test,lint]\n```\n\n### Run tests \ud83c\udf1d\n\nYou can run all the tests with:\n\n```bash\nbash scripts/test.sh\n```\n\n> Note: You can also generate a coverage report with:\n\n```bash\nbash scripts/test_html.sh\n```\n\n### Format the code \ud83c\udf42\n\nExecute the following command to apply `pre-commit` formatting:\n\n```bash\nbash scripts/format.sh\n```\n\nExecute the following command to apply `mypy` type checking:\n\n```bash\nbash scripts/lint.sh\n```\n\n## License \ud83c\udf7b\n\nThis project is licensed under the terms of the MIT license.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Pydantic Package for Adding Models into a Django or Django Rest Framework Project \u2728",
"version": "2.3.0",
"project_urls": {
"Funding": "https://github.com/sponsors/yezz123",
"Homepage": "https://github.com/yezz123/pyngo"
},
"split_keywords": [
"django",
" django-rest-framework",
" pydantic",
" pydantic-django",
" pydantic-models"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1892c7e9fe90be5eb6f5bb8754123299048f35a8e6628fadca73b39883e63412",
"md5": "7dbeab92efd99fe7b4ff6672c2280446",
"sha256": "559cf89e52235546ff747ee4267ece2dd9a9f99471e66ef078ad53902ac161dc"
},
"downloads": -1,
"filename": "pyngo-2.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7dbeab92efd99fe7b4ff6672c2280446",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 7799,
"upload_time": "2024-12-01T13:34:00",
"upload_time_iso_8601": "2024-12-01T13:34:00.568072Z",
"url": "https://files.pythonhosted.org/packages/18/92/c7e9fe90be5eb6f5bb8754123299048f35a8e6628fadca73b39883e63412/pyngo-2.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3b258bd0b54fa8508b2d171a01d857e5f9a254e71ad2eae24fcc61e9aba867ac",
"md5": "03753314a5ebf1eed1963c9e47e81ddd",
"sha256": "ff0ebc42a524dc5454763702a1941f977e3169ec7379d1cfff9077c4dafb40b5"
},
"downloads": -1,
"filename": "pyngo-2.3.0.tar.gz",
"has_sig": false,
"md5_digest": "03753314a5ebf1eed1963c9e47e81ddd",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 12049,
"upload_time": "2024-12-01T13:34:18",
"upload_time_iso_8601": "2024-12-01T13:34:18.715016Z",
"url": "https://files.pythonhosted.org/packages/3b/25/8bd0b54fa8508b2d171a01d857e5f9a254e71ad2eae24fcc61e9aba867ac/pyngo-2.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-01 13:34:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sponsors",
"github_project": "yezz123",
"github_not_found": true,
"lcname": "pyngo"
}