Basic type casting.
Project description
castfit: basic type casting
Cuddles the Cat
"If it fits, I sits."
Why?
castfit helps you convert things like command-line arguments (e.g., from docopt) and simple API responses into something more typed with low overhead.
Install
# modern (recommended)
uv add castfit
# classic
python -m pip install castfit
Alternatively, you can just download the single file and name it castfit.py.
Example: CLI-like Args
# Example: CLI-like args
from typing import Optional
from pathlib import Path
from castfit import castfit
class Args:
host: str
port: int
timeout: Optional[float]
log: Path
data = {
"host": "localhost",
"port": "8080",
# "timeout": "5.0" # key can be missing
"log": "app.log",
}
config = castfit(Args, data)
assert config.host == "localhost"
assert config.port == 8080
assert config.timeout is None
assert config.log == Path("app.log")
# if timeout was present:
data = {"host": "localhost", "port": "8080", "timeout": "5.0", "log": "app.log"}
config = castfit(Args, data)
assert config.host == "localhost"
assert config.port == 8080
assert config.timeout == 5.0
assert config.log == Path("app.log")
Example: Nested Types
# Example: nested types
from dataclasses import dataclass
from typing import Literal
from castfit import castfit
@dataclass
class Pet:
name: str
type: Literal["cat", "dog", "other"]
age: int
@dataclass
class Owner:
name: str
pets: list[Pet]
owner_data = {
"name": "Alice",
"pets": [
{"name": "Cuddles", "type": "cat", "age": "4"},
{"name": "Buddy", "type": "dog", "age": "2.5"}, # age will be cast to int(2)
],
}
owner = castfit(Owner, owner_data)
assert owner.name == "Alice"
assert len(owner.pets) == 2
assert isinstance(owner.pets[0], Pet)
assert owner.pets[0].name == "Cuddles"
assert owner.pets[0].type == "cat"
assert owner.pets[0].age == 4
assert owner.pets[1].name == "Buddy"
assert owner.pets[1].age == 2 # Cast from "2.5" to int
Example: Custom Functions
# Example: adding a custom converter
from dataclasses import dataclass
import castfit
@dataclass
class LatLon:
lat: float
lon: float
@castfit.casts
def str_to_latlon(s: str) -> LatLon:
lat, lon = map(float, s.split(","))
return LatLon(lat, lon)
assert castfit.to_type("40.7,-74.0", LatLon) == LatLon(40.7, -74.0)
Other Projects
pydantic: comprehensive, but feels heavy.cattrs: good simple cases, but has a complex set of converters.
License
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
castfit-0.1.2.tar.gz
(15.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file castfit-0.1.2.tar.gz.
File metadata
- Download URL: castfit-0.1.2.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7cc3db9ce9dfa5f0cee86efe8d101def223cb672dd0a59332abd0d38f8c28b3a
|
|
| MD5 |
e408d8393fb2509597ecb84a8d823c8e
|
|
| BLAKE2b-256 |
c94491ec3efedd42e8b364844e356febdd4305432b1a22e60a7876750fd9df33
|
File details
Details for the file castfit-0.1.2-py3-none-any.whl.
File metadata
- Download URL: castfit-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cd47a274b84ec8d77a73f2b9da8036f06124b0a79120c71e158e8ce964dfbb7
|
|
| MD5 |
628896a2fdfdd2c7be7c6c44f50a5d94
|
|
| BLAKE2b-256 |
b9d288b04229f6b3d63373bc760c89c064578353288df3b5db463d8e2696ebe5
|