Skip to content

ucon.contexts

Cross-dimensional conversion contexts for physical relationships.

ucon.contexts

Cross-dimensional conversion contexts for physical relationships.

A :class:ConversionContext bundles a set of cross-dimensional edges (e.g., wavelength <-> frequency via c, or temperature <-> energy via k_B). These edges are only active within a using_context() block.

Built-in contexts

  • :data:spectroscopy -- wavelength/frequency/energy via c and h
  • :data:boltzmann -- temperature/energy via k_B

Examples

from ucon import units from ucon.contexts import spectroscopy, using_context with using_context(spectroscopy): ... result = units.meter(500e-9).to(units.hertz) ... print(f"{result.quantity:.3e} Hz") 5.996e+14 Hz

ContextEdge dataclass

A single cross-dimensional edge specification.

Parameters

src : Unit or UnitProduct Source unit expression. dst : Unit or UnitProduct Destination unit expression. map : Map Conversion morphism from src to dst.

ConversionContext dataclass

An immutable bundle of cross-dimensional conversion edges.

Contexts are activated via :func:using_context, which copies the current graph, inserts the context edges, and scopes the copy for the duration of the with block.

Parameters

name : str Human-readable name (e.g., "spectroscopy"). edges : tuple[ContextEdge, ...] The cross-dimensional edge specifications. description : str Optional description of the physical basis.

using_context(*contexts)

Activate one or more conversion contexts.

Creates a copy of the current graph, inserts all context edges, and scopes the extended graph via using_graph().

Parameters

*contexts : ConversionContext One or more contexts to activate.

Yields

ConversionGraph The extended graph with context edges.

Examples

with using_context(spectroscopy): ... result = units.meter(500e-9).to(units.hertz)

with using_context(spectroscopy, boltzmann): ... result = units.kelvin(300).to(units.joule)