pint

时间:2018-06-14 22:01:57

标签: python pint

我正在尝试在需要在维度之间进行转换的情况下使用品脱,例如。液体盎司到克。

我转换所需的值是在数据库中并且针对各种物质(例如,不同液体的不同密度)进行更改,因此我使用Context.add_transformation()方法动态创建我的转换。这是我的测试程序:

#!/opt/env/upgrade/bin/python
import pint

ureg = pint.UnitRegistry()
c = pint.Context('ab')

def vtom(ureg, x):
    # here I print some output just to see if this function is ever called
    # and what arguments are passed.  It is not being called.
    print "vtom(%s, %s)" % (ureg, x)

    # right now I just return the input, so it's 1:1.  I know this is 
    # not right, I am just trying to get pint to find the transform and use it.
    return x

c.add_transformation('[volume]', '[mass]', vtom)
ureg.add_context(c)

PQ = ureg.Quantity

# this works fine    
a = PQ(1 * ureg.oz)
print '%s = %s' % (a, a.to('gram'))

b = PQ(10 * ureg.floz)
c = PQ(10 * ureg.gram)

# both these fail, see exception output below
print '%s = %s' % (c, c.to('floz'))
print '%s = %s' % (b, b.to('gram'))

输出:

1 ounce = 28.349523125 gram
Traceback (most recent call last):
  File "./p.py", line 26, in <module>
    print '%s = %s' % (b, b.to('gram'))
  File "/opt/env/upgrade/local/lib/python2.7/site-packages/pint/quantity.py", line 332, in to
    magnitude = self._convert_magnitude_not_inplace(other, *contexts, **ctx_kwargs)
  File "/opt/env/upgrade/local/lib/python2.7/site-packages/pint/quantity.py", line 300, in _convert_magnitude_not_inplace
    return self._REGISTRY.convert(self._magnitude, self._units, other)
  File "/opt/env/upgrade/local/lib/python2.7/site-packages/pint/registry.py", line 686, in convert
    return self._convert(value, src, dst, inplace)
  File "/opt/env/upgrade/local/lib/python2.7/site-packages/pint/registry.py", line 1214, in _convert
    return super(ContextRegistry, self)._convert(value, src, dst, inplace)
  File "/opt/env/upgrade/local/lib/python2.7/site-packages/pint/registry.py", line 937, in _convert
    return super(NonMultiplicativeRegistry, self)._convert(value, src, dst, inplace)
  File "/opt/env/upgrade/local/lib/python2.7/site-packages/pint/registry.py", line 708, in _convert
    raise DimensionalityError(src, dst, src_dim, dst_dim)
pint.errors.DimensionalityError: Cannot convert from 'fluid_ounce' ([length] ** 3) to 'gram' ([mass])

似乎品脱没有找到我的变形。在单位文本文件(pint附带的默认文件)中,&#39; [volume] = [length] ** 3&#39;是定义的,所以它应该能够走图表并找到[质量]&#39; ...或者我认为......

谢谢!

1 个答案:

答案 0 :(得分:0)

看起来您需要指定在调用.to时需要使用的上下文,因为您的上下文被称为'ab',您应该写c.to('floz', 'ab')。这解决了为什么没有调用函数vtom的问题。

看起来您还必须在两个方向上定义转换:

#!/opt/env/upgrade/bin/python
import pint

ureg = pint.UnitRegistry()
c = pint.Context('ab')

def vtom(ureg, x):
    print ("vtom(%s, %s)" % (ureg, x))
    density = 0.9 * ureg.gram / ureg.ml
    return x/density

def vtom2(ureg, x):
    print ("vtom2(%s, %s)" % (ureg, x))
    density = 0.9 * ureg.gram / ureg.ml
    return x*density

c.add_transformation('[volume]', '[mass]', vtom2)
c.add_transformation('[mass]', '[volume]', vtom)
ureg.add_context(c)
#ureg.enable_contexts('ab')
PQ = ureg.Quantity

# this works fine    
a = PQ(1 * ureg.oz)
print ('%s = %s' % (a, a.to('gram')))

b = PQ(10 * ureg.floz)
c = PQ(10 * ureg.gram)

# both these fail, see exception output below
print ('%s = %s' % (c, c.to('floz', 'ab')))
print ('%s = %s' % (b, b.to('gram','ab')))

(我想你们每个方向都需要不同的功能)。

<强>输出

1 ounce = 28.349523125 gram
vtom(<pint.registry.UnitRegistry object at 0x0390CDB0>, 10 gram)
10 gram = 0.37571136335381117 fluid_ounce
vtom2(<pint.registry.UnitRegistry object at 0x0390CDB0>, 10 fluid_ounce)
10 fluid_ounce = 266.16176606249996 gram

修改 正如下面的评论中所提到的,我认为最初的错误很可能是因为库对返回的值进行了一些维度分析,以检查它是否有意义,如果没有则会引发错误。因此,例如,质量可以是密度乘以体积,但它不能是体积乘以标量,因为这只是一个更大/更小的体积,而不是质量。