Skip to main content

data transfer objects with Python

Project description

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

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

py_dto-1.2.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_dto-1.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file py_dto-1.2.0.tar.gz.

File metadata

  • Download URL: py_dto-1.2.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for py_dto-1.2.0.tar.gz
Algorithm Hash digest
SHA256 c8c04e46c85f1c1aa3320e29674ef37aeaf3f148211173448972964759e0208f
MD5 07049b5e6d93787d5eb8356b47404438
BLAKE2b-256 a413423d809af2db17a13211a381e1ec9da4203149763bb0439c8cf469dd7261

See more details on using hashes here.

File details

Details for the file py_dto-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: py_dto-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for py_dto-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d04d36ae642c2cab2fab38deae28ede96f95c6a287fb2a7215ca51f9562d5fac
MD5 9bf4a10ff9efcff658fe70dc97af869e
BLAKE2b-256 0fe6a754bd587efe9866b08807b47d7e6b8db262815420a40253f7fcdddaef79

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page