{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# fastdispatch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[![CI](https://github.com/fastai/fastdispatch/actions/workflows/test.yaml/badge.svg)](https://github.com/fastai/fastdispatch/actions/workflows/test.yaml) [![Deploy to GitHub Pages](https://github.com/fastai/fastdispatch/actions/workflows/deploy.yaml/badge.svg)](https://github.com/fastai/fastdispatch/actions/workflows/deploy.yaml)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wrapper for plum dispatch to make it more compatible with fastcore's typedispatch. Hopefully this is just temporary, and instead the functionality here will be moved into plum." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`pip install fastdispatch`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How to use" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`fastdispatch` works just like plum, with a few extensions. We recommend reading through their [very informative docs](https://github.com/wesselb/plum), however, here's a quick example to get started:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastcore.test import *\n", "from fastdispatch import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Decorate type annotated Python functions with `fastdispatch.dispatch` to add them as _methods_ to a dispatched _function_ (following [Julia's terminology](https://docs.julialang.org/en/v1/manual/methods/)):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@dispatch\n", "def f(x: str): return \"This is a string!\"\n", "\n", "@dispatch\n", "def f(x: int): return \"This is an integer!\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is a string!'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f('1')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is an integer!'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If there's no matching method, `plum.NotFoundLookupError` is raised:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_fail(lambda: f(1.0), contains='For function \"f\", signature Signature(builtins.float) could not be resolved.')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }