A reusable Python library that maps dictionary data to Python objects.
Project description
datamapperx
A complete, production-ready Python package for mapping dictionary data to Python objects (classes), inspired by AutoMapper in .NET.
Features
- Basic Mapping: Map dictionaries to standard Python objects or dataclasses.
- Custom Mapping: Rename source fields to match target attributes based on custom dictionary mappings.
- Ignore Fields: Exclude specific fields from mapping during object creation.
- Nested Mapping: Supports mapping nested dictionaries to nested Python class structures automatically using type hints.
- Type Conversion: Converts data types automatically based on type hints (e.g.
strtointorfloat). - Error Handling: Custom
MappingErrorexception with informative messages for missing fields or failed conversions. - Logging: Configured via the
loggingmodule to trace mapping execution procedures.
Installation
You can install datamapperx locally:
pip install -e .
Or build and publish it:
python -m build
twine upload dist/*
Usage Example
from datamapperx import map_to
class UserProfile:
def __init__(self, username: str, age: int, is_active: bool = True):
self.username = username
self.age = age
self.is_active = is_active
# 1. Basic Mapping
data = {"username": "john_doe", "age": 30}
user = map_to(UserProfile, data)
print(user.username, user.age)
# 2. Custom Mapping
data = {"user_name": "jane", "user_age": 25}
custom_map = {"user_name": "username", "user_age": "age"}
user = map_to(UserProfile, data, custom_map=custom_map)
# 3. Ignoring Fields
data = {"username": "admin", "age": 40, "is_active": False}
# is_active gets the default (True) instead of False because it's ignored
user = map_to(UserProfile, data, ignore=["is_active"])
# 4. Nested Mapping
class Address:
def __init__(self, city: str):
self.city = city
class UserWithAddress:
def __init__(self, profile: UserProfile, address: Address):
self.profile = profile
self.address = address
data = {
"profile": {"username": "bob", "age": 35},
"address": {"city": "New York"}
}
user_nest = map_to(UserWithAddress, data)
print(user_nest.address.city) # "New York"
MLOps & CI/CD
datamapperx follows software engineering and MLOps best practices:
- CI/CD Pipeline: GitHub Actions are configured in
.github/workflows/ci.yml. It runspytestautomatically on PRs and commits to themainbranch. - Testing: Using
pytestfor all modules (tests/test_mapper.py). To run locally, simply runpytest. - Packaging: Standardized layout with
pyproject.toml,setup.py, andsrc/datamapperxfor safe package resolution and module exporting. - Logging Control: Uses standard Python
logging. This allows granular verbosity control dynamically in your production systems.
Project details
Release history Release notifications | RSS feed
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 datamapperx-0.1.0.tar.gz.
File metadata
- Download URL: datamapperx-0.1.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
182e3548ce65142e6c946f86caada74fd192e0cc259b480692e922e1f2bbf1f9
|
|
| MD5 |
5bdac48f6ffb1db348ec25793bea4bc2
|
|
| BLAKE2b-256 |
b64262bfa50d9b28b31c1023191b290c5f2cca9c65337f27d2b2605ab71aa57f
|
File details
Details for the file datamapperx-0.1.0-py3-none-any.whl.
File metadata
- Download URL: datamapperx-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
526ce24f29097f7958d7bb17b6c775c096d785aedfed4508abf4aaa4c3ada1d9
|
|
| MD5 |
2872280d32a30320664ad1693a456935
|
|
| BLAKE2b-256 |
b2eee6a80f64c9bf11ee2ac6be9f5659b105ae8c3995218011da33360ab1afb7
|