Skip to main content

Struct2Model

Create serializable rules for your python models.

Usage

import datetime
from struct2model.main import transform_value


VALUE = {
    "id": 1323,
    "name": "Sergey",
    "email": "serger.g@gmail.com",
    "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

Release files for struct2model 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for struct2model 0.1.0
File Size Uploaded
struct2model-0.1.0.tar.gz 2.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for struct2model 0.1.0
File Interpreter ABI Platform
struct2model-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.3 kB

Release files / struct2model-0.1.0.tar.gz

Download URL struct2model-0.1.0.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
085b0ff3d38541073673e09b2650e09c5373a7da205b164ad22f2702491a035a
BLAKE2b-256 checksum
How to use checksums
bc32c4091fc26a94ac8bbc6e606ad79130c0789805e95bcabb3193311beaa74f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.4 Linux/5.15.133.1-microsoft-standard-WSL2

Release files / struct2model-0.1.0-py3-none-any.whl

Download URL struct2model-0.1.0-py3-none-any.whl
Size 2.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b91ec9ed19dd1bd3b0e9efafbc8fb1eb807266ba31ac4231b4343b5079f0d69a
BLAKE2b-256 checksum
How to use checksums
17ddb19eaac8939034cfff62338f52d298a5c89b6ca8cc0bba235a4775fa15f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.4 Linux/5.15.133.1-microsoft-standard-WSL2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page