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
File details
Details for the file convertible-0.1.0.tar.gz
.
File metadata
- Download URL: convertible-0.1.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3f2809599364f26fcc82420c9d75f62f3c5e63b3be0fddccfd46f101187eba4 |
|
MD5 | a7f8d0d68e132da5e0c84e5ae9f7825a |
|
BLAKE2b-256 | 9be12b2ba75923fe6ed7583c4cef238c0889b392131e7626cb856ed7d149fdc8 |
Provenance
File details
Details for the file convertible-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: convertible-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10efbb264b07a958a787fdea80a61529c2781efbc623dc513249c9e4b8f0d588 |
|
MD5 | caf2d585a769b39dcb4b3c5494e6f45d |
|
BLAKE2b-256 | ac67ce7439de8b983906196c257507ebbf0aa1f48b07339de7b96f4756220e29 |