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.3.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pymapme-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 b2627927b7e0f7f18d114d11abf3610fb6ef9832d7282ccc1c4db3393707ef9f
MD5 e51f22f713f8b30937b7d88b66d1dfd3
BLAKE2b-256 8863fbb6f9371f44a659680a2c6359694e5a46c6b65c1852da75f705b7cf2379

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pymapme-0.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dac1bdab8dcdec164f7d141ad40f6178e5a3ca76f47ab4662795fe494d6d538a
MD5 71336f355b072b24be006e5b23ca8987
BLAKE2b-256 b5352493ad795bd45ef4589cf8ad2782ccce5cfed626047b45fcbe349fa4c3b6

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