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
Metadata
Release files for convertible 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| convertible-0.1.0.tar.gz | 3.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| convertible-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.9 kB
Release files / convertible-0.1.0.tar.gz
| Download URL | convertible-0.1.0.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a3f2809599364f26fcc82420c9d75f62f3c5e63b3be0fddccfd46f101187eba4
|
|
BLAKE2b-256 checksum How to use checksums |
9be12b2ba75923fe6ed7583c4cef238c0889b392131e7626cb856ed7d149fdc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.1
|
Release files / convertible-0.1.0-py3-none-any.whl
| Download URL | convertible-0.1.0-py3-none-any.whl |
|---|---|
| Size | 3.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
10efbb264b07a958a787fdea80a61529c2781efbc623dc513249c9e4b8f0d588
|
|
BLAKE2b-256 checksum How to use checksums |
ac67ce7439de8b983906196c257507ebbf0aa1f48b07339de7b96f4756220e29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.1
|