Skip to main content

Python DTO

rogervila/py_dto

PyPI version PyPI - Downloads

Data Transfer Objects (DTO) with Python.

Install

pip install py_dto

Requires Python 3.9 or newer. Tested on Python 3.9 through 3.14.

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.

Icons made by Pixel perfect from www.flaticon.com

Metadata

Release files for py-dto 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-dto 1.2.0
File Size Uploaded
py_dto-1.2.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-dto 1.2.0
File Interpreter ABI Platform
py_dto-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.5 kB

Release files / py_dto-1.2.0.tar.gz

Download URL py_dto-1.2.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c8c04e46c85f1c1aa3320e29674ef37aeaf3f148211173448972964759e0208f
BLAKE2b-256 checksum
How to use checksums
a413423d809af2db17a13211a381e1ec9da4203149763bb0439c8cf469dd7261
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / py_dto-1.2.0-py3-none-any.whl

Download URL py_dto-1.2.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d04d36ae642c2cab2fab38deae28ede96f95c6a287fb2a7215ca51f9562d5fac
BLAKE2b-256 checksum
How to use checksums
0fe6a754bd587efe9866b08807b47d7e6b8db262815420a40253f7fcdddaef79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page