Skip to main content

A tiny library to help you to convert classes to DTO and vice versa.

Project description

DuckMapper

A tiny library to help you to convert classes to DTO and vice versa.

Usage

Using the default function

class testClass():
    def __init__(self):
        self.id = None
        self.gender =None
        self.github = None
        self.name = None
        self.password = None
        self.phone_number = None
class testDTO:
    def __init__(self):
        self.name = None
        self.github = None
        self.telephone = None
x = testClass()
x.name = "Lucas"
x.phone_number = 99999999
x.github = "https://github.com/lucascz37"
result = convertTo(source=x, target=testDTO, default=10)

obs: fields not mapped will receive the value passed on the default param

using decorator

With the same two classes created before with decorator you can specify fields that dont have the same name. My suggestion it's that you can create a class that have functions with the decorator that convert all your project's DTO to classes and the other way around.

@mapBy(fields={"telephone": "phone_number"})
def convertTestClasstoTestDTO():
    pass


teste = convertTestClasstoTestDTO(source=x, target=testDTO, default=100)

obs: the fields dict should be like this: {"target_field1": "source_field1", "target_field2": "source_field2"...}

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

DuckMapper-1.0.1.tar.gz (3.1 kB view hashes)

Uploaded Source

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