Skip to main content

functionality for mapping one model into another

Project description

PyMapMe

PyMapMe is a tiny library for mapping pydantic models, it might be useful when you have some model and want to represent it using different structure.

Supported functionality:

  • nested mapping
  • using helper functions with the access to InModel (see _get_full_name)
  • using context data, when you need to extend your model with some extra data

Getting started

from typing import Any

from pydantic import BaseModel, Field
from pymapme.models.mapping import MappingModel


class Person(BaseModel):
    name: str
    surname: str


class Profile(BaseModel):
    nickname: str
    person: Person


class User(MappingModel):
    nickname: str = Field(source='nickname')
    first_name: str = Field(source='person.name')
    surname: str = Field(source='person.surname')
    full_name: str = Field(source_func='_get_full_name')
    age: int = Field(source_func='_get_age_from_context')

    @staticmethod
    def _get_full_name(model: Profile, default: Any):
        return model.person.name + ' ' + model.person.surname

    @staticmethod
    def _get_age_from_context(model: Profile, default: Any, age: int):
        return age


extra = {'age': 35}
profile = Profile(nickname='baobab', person=Person(name='John', surname='Smith'))
user = User.build_from_model(profile, context=extra)
print(user.dict())
# {'nickname': 'baobab', 'first_name': 'John', 'surname': 'Smith', 'full_name': 'John Smith', 'age': 35}

Development

Run tests:

make run-tests

Run static analysis:

make run-mypy

Build package:

make build-package

Installation

It is recommended to use Poetry:

poetry add pymapme

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

pymapme-0.0.2.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

pymapme-0.0.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file pymapme-0.0.2.tar.gz.

File metadata

  • Download URL: pymapme-0.0.2.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.8 Linux/5.15.71-1-lts

File hashes

Hashes for pymapme-0.0.2.tar.gz
Algorithm Hash digest
SHA256 70bd0cdcd0d64ef502e0920d6c46f2af136f247af9b7c4112f04113d984c8f66
MD5 16de54d39848d1e21bcef4f34a93f876
BLAKE2b-256 40ce898b8e87be2932e1e9e70467e3870c21c657d2d0aac9d4180515811c0fbb

See more details on using hashes here.

File details

Details for the file pymapme-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pymapme-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.8 Linux/5.15.71-1-lts

File hashes

Hashes for pymapme-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 970d99b07ddeea2bcec28514f3d370115940b8a5987e6f700ebba77e30adf374
MD5 ea1d2ad77fdab52d77e1968abdbee171
BLAKE2b-256 816a94aaa5af181e65c14ab642f6a7bc7cf696485613c190809038ad818ac5b2

See more details on using hashes here.

Supported by

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