Skip to main content

Map data between pydantic models

Project description

pydamapper

Map data between pydantic models.

Python 3.13+ Pydantic v2 uv Ruff Checked with mypy license Contributor Covenant

Real applications have real data, and real data nests. Objects inside of objects inside of lists of objects.

Mahmoud Hashemi

Mapping real data should be as easy as declaring their schemas, ergo, pydamapper.

🚀 Highlights

  • Automatic mapping - Just using the field names. Zero boilerplate. Reuse your pydantic models.
  • Complex structures - Map to new nested estructures or lists of new nested structures.
  • Fault tolerant - If it cannot map all the data, it'll return what it found with readable errors.
  • Enjoy Pydantic's features - Built-in type checking using Pydantic's validation.

🛠 Usage

from pydantic import BaseModel
from pydamapper import map_models

# TBD

Build new models

# TBD

Build lists of existing or new models

# TBD

What is pydamapper?

The Python Data Mapper is a data mapping tool.

It allows you to easily map data from a data structure (for example, a webhook's payload) to another data structure (for example, an API endpoint payload), using Pydantic validation.

It was created with the purpose of facilitating integration between APIs, allowing you to just define input and output schemas to translate one API to another API.

Why?

  • Lazyness: integrating two simple APIs should be as easy as defining their schemas (or asking ChatGPT to do it for you).
  • DRY: Don't repeat yourself. If you have the model schema, why bother with something else?

Other solutions?

pymapme

This is the most similar solution. However, it offers a different experience from what I was looking for.

Besides, it doesn't support:

  • Match with the name only
  • Partial returns
  • Detailed errors report

glom

It's not built with the goal of mapping and validating data between pydantic models, but it can be used for it. You can check and example in the docs.

Check a video about the purpose of this tool here. You can read more about glom in the creator's website.

Maybe using glom in this package could be a good idea 🤷‍♂️.

Just direct assignment

Certainly, the most straightforward solution. However:

  • Too many lines of code for larger or complex models
  • Inflexible
  • Boring

Why not just use AI to do the mapping for me and just copy and paste?

Well, I hope AI will recommend you to pip install pydamapper in the future.

🤝 Contribute

We welcome contributions! Here's how to set up:

# 1. Clone the repo
git  clone  https://github.com/julioccorderoc/pydamapper.git

# 2. Install dev environment
make  setup  # Installs pre-commit hooks, testing tools, etc

# 3. Run tests
make  test  # Runs pytest with coverage

# 4. Run static checks
make  check  # Runs code quality tools

# 5. Clean up
make  clean  # Cleans up generated files

📮 Need Help?

Open an issue or DM me on LinkedIn: @julioccorderoc.

📚 Documentation

TBD

Full docs available here.

📄 License

MIT License - Free for commercial use

Disclaimer

  • This package is still in development.
  • I created this package as a learning project, so be aware.

🙌 Credits

  • Created by me.
  • Build on top of the awesome Pydantic community.

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

pydamapper-0.1.0.tar.gz (50.4 kB view details)

Uploaded Source

Built Distribution

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

pydamapper-0.1.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file pydamapper-0.1.0.tar.gz.

File metadata

  • Download URL: pydamapper-0.1.0.tar.gz
  • Upload date:
  • Size: 50.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.8

File hashes

Hashes for pydamapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b952b26463eeaf4a9f6cf8befc850e6509776d4ee6d3cd5eb61c84d1e66cce15
MD5 9578420e96f2773a1cc567ac6c660701
BLAKE2b-256 97a85577b59e1ae7e18d7de6d1fa3c03589936e6c6a405166c7e487fb512d00b

See more details on using hashes here.

File details

Details for the file pydamapper-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydamapper-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.8

File hashes

Hashes for pydamapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8de70bf8a5f9d9ea3cd514de3e206a5e997edd028e4f21a2ba22e9d801104b80
MD5 95befccfd3edbd53d2b3d054c985de4a
BLAKE2b-256 dcf6a52d1e171fb6f1df053b338972f5ab0c4623302d635bd5410fa92a67f641

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