molop.unit¶
This page provides the API reference for the molop.unit module.
Author: TMJ Date: 2024-10-19 09:57:26 LastEditors: TMJ LastEditTime: 2025-12-16 01:12:17 Description: 请填写简介
unit_transform(value, unit)
¶
Transform the unit of a quantity.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
value
|
Optional[PlainQuantity]
|
The quantity to be transformed. |
必需 |
unit
|
UnitLike
|
The target unit. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
PlainQuantity | None
|
Optional[PlainQuantity]: The transformed quantity. |
源代码位于: src/molop/unit/__init__.py
Python
def unit_transform(value: PlainQuantity | None, unit: UnitLike) -> PlainQuantity | None:
"""
Transform the unit of a quantity.
Parameters:
value (Optional[PlainQuantity]): The quantity to be transformed.
unit (UnitLike): The target unit.
Returns:
Optional[PlainQuantity]: The transformed quantity.
"""
if value is None:
return None
if value.units == unit:
return value
return value.to(unit)