unxt-api#
Abstract dispatch API for unxt.
unxt-api defines the abstract dispatch interfaces that unxt and other packages implement. It provides a minimal dependency foundation for packages that want to define or use unxtβs multiple-dispatch-based API without pulling in the full unxt implementation.
The unxt-api package serves several important purposes:
Minimal Dependencies: Depends only on
plum-dispatch, not onjax,numpy, orastropyExtensibility: Allows third-party packages to register their own implementations
Type Safety: Provides a clear contract for what functions exist and what they should do
Separation of Concerns: API definitions are separate from implementation details
Installation#
pip install unxt-api
uv add unxt-api
Core API#
The unxt-api package defines abstract dispatch functions organized by domain:
Dimensions (
dimension(),dimension_of()) - Working with physical dimensionsUnits (
unit(),unit_of()) - Constructing and inspecting unitsQuantities (
uconvert(),uconvert_value(),ustrip(),is_unit_convertible(),wrap_to()) - Unit conversion and quantity operationsUnit Systems (
unitsystem_of()) - Inspecting unit systems
Using Multiple Dispatch#
All functions in unxt-api use plum for multiple dispatch. This means:
Functions can have multiple implementations based on argument types
You can register your own implementations for custom types
Type annotations drive dispatch - the runtime types of arguments determine which implementation runs
Viewing All Implementations#
To see all registered implementations of a function:
import unxt_api as uapi
# View all dimension() implementations
uapi.dimension.methods
# View all uconvert() implementations
uapi.uconvert.methods
# View all unit_of() implementations
uapi.unit_of.methods
Registering Custom Implementations#
You can extend unxt-apiβs dispatch system with your own types:
from plum import dispatch
import unxt_api as uapi
class MyCustomQuantity:
def __init__(self, value, unit_str):
self.value = value
self.unit_str = unit_str
# Register implementation for your type
@dispatch
def unit_of(obj: MyCustomQuantity, /):
"""Get unit from MyCustomQuantity."""
return uapi.unit(obj.unit_str)
# Now it works with the dispatch system
my_q = MyCustomQuantity(5.0, "m")
uapi.unit_of(my_q) # Unit("m")
Integration with unxt#
The unxt package provides the concrete implementations of all unxt-api functions. When you use:
import unxt as u
q = u.Q(5, "m")
u.uconvert("km", q)
The u.uconvert function is the implementation registered by unxt for the abstract unxt_api.uconvert function.
See Also#
unxt Documentation - Full implementation with examples
plum Documentation - Multiple dispatch library
License#
BSD 3-Clause License. See LICENSE for details.
Contributing#
Contributions are welcome! Please see the main unxt repository for contributing guidelines.