Giter Club home page Giter Club logo

pymapme's Introduction

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

pymapme's People

Contributors

funnydman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

pymapme's Issues

[Question]: Can you use PyMapMe for translating one model's fields names to another?

Perhaps not your original use case here, but I've built a FastAPI interface with pydantic and SQLAlchemy for a complex astronomy project.

One of the APIs I pull from is a lisp based interface that returns many short forms and ALL CAPS field names and such, many of which need to be "translated" into the actual model I'm goign to be using (which is more lower case, fully named, and snake cased. For returns from the model, I am trying to avoid a whack long set of statements like this in numerous places and for some API responses tht return a lot of fields.

    data = resp["result"]["PARAMETERS"]
     
    # Now we have the datalake file path and some calibrate fields we can insert image.
    fits_headers['pixel_scale'] = data["QUALITIES-INFO"]["PIXEL-SCALE"]
    fits_headers['gain'] = data["QUALITIES-INFO"]["GAIN"]

I notice it looks like PyMapMe's use case is more about re-purposing models than mapping fields, but was wondeirng if it could serve the same purpose. In the above, instance I have both a Images model and a Calibration model which reflect my underlying database tables, just nothing that represents the stuff coming back from the lisp API I call.

lemme know! Interesting project now I'm messing more with Python than Go and ruby...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.