py-dto


Namepy-dto JSON
Version 1.1.0 PyPI version JSON
download
home_pagehttps://github.com/rogervila/py_dto
Summarydata transfer objects with Python
upload_time2024-06-20 18:33:14
maintainerNone
docs_urlNone
authorRoger Vilà
requires_pythonNone
licenseMIT
keywords python data transfer objects data transfer objects python dto dto
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # Python DTO

<p align="center"><img height="200" alt="rogervila/py_dto" src="https://rogervila.es/static/img/py_dto.png" /></p>

[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=rogervila_py_dto&metric=coverage)](https://sonarcloud.io/dashboard?id=rogervila_py_dto)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=rogervila_py_dto&metric=alert_status)](https://sonarcloud.io/dashboard?id=rogervila_py_dto)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=rogervila_py_dto&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=rogervila_py_dto)
[![Downloads](https://img.shields.io/pypi/dm/py-dto.svg)](https://github.com/rogervila/py_dto)

Data Transfer Objects (DTO) with Python.


## Install

```sh
pip install py_dto
```

## Usage

Define the object properties with types defined, then pass a `dict` with data.

### Basic example

For type hinting

```py
from py_dto import DTO

# This DTO will be used as a type definition
class UserProfile(DTO):
    avatar: str

# The DTO with the properties defined
class User(DTO):
    profile: UserProfile
    name: str
    email: str
    age: int
    tags: list[str]

# Create the DTO instance
user = User({
    'profile': UserProfile({'avatar': 'https://i.pravatar.cc/300'}),
    'name': 'John',
    'email': 'john@example.com',
    'age': 42,
    'tags': ['developer', 'python']
})

print(user.name) # 'John'
print(user.profile.avatar) # https://i.pravatar.cc/300
```

### The `Any` type

Even DTO are supposed to specify data types, you can use the `Any` type to accept literally any type for a property:

```py
from py_dto import DTO
from typing import Any

# The DTO accepts "any" type of data for the "name" property
class User(DTO):
    name: Any

# Create the DTO instance
user = User({
    'name': 'John',
})

print(user.name) # 'John'

user = User({
    'name': 123,
})

print(user.name) # 123
```

### Dealing with `None`

Imagine you are retrieving data from a database table where a column is empty for some records.

By using python's `Optional` type on a specific property, the DTO will not raise an exception if a `None` value is set.

```py
from py_dto import DTO
from typing import Optional

# The DTO "name" property can be a str or a None value
class User(DTO):
    name: Optional[str]

# Create the DTO instance with a "str"
user = User({
    'name': 'John',
})

print(user.name) # 'John'

# Create the DTO instance with a "None"
user = User({
    'name': None,
})

print(user.name) # None

# Any other type will raise an exception
try:
    user = User({
        'name': 123,
    })
except:
    print('123 does not have a "str" nor a "None" type')
```

## License

This project is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

Original idea comes from [spatie/data-transfer-object](https://github.com/spatie/data-transfer-object) package for PHP.

<div>Icons made by <a href="https://www.flaticon.com/authors/pixel-perfect" title="Pixel perfect">Pixel perfect</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a></div>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/rogervila/py_dto",
    "name": "py-dto",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "python data transfer objects, data transfer objects, python dto, dto",
    "author": "Roger Vil\u00e0",
    "author_email": "rogervila@me.com",
    "download_url": "https://files.pythonhosted.org/packages/85/8e/43151b7a23e997b15123dc5f849fcb3fec20eabb7ada0c17b4b9fa5bce88/py_dto-1.1.0.tar.gz",
    "platform": null,
    "description": "# Python DTO\n\n<p align=\"center\"><img height=\"200\" alt=\"rogervila/py_dto\" src=\"https://rogervila.es/static/img/py_dto.png\" /></p>\n\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=rogervila_py_dto&metric=coverage)](https://sonarcloud.io/dashboard?id=rogervila_py_dto)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=rogervila_py_dto&metric=alert_status)](https://sonarcloud.io/dashboard?id=rogervila_py_dto)\n[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=rogervila_py_dto&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=rogervila_py_dto)\n[![Downloads](https://img.shields.io/pypi/dm/py-dto.svg)](https://github.com/rogervila/py_dto)\n\nData Transfer Objects (DTO) with Python.\n\n\n## Install\n\n```sh\npip install py_dto\n```\n\n## Usage\n\nDefine the object properties with types defined, then pass a `dict` with data.\n\n### Basic example\n\nFor type hinting\n\n```py\nfrom py_dto import DTO\n\n# This DTO will be used as a type definition\nclass UserProfile(DTO):\n    avatar: str\n\n# The DTO with the properties defined\nclass User(DTO):\n    profile: UserProfile\n    name: str\n    email: str\n    age: int\n    tags: list[str]\n\n# Create the DTO instance\nuser = User({\n    'profile': UserProfile({'avatar': 'https://i.pravatar.cc/300'}),\n    'name': 'John',\n    'email': 'john@example.com',\n    'age': 42,\n    'tags': ['developer', 'python']\n})\n\nprint(user.name) # 'John'\nprint(user.profile.avatar) # https://i.pravatar.cc/300\n```\n\n### The `Any` type\n\nEven DTO are supposed to specify data types, you can use the `Any` type to accept literally any type for a property:\n\n```py\nfrom py_dto import DTO\nfrom typing import Any\n\n# The DTO accepts \"any\" type of data for the \"name\" property\nclass User(DTO):\n    name: Any\n\n# Create the DTO instance\nuser = User({\n    'name': 'John',\n})\n\nprint(user.name) # 'John'\n\nuser = User({\n    'name': 123,\n})\n\nprint(user.name) # 123\n```\n\n### Dealing with `None`\n\nImagine you are retrieving data from a database table where a column is empty for some records.\n\nBy using python's `Optional` type on a specific property, the DTO will not raise an exception if a `None` value is set.\n\n```py\nfrom py_dto import DTO\nfrom typing import Optional\n\n# The DTO \"name\" property can be a str or a None value\nclass User(DTO):\n    name: Optional[str]\n\n# Create the DTO instance with a \"str\"\nuser = User({\n    'name': 'John',\n})\n\nprint(user.name) # 'John'\n\n# Create the DTO instance with a \"None\"\nuser = User({\n    'name': None,\n})\n\nprint(user.name) # None\n\n# Any other type will raise an exception\ntry:\n    user = User({\n        'name': 123,\n    })\nexcept:\n    print('123 does not have a \"str\" nor a \"None\" type')\n```\n\n## License\n\nThis project is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).\n\nOriginal idea comes from [spatie/data-transfer-object](https://github.com/spatie/data-transfer-object) package for PHP.\n\n<div>Icons made by <a href=\"https://www.flaticon.com/authors/pixel-perfect\" title=\"Pixel perfect\">Pixel perfect</a> from <a href=\"https://www.flaticon.com/\" title=\"Flaticon\">www.flaticon.com</a></div>\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "data transfer objects with Python",
    "version": "1.1.0",
    "project_urls": {
        "Download": "https://github.com/rogervila/py_dto/archive/1.1.0.tar.gz",
        "Homepage": "https://github.com/rogervila/py_dto"
    },
    "split_keywords": [
        "python data transfer objects",
        " data transfer objects",
        " python dto",
        " dto"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "46167056a7bda56e4bc216b29dff939366f6389572bb8fdd292661e30d967302",
                "md5": "f45b96deaf08b46841d639160b86f38e",
                "sha256": "350d8fae8ae3cb6c4d495a7495ad154d6c71c7ddb96a0a3fd52f2e7d4e5cfb4d"
            },
            "downloads": -1,
            "filename": "py_dto-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f45b96deaf08b46841d639160b86f38e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 3862,
            "upload_time": "2024-06-20T18:33:13",
            "upload_time_iso_8601": "2024-06-20T18:33:13.028401Z",
            "url": "https://files.pythonhosted.org/packages/46/16/7056a7bda56e4bc216b29dff939366f6389572bb8fdd292661e30d967302/py_dto-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "858e43151b7a23e997b15123dc5f849fcb3fec20eabb7ada0c17b4b9fa5bce88",
                "md5": "c0885c972eba095044ae4f5d7e26c569",
                "sha256": "790238f5f28c85c66259875ff2619746e55880a39f521628eafd42e4f292e4b0"
            },
            "downloads": -1,
            "filename": "py_dto-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c0885c972eba095044ae4f5d7e26c569",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4918,
            "upload_time": "2024-06-20T18:33:14",
            "upload_time_iso_8601": "2024-06-20T18:33:14.585628Z",
            "url": "https://files.pythonhosted.org/packages/85/8e/43151b7a23e997b15123dc5f849fcb3fec20eabb7ada0c17b4b9fa5bce88/py_dto-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-20 18:33:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "rogervila",
    "github_project": "py_dto",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "py-dto"
}
        
Elapsed time: 0.26450s