data transfer objects with Python
Project description
Python DTO
Data Transfer Objects (DTO) with Python.
Install
pip install py_dto
Usage
Define the object properties with types defined, then pass a dict with data.
Basic example
For type hinting
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:
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.
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.
Original idea comes from spatie/data-transfer-object package for PHP.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_dto-1.1.0.tar.gz.
File metadata
- Download URL: py_dto-1.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
790238f5f28c85c66259875ff2619746e55880a39f521628eafd42e4f292e4b0
|
|
| MD5 |
c0885c972eba095044ae4f5d7e26c569
|
|
| BLAKE2b-256 |
858e43151b7a23e997b15123dc5f849fcb3fec20eabb7ada0c17b4b9fa5bce88
|
File details
Details for the file py_dto-1.1.0-py3-none-any.whl.
File metadata
- Download URL: py_dto-1.1.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
350d8fae8ae3cb6c4d495a7495ad154d6c71c7ddb96a0a3fd52f2e7d4e5cfb4d
|
|
| MD5 |
f45b96deaf08b46841d639160b86f38e
|
|
| BLAKE2b-256 |
46167056a7bda56e4bc216b29dff939366f6389572bb8fdd292661e30d967302
|