Python DTO Mappers
Reusable DTO mapping patterns with type safety and auto-mapping functionality.
Installation
pip install python-dto-mappers
Features
- BaseMapper Protocol: Type-safe mapper interface
- Auto-Mapping: Automatic field mapping with @auto_map decorator
- Field Transformations: Common transformations (snake_case ↔ camelCase, datetime ↔ ISO)
- Partial Updates: Extract changed fields for PATCH operations
- Chained Mapping: ORM → Domain → DTO transformations
Usage
Basic Mapper
from pydantic import BaseModel
from python_dto_mappers import BaseMapper
class UserEntity(BaseModel):
id: int
name: str
email: str
class UserDTO(BaseModel):
id: int
name: str
email: str
class UserMapper(BaseMapper[UserEntity, UserDTO]):
def map(self, source: UserEntity) -> UserDTO:
return UserDTO(
id=source.id,
name=source.name,
email=source.email
)
Auto-Mapping
from python_dto_mappers import auto_map
@auto_map(UserEntity, UserDTO)
class UserAutoMapper:
def transform_name(self, source):
# Custom transformation for specific field
return source.name.upper()
mapper = UserAutoMapper()
dto = mapper.map(user_entity)
Field Transformations
from python_dto_mappers import snake_to_camel, map_datetime_to_iso
# Case conversion
camel = snake_to_camel("user_name") # "userName"
snake = camel_to_snake("userName") # "user_name"
# Datetime conversion
iso_str = map_datetime_to_iso(datetime.now())
dt = map_iso_to_datetime("2024-01-01T12:00:00")
Partial Updates (PATCH)
from python_dto_mappers import extract_changed_fields
original = User(id=1, name="John", email="john@example.com")
update = UserUpdateDTO(name="Jane", email="john@example.com")
changed = extract_changed_fields(original, update)
# {'name': 'Jane'} - only changed fields
Chained Mapping
from python_dto_mappers import chain_map
# ORM → Domain → DTO
dto = chain_map(
orm_user,
through=[DomainUser, UserDTO]
)
API Reference
Mapper[TSource, TTarget] (Protocol)
Type-safe mapper protocol.
BaseMapper[TSource, TTarget]
Base mapper class with type information.
@auto_map(source_type, target_type, exclude=None)
Auto-generate mapping for matching fields.
Parameters:
source_type: Source model typetarget_type: Target model typeexclude: Set of fields to exclude
@field_transform(field_name)
Mark method as field transformation.
Field Transformers
snake_to_camel(snake_str): snake_case → camelCasecamel_to_snake(camel_str): camelCase → snake_casemap_datetime_to_iso(dt): datetime → ISO stringmap_iso_to_datetime(iso_str): ISO string → datetimemap_nested_object(obj, mapper): Map nested object
Utils
extract_changed_fields(original, update_dto, exclude): Extract changed fieldschain_map(source, through): Chain multiple mappings
Dependencies
pydantic>=2.0.0typing-extensions>=4.0.0
License
MIT
Metadata
Release files for python-dto-mappers 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python_dto_mappers-0.1.0.tar.gz | 9.2 kB | Without provenance | Details for python_dto_mappers-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_dto_mappers-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for python_dto_mappers-0.1.0-py3-none-any.whl |
Total release size: 18.1 kB
Release files / python_dto_mappers-0.1.0.tar.gz
| Download URL | python_dto_mappers-0.1.0.tar.gz |
|---|---|
| Size | 9.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
162be7aa4d85ee6ee60f0aafa04458b40abc4f7e77376e514904f7b31a923a33
|
|
BLAKE2b-256 checksum How to use checksums |
b57d2b36aceb5ad3544a8d5c1cfaf93bbe04d248ff99c5b228ffc50b19c38ae4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Release files / python_dto_mappers-0.1.0-py3-none-any.whl
| Download URL | python_dto_mappers-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
29fda6b768042384a358f3316598294cfe32d3efe22db5d3017133837321d271
|
|
BLAKE2b-256 checksum How to use checksums |
e631ca2ee05d8113a38c8c6bed973f299053f3bc85bb1a195f754a4f287cda48
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Security for python-dto-mappers 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.