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-unit-tests
Run static analysis:
make run-static-analysis
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.4.tar.gz
(15.1 kB
view details)
Built Distribution
pymapme-0.0.4-py3-none-any.whl
(15.9 kB
view details)
File details
Details for the file pymapme-0.0.4.tar.gz
.
File metadata
- Download URL: pymapme-0.0.4.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.14 CPython/3.10.8 Linux/5.15.78-1-lts
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39b36c7d0e9c334fd763deccdaf80e216ef534b48d2339d5e394350626862ca4 |
|
MD5 | 820c9da508aa0d29716fa305000b8e3b |
|
BLAKE2b-256 | 1e2ba50ec0bda3d256823dad6c1c1fad14fd31921a7e13196117c8b1eb4540fd |
File details
Details for the file pymapme-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: pymapme-0.0.4-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.78-1-lts
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e6a7bd6edddaeed95bdc3a339570a310927338cf6d248d98b0fba21daea3c13 |
|
MD5 | 36eb9721faef858df1b5dbbb19fbdd61 |
|
BLAKE2b-256 | bbc53f77a17b4a39d6d39b7bd53437b25576fd91365ec75d77243b26e11aae60 |