Skip to main content

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. str to int or float).
  • Error Handling: Custom MappingError exception with informative messages for missing fields or failed conversions.
  • Logging: Configured via the logging module 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 runs pytest automatically on PRs and commits to the main branch.
  • Testing: Using pytest for all modules (tests/test_mapper.py). To run locally, simply run pytest.
  • Packaging: Standardized layout with pyproject.toml, setup.py, and src/datamapperx for safe package resolution and module exporting.
  • Logging Control: Uses standard Python logging. This allows granular verbosity control dynamically in your production systems.

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

datamapperx-0.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

datamapperx-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

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

Hashes for datamapperx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 182e3548ce65142e6c946f86caada74fd192e0cc259b480692e922e1f2bbf1f9
MD5 5bdac48f6ffb1db348ec25793bea4bc2
BLAKE2b-256 b64262bfa50d9b28b31c1023191b290c5f2cca9c65337f27d2b2605ab71aa57f

See more details on using hashes here.

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

Hashes for datamapperx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 526ce24f29097f7958d7bb17b6c775c096d785aedfed4508abf4aaa4c3ada1d9
MD5 2872280d32a30320664ad1693a456935
BLAKE2b-256 b2eee6a80f64c9bf11ee2ac6be9f5659b105ae8c3995218011da33360ab1afb7

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