解析品脱温度字符串

问题描述 投票:0回答:1

我试图解析带有温度的字符串,但由于温度的非乘性性质,品脱失败。然而,内部转换似乎应该处理这个问题:

from pint import Quantity
Quantity("6degC")

产品

---------------------------------------------------------------------------
OffsetUnitCalculusError                   Traceback (most recent call last)
Cell In[6], line 1
----> 1 Quantity("6degC")

File ~/virtual_enviroments/cfd-2d/lib/python3.10/site-packages/pint/facets/plain/quantity.py:199, in PlainQuantity.__new__(cls, value, units)
    197 if units is None and isinstance(value, str):
    198     ureg = SharedRegistryObject.__new__(cls)._REGISTRY
--> 199     inst = ureg.parse_expression(value)
    200     return cls.__new__(cls, inst)
    202 if units is None and isinstance(value, cls):

File ~/virtual_enviroments/cfd-2d/lib/python3.10/site-packages/pint/facets/plain/registry.py:1398, in GenericPlainRegistry.parse_expression(self, input_string, case_sensitive, **values)
   1395 def _define_op(s: str):
   1396     return self._eval_token(s, case_sensitive=case_sensitive, **values)
-> 1398 return build_eval_tree(gen).evaluate(_define_op)

File ~/virtual_enviroments/cfd-2d/lib/python3.10/site-packages/pint/pint_eval.py:382, in EvalTreeNode.evaluate(self, define_op, bin_op, un_op)
    379     if op_text not in bin_op:
    380         raise DefinitionSyntaxError(f"missing binary operator '{op_text}'")
--> 382     return bin_op[op_text](
    383         self.left.evaluate(define_op, bin_op, un_op),
    384         self.right.evaluate(define_op, bin_op, un_op),
    385     )
    386 elif self.operator:
    387     assert isinstance(self.left, EvalTreeNode), "self.left not EvalTreeNode (4)"

File ~/virtual_enviroments/cfd-2d/lib/python3.10/site-packages/pint/facets/plain/quantity.py:1018, in PlainQuantity.__mul__(self, other)
   1017 def __mul__(self, other):
-> 1018     return self._mul_div(other, operator.mul)

File ~/virtual_enviroments/cfd-2d/lib/python3.10/site-packages/pint/facets/plain/quantity.py:101, in check_implemented.<locals>.wrapped(self, *args, **kwargs)
     99 elif isinstance(other, list) and other and isinstance(other[0], type(self)):
    100     return NotImplemented
--> 101 return f(self, *args, **kwargs)

File ~/virtual_enviroments/cfd-2d/lib/python3.10/site-packages/pint/facets/plain/quantity.py:75, in ireduce_dimensions.<locals>.wrapped(self, *args, **kwargs)
     74 def wrapped(self, *args, **kwargs):
---> 75     result = f(self, *args, **kwargs)
     76     try:
     77         if result._REGISTRY.autoconvert_to_preferred:

File ~/virtual_enviroments/cfd-2d/lib/python3.10/site-packages/pint/facets/plain/quantity.py:966, in PlainQuantity._mul_div(self, other, magnitude_op, units_op)
    964 if not self._check(other):
    965     if not self._ok_for_muldiv(no_offset_units_self):
--> 966         raise OffsetUnitCalculusError(self._units, getattr(other, "units", ""))
    967     if len(offset_units_self) == 1:
    968         if self._units[offset_units_self[0]] != 1 or magnitude_op not in (
    969             operator.mul,
    970             operator.imul,
    971         ):

OffsetUnitCalculusError: Ambiguous operation with offset unit (degree_Celsius). See https://pint.readthedocs.io/en/stable/user/nonmult.html for guidance.

但是这有效

Quantity(6, "degC")
<Quantity(6, 'degree_Celsius')>

所以这应该是内部采取的解析路径。还是我看错了?

python pint
1个回答
0
投票

Pint 从 0.6 版本开始不再支持该语法。必须显式创建具有偏移的单位,例如

Quantity(6, "degC")
而不是
Quantity("6degC")

要获得您想要的行为,请执行以下操作

from pint import UnitRegistry
ureg = UnitRegistry(autoconvert_offset_to_baseunit=True)

ureg.Quantity("6degC")

6度_摄氏度

我喜欢它的解释方式这里

© www.soinside.com 2019 - 2024. All rights reserved.