Skip to main content

No project description provided

Project description

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

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

struct2model-0.1.0.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

struct2model-0.1.0-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

Details for the file struct2model-0.1.0.tar.gz.

File metadata

  • Download URL: struct2model-0.1.0.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.4 Linux/5.15.133.1-microsoft-standard-WSL2

File hashes

Hashes for struct2model-0.1.0.tar.gz
Algorithm Hash digest
SHA256 085b0ff3d38541073673e09b2650e09c5373a7da205b164ad22f2702491a035a
MD5 6ebbc385d8f5651c35a5a82e9c929000
BLAKE2b-256 bc32c4091fc26a94ac8bbc6e606ad79130c0789805e95bcabb3193311beaa74f

See more details on using hashes here.

File details

Details for the file struct2model-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: struct2model-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.4 Linux/5.15.133.1-microsoft-standard-WSL2

File hashes

Hashes for struct2model-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b91ec9ed19dd1bd3b0e9efafbc8fb1eb807266ba31ac4231b4343b5079f0d69a
MD5 34d6bfe7b0f5852435c99bf547fd9e13
BLAKE2b-256 17ddb19eaac8939034cfff62338f52d298a5c89b6ca8cc0bba235a4775fa15f2

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