plum-dispatch


Nameplum-dispatch JSON
Version 2.5.4 PyPI version JSON
download
home_pageNone
SummaryMultiple dispatch in Python
upload_time2024-11-26 15:04:15
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
keywords multiple dispatch
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # [Plum: Multiple Dispatch in Python](https://github.com/beartype/plum)

[![DOI](https://zenodo.org/badge/110279931.svg)](https://zenodo.org/badge/latestdoi/110279931)
[![CI](https://github.com/beartype/plum/workflows/CI/badge.svg?branch=master)](https://github.com/beartype/plum/actions?query=workflow%3ACI)
[![Coverage Status](https://coveralls.io/repos/github/beartype/plum/badge.svg?branch=master&service=github)](https://coveralls.io/github/beartype/plum?branch=master)
[![Latest Docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://beartype.github.io/plum)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Everybody likes multiple dispatch, just like everybody likes plums.

The design philosophy of Plum is to provide an implementation of multiple dispatch that is Pythonic, yet close to how [Julia](http://julialang.org/) does it.
[See here for a comparison between Plum, `multipledispatch`, and `multimethod`.](https://beartype.github.io/plum/comparison.html)

*Note:*
Plum 2 is now powered by [Beartype](https://github.com/beartype/beartype)!
If you notice any issues with the new release, please open an issue.

# Installation

Plum requires Python 3.8 or higher.

```bash
pip install plum-dispatch
```

# [Documentation](https://beartype.github.io/plum)

See [here](https://beartype.github.io/plum).

# What's This?

Plum brings your type annotations to life:

```python
from numbers import Number

from plum import dispatch


@dispatch
def f(x: str):
    return "This is a string!"


@dispatch
def f(x: int):
    return "This is an integer!"


@dispatch
def f(x: Number):
    return "This is a general number, but I don't know which type."
```

```python
>>> f("1")
'This is a string!'

>>> f(1)
'This is an integer!'

>>> f(1.0)
'This is a number, but I don't know which type.'

>>> f(object())
NotFoundLookupError: `f(<object object at 0x7fd3b01cd330>)` could not be resolved.

Closest candidates are the following:
    f(x: str)
        <function f at 0x7fd400644ee0> @ /<ipython-input-2-c9f6cdbea9f3>:6
    f(x: int)
        <function f at 0x7fd3a0235ca0> @ /<ipython-input-2-c9f6cdbea9f3>:11
    f(x: numbers.Number)
        <function f at 0x7fd3a0235d30> @ /<ipython-input-2-c9f6cdbea9f3>:16
```


> [!IMPORTANT]
> Dispatch, as implemented by Plum, is based on the _positional_ arguments to a function.
> Keyword arguments are not used in the decision making for which method to call.
> In particular, this means that _positional arguments without a default value must
> always be given as positional arguments_!
>
> Example:
> ```python
> from plum import dispatch
>
> @dispatch
> def f(x: int):
>    return x
>
> >>> f(1)        # OK
> 1
>
> >> try: f(x=1)  # Not OK
> ... except Exception as e: print(f"{type(e).__name__}: {e}")
> NotFoundLookupError: `f()` could not be resolved...
> ```


This also works for multiple arguments, enabling some neat design patterns:

```python
from numbers import Number, Real, Rational

from plum import dispatch


@dispatch
def multiply(x: Number, y: Number):
    return "Performing fallback implementation of multiplication..."


@dispatch
def multiply(x: Real, y: Real):
    return "Performing specialised implementation for reals..."


@dispatch
def multiply(x: Rational, y: Rational):
    return "Performing specialised implementation for rationals..."
```

```python
>>> multiply(1, 1)
'Performing specialised implementation for rationals...'

>>> multiply(1.0, 1.0)
'Performing specialised implementation for reals...'

>>> multiply(1j, 1j)
'Performing fallback implementation of multiplication...'

>>> multiply(1, 1.0)  # For mixed types, it automatically chooses the right optimisation!
'Performing specialised implementation for reals...'
```
# Projects Using Plum

The following projects are using Plum to do multiple dispatch!
Would you like to add your project here?
Please feel free to open a PR to add it to the list!

- [Coordinax](https://github.com/GalacticDynamics/coordinax) implements coordinates in JAX.
- [GPAR](https://github.com/wesselb/gpar) is an implementation of the [Gaussian Process Autoregressive Model](https://arxiv.org/abs/1802.07182).
- [GPCM](https://github.com/wesselb/gpcm) is an implementation of various [Gaussian Process Convolution Models](https://arxiv.org/abs/2203.06997).
- [Galax](https://github.com/GalacticDynamics/galax) does galactic and gravitational dynamics.
- [Geometric Kernels](https://github.com/GPflow/GeometricKernels) implements kernels on non-Euclidean spaces, such as Riemannian manifolds, graphs, and meshes.
- [LAB](https://github.com/wesselb/lab) uses Plum to provide backend-agnostic linear algebra (something that works with PyTorch/TF/JAX/etc).
- [MLKernels](https://github.com/wesselb/mlkernels) implements standard kernels.
- [MMEval](https://github.com/open-mmlab/mmeval) is a unified evaluation library for multiple machine learning libraries.
- [Matrix](https://github.com/wesselb/matrix) extends LAB and implements structured matrix types, such as low-rank matrices and Kronecker products.
- [NetKet](https://github.com/netket/netket), a library for machine learning with JAX/Flax targeted at quantum physics, uses Plum extensively to pick the right, efficient implementation for a large combination of objects that interact.
- [NeuralProcesses](https://github.com/wesselb/neuralprocesses) is a framework for composing Neural Processes.
- [OILMM](https://github.com/wesselb/oilmm) is an implementation of the [Orthogonal Linear Mixing Model](https://arxiv.org/abs/1911.06287).
- [PySAGES](https://github.com/SSAGESLabs/PySAGES) is a suite for advanced general ensemble simulations.
- [Quax](https://github.com/patrick-kidger/quax) implements multiple dispatch over abstract array types in JAX.
- [Unxt](https://github.com/GalacticDynamics/unxt) implements unitful quantities in JAX.
- [Varz](https://github.com/wesselb/varz) uses Plum to provide backend-agnostic tools for non-linear optimisation.

[See the docs for a comparison of Plum to other implementations of multiple dispatch.](https://beartype.github.io/plum/comparison.html)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "plum-dispatch",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "multiple dispatch",
    "author": null,
    "author_email": "Wessel Bruinsma <wessel.p.bruinsma@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/8c/a6/d6b9db4d70d4ba7857800089ea65dd5e81dbf1a482db04eee89dbb8f9c3b/plum_dispatch-2.5.4.tar.gz",
    "platform": null,
    "description": "# [Plum: Multiple Dispatch in Python](https://github.com/beartype/plum)\n\n[![DOI](https://zenodo.org/badge/110279931.svg)](https://zenodo.org/badge/latestdoi/110279931)\n[![CI](https://github.com/beartype/plum/workflows/CI/badge.svg?branch=master)](https://github.com/beartype/plum/actions?query=workflow%3ACI)\n[![Coverage Status](https://coveralls.io/repos/github/beartype/plum/badge.svg?branch=master&service=github)](https://coveralls.io/github/beartype/plum?branch=master)\n[![Latest Docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://beartype.github.io/plum)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\nEverybody likes multiple dispatch, just like everybody likes plums.\n\nThe design philosophy of Plum is to provide an implementation of multiple dispatch that is Pythonic, yet close to how [Julia](http://julialang.org/) does it.\n[See here for a comparison between Plum, `multipledispatch`, and `multimethod`.](https://beartype.github.io/plum/comparison.html)\n\n*Note:*\nPlum 2 is now powered by [Beartype](https://github.com/beartype/beartype)!\nIf you notice any issues with the new release, please open an issue.\n\n# Installation\n\nPlum requires Python 3.8 or higher.\n\n```bash\npip install plum-dispatch\n```\n\n# [Documentation](https://beartype.github.io/plum)\n\nSee [here](https://beartype.github.io/plum).\n\n# What's This?\n\nPlum brings your type annotations to life:\n\n```python\nfrom numbers import Number\n\nfrom plum import dispatch\n\n\n@dispatch\ndef f(x: str):\n    return \"This is a string!\"\n\n\n@dispatch\ndef f(x: int):\n    return \"This is an integer!\"\n\n\n@dispatch\ndef f(x: Number):\n    return \"This is a general number, but I don't know which type.\"\n```\n\n```python\n>>> f(\"1\")\n'This is a string!'\n\n>>> f(1)\n'This is an integer!'\n\n>>> f(1.0)\n'This is a number, but I don't know which type.'\n\n>>> f(object())\nNotFoundLookupError: `f(<object object at 0x7fd3b01cd330>)` could not be resolved.\n\nClosest candidates are the following:\n    f(x: str)\n        <function f at 0x7fd400644ee0> @ /<ipython-input-2-c9f6cdbea9f3>:6\n    f(x: int)\n        <function f at 0x7fd3a0235ca0> @ /<ipython-input-2-c9f6cdbea9f3>:11\n    f(x: numbers.Number)\n        <function f at 0x7fd3a0235d30> @ /<ipython-input-2-c9f6cdbea9f3>:16\n```\n\n\n> [!IMPORTANT]\n> Dispatch, as implemented by Plum, is based on the _positional_ arguments to a function.\n> Keyword arguments are not used in the decision making for which method to call.\n> In particular, this means that _positional arguments without a default value must\n> always be given as positional arguments_!\n>\n> Example:\n> ```python\n> from plum import dispatch\n>\n> @dispatch\n> def f(x: int):\n>    return x\n>\n> >>> f(1)        # OK\n> 1\n>\n> >> try: f(x=1)  # Not OK\n> ... except Exception as e: print(f\"{type(e).__name__}: {e}\")\n> NotFoundLookupError: `f()` could not be resolved...\n> ```\n\n\nThis also works for multiple arguments, enabling some neat design patterns:\n\n```python\nfrom numbers import Number, Real, Rational\n\nfrom plum import dispatch\n\n\n@dispatch\ndef multiply(x: Number, y: Number):\n    return \"Performing fallback implementation of multiplication...\"\n\n\n@dispatch\ndef multiply(x: Real, y: Real):\n    return \"Performing specialised implementation for reals...\"\n\n\n@dispatch\ndef multiply(x: Rational, y: Rational):\n    return \"Performing specialised implementation for rationals...\"\n```\n\n```python\n>>> multiply(1, 1)\n'Performing specialised implementation for rationals...'\n\n>>> multiply(1.0, 1.0)\n'Performing specialised implementation for reals...'\n\n>>> multiply(1j, 1j)\n'Performing fallback implementation of multiplication...'\n\n>>> multiply(1, 1.0)  # For mixed types, it automatically chooses the right optimisation!\n'Performing specialised implementation for reals...'\n```\n# Projects Using Plum\n\nThe following projects are using Plum to do multiple dispatch!\nWould you like to add your project here?\nPlease feel free to open a PR to add it to the list!\n\n- [Coordinax](https://github.com/GalacticDynamics/coordinax) implements coordinates in JAX.\n- [GPAR](https://github.com/wesselb/gpar) is an implementation of the [Gaussian Process Autoregressive Model](https://arxiv.org/abs/1802.07182).\n- [GPCM](https://github.com/wesselb/gpcm) is an implementation of various [Gaussian Process Convolution Models](https://arxiv.org/abs/2203.06997).\n- [Galax](https://github.com/GalacticDynamics/galax) does galactic and gravitational dynamics.\n- [Geometric Kernels](https://github.com/GPflow/GeometricKernels) implements kernels on non-Euclidean spaces, such as Riemannian manifolds, graphs, and meshes.\n- [LAB](https://github.com/wesselb/lab) uses Plum to provide backend-agnostic linear algebra (something that works with PyTorch/TF/JAX/etc).\n- [MLKernels](https://github.com/wesselb/mlkernels) implements standard kernels.\n- [MMEval](https://github.com/open-mmlab/mmeval) is a unified evaluation library for multiple machine learning libraries.\n- [Matrix](https://github.com/wesselb/matrix) extends LAB and implements structured matrix types, such as low-rank matrices and Kronecker products.\n- [NetKet](https://github.com/netket/netket), a library for machine learning with JAX/Flax targeted at quantum physics, uses Plum extensively to pick the right, efficient implementation for a large combination of objects that interact.\n- [NeuralProcesses](https://github.com/wesselb/neuralprocesses) is a framework for composing Neural Processes.\n- [OILMM](https://github.com/wesselb/oilmm) is an implementation of the [Orthogonal Linear Mixing Model](https://arxiv.org/abs/1911.06287).\n- [PySAGES](https://github.com/SSAGESLabs/PySAGES) is a suite for advanced general ensemble simulations.\n- [Quax](https://github.com/patrick-kidger/quax) implements multiple dispatch over abstract array types in JAX.\n- [Unxt](https://github.com/GalacticDynamics/unxt) implements unitful quantities in JAX.\n- [Varz](https://github.com/wesselb/varz) uses Plum to provide backend-agnostic tools for non-linear optimisation.\n\n[See the docs for a comparison of Plum to other implementations of multiple dispatch.](https://beartype.github.io/plum/comparison.html)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Multiple dispatch in Python",
    "version": "2.5.4",
    "project_urls": {
        "documentation": "https://beartype.github.io/plum",
        "repository": "https://github.com/beartype/plum"
    },
    "split_keywords": [
        "multiple",
        "dispatch"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a20369d3df4a87d14c0afc62a5492234d95a51a5abcc10bddbbebe0d08693d0e",
                "md5": "5a255f25dced0b82b01f546a5eed2205",
                "sha256": "8c05b120dd93a42f51adb0fb5e3e5f9b46129cd4d576872ad2ceb692ce1f6759"
            },
            "downloads": -1,
            "filename": "plum_dispatch-2.5.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5a255f25dced0b82b01f546a5eed2205",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 42412,
            "upload_time": "2024-11-26T15:04:14",
            "upload_time_iso_8601": "2024-11-26T15:04:14.188600Z",
            "url": "https://files.pythonhosted.org/packages/a2/03/69d3df4a87d14c0afc62a5492234d95a51a5abcc10bddbbebe0d08693d0e/plum_dispatch-2.5.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8ca6d6b9db4d70d4ba7857800089ea65dd5e81dbf1a482db04eee89dbb8f9c3b",
                "md5": "61efcc1eaf59097999a452c5aea819a3",
                "sha256": "db09f143ccf8f8c2ca4440349e895fe4655f2ad42102988693bea1e6991ead35"
            },
            "downloads": -1,
            "filename": "plum_dispatch-2.5.4.tar.gz",
            "has_sig": false,
            "md5_digest": "61efcc1eaf59097999a452c5aea819a3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 35237,
            "upload_time": "2024-11-26T15:04:15",
            "upload_time_iso_8601": "2024-11-26T15:04:15.448884Z",
            "url": "https://files.pythonhosted.org/packages/8c/a6/d6b9db4d70d4ba7857800089ea65dd5e81dbf1a482db04eee89dbb8f9c3b/plum_dispatch-2.5.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-26 15:04:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "beartype",
    "github_project": "plum",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "plum-dispatch"
}
        
Elapsed time: 0.36324s