Utilities to translate resources between platforms.
Reason this release was yanked:
Add badges.
Project description
Resource Translate
Utilities to translate resources between platforms.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for resource_translate-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3b97fdbef94ea285d3d83135ff4c93a05d70733804e02d458caa4e1435b5c42 |
|
MD5 | 760730c6d106b3f7cd2a73c8d0af05ef |
|
BLAKE2b-256 | 578e9e3937bcef200842cfbb0fd91a03810ed0f29dc829bd889a379c351aae69 |