Skip to main content

Utilities to translate resources between platforms.

Reason this release was yanked:

Add badges.

Project description

Resource Translate

Utilities to translate resources between platforms.

Code style: black


Installation

pip install resource_translate

Or with testing:

pip install resource_translate[dev]

Examples

Translating an Object

Given the following MockPerson object:

class _MockAddress:
    town = "Bobtonville"
    country_code = "US"


class MockPerson:
    first_name = "Bob"
    calling_code = 1
    phone_number = "(916) 276-6782"
    employer = None
    address = _MockAddress

And the following PersonFromObj translator:

from resource_translate import Translator, attr


class PersonFromObj(Translator):
    constants = {"tags": "mock-person"}
    mapping = {
        "name": "first_name",
        "billing_address": {"city": "address.town"},
        "employer": "employer",
        "missing": "absent",
    }

    @attr
    def phone(self):
        return f"+{self.resource.calling_code} {self.resource.phone_number}"

    @attr("billing_address")
    def country(self):
        if self.resource.address.country_code == "US":
            return "USA"

        return self.resource.address.country_code

    @attr("nested", "attr")
    def deep(self):
        if self.repr["billing_address"]["country"] == "USA":
            return "Deep Attribute"

Calling:

PersonFromObj(MockPerson).repr

Returns:

{
    "name": "Bob",
    "phone": "+1 (916) 276-6782",
    "nested": {"attr": {"deep": "Deep Attribute"}},
    "tags": "mock-person",
    "billing_address": {"city": "Bobtonville", "country": "USA"},
}

Translating a Mapping

Given the following MOCK_PERSON mapping:

_MOCK_ADDRESS = {"town": "Bobtonville", "country_code": "US"}

MOCK_PERSON = {
    "first_name": "Bob",
    "calling_code": 1,
    "phone_number": "(916) 276-6782",
    "employer": None,
    "address": _MOCK_ADDRESS,
}

And the following PersonFromMap translator:

from resource_translate import Translator, attr


class PersonFromMap(Translator):
    constants = {"tags": "mock-person"}
    mapping = {
        "name": "first_name",
        "billing_address": {"city": ("address", "town")},
        "employer": "employer",
        "missing": "absent",
    }

    @attr
    def phone(self):
        return f"+{self.resource['calling_code']} {self.resource['phone_number']}"

    @attr("billing_address")
    def country(self):
        if self.resource["address"]["country_code"] == "US":
            return "USA"

        return self.resource["address"]["country_code"]

    @attr("nested", "attr")
    def deep(self):
        if self.repr["billing_address"]["country"] == "USA":
            return "Deep Attribute"

Calling:

PersonFromMap(MOCK_PERSON, from_map=True).repr

Returns:

{
    "name": "Bob",
    "phone": "+1 (916) 276-6782",
    "nested": {"attr": {"deep": "Deep Attribute"}},
    "tags": "mock-person",
    "billing_address": {"city": "Bobtonville", "country": "USA"},
}

Explicit Attributes

Keyword arguments are set directly on the translated resource - given the prior PersonFromObj translator, calling:

PersonFromObj(MockPerson, tags="kwargs-override", billing_address={"postal_code": "78498"}).repr

Returns:

{
    "name": "Bob",
    "phone": "+1 (916) 276-6782",
    "nested": {"attr": {"deep": "Deep Attribute"}},
    "tags": "kwargs-override",
    "billing_address": {"city": "Bobtonville", "country": "USA", "postal_code": "78498"},
}

For additional examples, see tests/.


Reference

$ python
>>> from resource_translate import Translator, attr
>>> help(Translator)
...
>>> help(attr)
...

Testing

Having installed with testing, invoke Pytest from the project root:

pytest

Or with coverage:

pytest --cov src --cov-report term-missing

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

resource-translate-0.0.1.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

resource_translate-0.0.1-py3-none-any.whl (8.4 kB view hashes)

Uploaded Python 3

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