Skip to main content

Value-to-value conversion in Python

Project description

Convertible

Value-to-value conversion in Python.

Usage

Convertible

First, let's create a model for conversion. The model can be any class.

For convenience, let's take dataclass:

from dataclasses import dataclass
from convertible import Convertible

@dataclass
class User(Convertible):
    name: str
    age: int

Now User has conversion methods try_from and try_into. But we haven't implemented any converter. Let's do this.

From Converter

from typing import Tuple
from convertible import From

class UserFromTuple(From[tuple[str, int], User]):
    def try_from(self, value: tuple[str, int]) -> User:
        return User(name=value[0], age=value[1])

Now we can instantiate User from tuple:

user = User.try_from(("John", 23))
assert user.age == 23

Into Converter

We can convert user into another type. Let's define the converter:

# note that User is now the first parameter:
class UserIntoDict(From[User, dict]):
    def try_from(self, user: User) -> dict:
        return {
            "username": user.name,
            "age": user.age
        }

data = user.try_into(dict)
assert data["username"] == user.name

Cross conversion

Implementing From for two Convertible classes automatically provides the second Convertible with an implementation for try_into:

@dataclass
class Admin(Convertible):
    name: str


class AdminFromUser(From[User, Admin]):
    def try_from(self, user: User) -> Admin:
        return Admin(name=user.name)


# these are equivalent
assert user.try_into(Admin) == Admin.try_from(user)

Manual converter registration

You may need to define converters for types that are not Convertible. This can be done using the register_converter function:

class IntFromString:
    def try_from(self, value: str) -> int:
        return int(value)

From[int, str].register_converter(IntFromString)

value = From[int, str].try_from("123")
assert value == 123

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

convertible-0.1.0.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

convertible-0.1.0-py3-none-any.whl (3.3 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