Giter Site home page Giter Site logo

struct2model's Introduction

Struct2Model

Create serializable rules for your python models.

Usage

import datetime
from struct2model.main import transform_value


VALUE = {
    "id": 1323,
    "name": "Sergey",
    "email": "[email protected]",
    "birth": "1985-05-05",
    "last_login": "2015-05-05 12:00:00",
    "blocked": "SIM",
    "created_at": "2015-05-05T12:00:00.000Z",
    "updated_at": "2015-05-05T12:00:00.000+03:00",
    "address": {
        "id": 1,
        "city": "Moscow",
        "street": "Lenina",
        "house": "1",
        "state": "1",
        "zip": "123456",
    },
    "phones": [
        {"id": 1, "number": "1234567890"},
        {"id": 2, "number": "0987654321"},
    ],
}

RAPAZ_TO_PERSON_MAPPING_RULES = {
    "id": "id",
    "nome": "name",
    "data_nascimento": "birth|date,%Y-%m-%d",
    "endereco": {
        "rua": "address.street",
        "numero": "address.house",
        "bairro": "address.state",
        "cidade": "address.city",
        "estado": "address.state",
        "cep": "address.zip",
    },
    "ultima_vez_logado": "last_login|datetime,%Y-%m-%d %H:%M:%S",
    "data_cadastro": "created_at|datetime,%Y-%m-%dT%H:%M:%S.%fZ",
    "telefones": {
        "__for__": "phones",
        "__as__": "phone",
        "numero": "phone.number",
    },
    "bloqueado": "blocked|bool,SIM",
}

result = transform_value(VALUE, RAPAZ_TO_PERSON_MAPPING_RULES)

assert result["data_nascimento"] == datetime.date(1985, 5, 5)
assert result["ultima_vez_logado"] == datetime.datetime(2015, 5, 5, 12, 0)
assert result["data_cadastro"] == datetime.datetime(2015, 5, 5, 12, 0)
assert result["telefones"][0]["numero"] == "1234567890"
assert result["telefones"][1]["numero"] == "0987654321"
assert result["bloqueado"] == True
assert result["endereco"]["rua"] == "Lenina"
assert result["endereco"]["numero"] == "1"
assert result["endereco"]["bairro"] == "1"
assert result["endereco"]["cidade"] == "Moscow"
assert result["endereco"]["estado"] == "1"
assert result["endereco"]["cep"] == "123456"

assert result["id"] == 1323

Installation

pip install struct2model

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

License

MIT License

struct2model's People

Contributors

luscasleo avatar

Watchers

 avatar

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.