Automatic type conversion library for Python
Project description
autocast
Automatic type conversion for Python.
autocast is a lightweight library that automatically converts objects from one type to another using a graph of registered transformation functions. If a direct conversion isn't available, autocast finds the shortest path through intermediate types to get you the result you need.
It comes with built-in support for NumPy, PyTorch, Open3D, and OmegaConf, but it is designed to be easily extensible for your own classes.
📦 Installation
pip install acaster
To install with support for all optional backends (PyTorch, Open3D, etc.):
pip install "acaster[all]"
🚀 Quick Start
Basic Usage The core function is acast(object, target_type).
import torch
import numpy as np
from autocast import acast
# Create a Torch tensor
tensor = torch.ones((3, 3))
# Automatically convert to NumPy array
array = acast(tensor, np.ndarray)
print(type(array))
# <class 'numpy.ndarray'>
A more complex example: Building a Open3D Point Cloud directly from PyTorch tensors or Numpy arrays without having to manually convert them to Open3D Vector3dVector:
rgb_image = torch.tensor(...) # H x W x 3
predicted_pts = ... # N x 3
pcd = acast((predicted_pts, rgb_image), o3d.geometry.PointCloud)
print(type(pcd))
# <class 'o3d.geometry.PointCloud'>
o3d.io.write_point_cloud(save_path, pcd)
Recursive Collection Handling
autocast automatically handles nested lists and dictionaries.
data = {
"points": torch.rand(100, 3),
"colors": torch.rand(100, 3),
"meta": [torch.tensor(1.0), torch.tensor(2.0)]
}
# Convert everything inside the dict to NumPy arrays
numpy_data = acast(data, np.ndarray)
print(type(numpy_data['points']))
# <class 'numpy.ndarray'>
🛠 Features
- Graph-Based Casting: Uses Breadth-First Search (BFS) to find conversion paths. If you have A -> B and B -> C, autocast can do A -> C automatically.
- Zero-Overhead Imports: Backend modules (like torch or open3d) are only imported if they are installed in your environment.
- Extensible: Register your own types and conversion functions easily.
| From | To | Notes |
|---|---|---|
torch.Tensor |
numpy.ndarray |
Zero-copy where possible |
numpy.ndarray |
torch.Tensor |
|
list/tuple |
np.ndarray |
|
open3d.geometry |
numpy.ndarray |
Vector3dVector support |
omegaconf.ListConfig |
list/dict |
Converts to native containers |
🧩 Extending autocast
You can register your own casts using the add_acast function or the @acaster decorator.
Option 1: The Decorator
from autocast import acaster, acast
class Cat:
def speak(self): return "Meow"
class Dog:
def speak(self): return "Woof"
# Register a function that converts Cat -> Dog
@acaster(Cat, Dog)
def cat_to_dog(cat_instance):
return Dog()
my_cat = Cat()
my_dog = acast(my_cat, Dog)
Option 2: Manual Registration
from autocast import add_acast
def str_to_int(s):
return int(s)
add_acast(str, int, str_to_int)
print(acast("123", int)) # 123
🤝 Contributing
We welcome contributions! If you want to add support for a new library (e.g., Pandas, Polars, JAX), please submit a Pull Request.
- Fork the repository
- Create your feature branch (git checkout -b feature/new-cast)
- Commit your changes
- Push to the branch
- Open a Pull Request
To run the tests, install pytests and run python -m pytest.
📄 License
Distributed under the MIT License. See LICENSE for more information.
Acknowledgements
The idea for this library was inspired by erezsh/autocast.
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
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 acaster-1.0.0.tar.gz.
File metadata
- Download URL: acaster-1.0.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2f7ae9d76de75528850211e3dee97305b52d7dae9bba7c865284165a80870ed
|
|
| MD5 |
ae34a080040ee47d893e7bc8b4243ead
|
|
| BLAKE2b-256 |
80b37e47666ec78d106e480f454dd7d3106f71a26c885aa2faa025c3728ec852
|
File details
Details for the file acaster-1.0.0-py3-none-any.whl.
File metadata
- Download URL: acaster-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
975413307b60eb511a343cd70624d47a4f348fc383612634c5b9f77385620c2c
|
|
| MD5 |
5cc390cf24c726f67493c099dde7c84d
|
|
| BLAKE2b-256 |
1694dc9b69c37e11f8c6a0bedfc8c2588d3f408546e3f2904a14fea93d4cd3b8
|