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
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
Hashes for convertible-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10efbb264b07a958a787fdea80a61529c2781efbc623dc513249c9e4b8f0d588 |
|
MD5 | caf2d585a769b39dcb4b3c5494e6f45d |
|
BLAKE2b-256 | ac67ce7439de8b983906196c257507ebbf0aa1f48b07339de7b96f4756220e29 |