Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Quantity mapping constructor #120

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
"protected-access", # handled by ruff
"redefined-builtin", # handled by ruff
"too-many-function-args", # plum-dispatch
"unexpected-keyword-arg", # plum-dispatch
"unused-argument", # handled by ruff
"wrong-import-order", # handled by ruff
"wrong-import-position",
Expand Down
39 changes: 38 additions & 1 deletion src/unxt/_quantity/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

__all__ = ["AbstractQuantity", "can_convert_unit"]

from collections.abc import Callable, Sequence
from collections.abc import Callable, Mapping, Sequence
from dataclasses import fields, replace
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias, TypeGuard, TypeVar

Expand Down Expand Up @@ -219,6 +219,43 @@ def constructor(
# Dispatched on no argument. Dispatch to the full constructor.
return cls.constructor(value, unit, dtype=dtype)

@classmethod # type: ignore[no-redef]
@dispatcher
def constructor(
cls: "type[AbstractQuantity]", mapping: Mapping[str, Any]
) -> "AbstractQuantity":
"""Construct a `Quantity` from a Mapping.

Parameters
----------
mapping : Mapping[str, Any]
Mapping of the fields of the `Quantity`, e.g. 'value' and 'unit'.

Returns
-------
AbstractQuantity

Examples
--------
For this example we'll use the `Quantity` class. The same applies to
any subclass of `AbstractQuantity`.

>>> import jax.numpy as jnp
>>> from unxt import Quantity

>>> x = jnp.array([1.0, 2, 3])
>>> q = Quantity.constructor({"value": x, "unit": "m"})
>>> q
Quantity['length'](Array([1., 2., 3.], dtype=float32), unit='m')

>>> Quantity.constructor({"value": q, "unit": "km"})
Quantity['length'](Array([0.001, 0.002, 0.003], dtype=float32), unit='km')

"""
# Dispatch on both arguments.
# Construct using the standard `__init__` method.
return cls.constructor(**mapping)

# See below for additional constructors.

# ===============================================================
Expand Down
Loading