简体   繁体   中英

Find the original MeasureBase value with python-measurement

python-measurement is a library to read measurements pythonically, https://python-measurement.readthedocs.io/en/latest/index.html eg

>>> from measurement.measures import Weight
>>> w = Weight(lb=135) # Represents 135lbs
>>> print w
135.0 lb
>>> print w.kg
61.234919999999995

When I read the measurement object directly into django/ORM/DB, I'm not sure why it automatically converted to the default_unit , eg

  • When reading 1 us_qt , it returns aa MeasureBase object with the value .001 cubic_meter

In the database, the value is .001 and the unit is cubic_meter , but I need to see what the original unit was - ie US qt .

Is there a way to see the original unit (at a minimum, as the value can be computed) and value that was entered?

Most probably, there's some mechanism in your Django/DB library that uses python-measurement defaults the different subclasses of MeasureBase to some default units. Like how the "standard unit" feature is used in the doc example .

from sympy import S, Symbol
from measurement.base import MeasureBase

class Temperature(MeasureBase):
    SU = Symbol('kelvin')
    STANDARD_UNIT = 'k'
    UNITS = {
        'c': SU - S(273.15),
        'f': (SU - S(273.15)) * S('9/5') + 32,
        'k': 1.0
    }
    ALIAS = {
        'celsius': 'c',
        'fahrenheit': 'f',
        'kelvin': 'k',
    }

What you can try then is to guess the units independently using the native python-measurement functions, eg

>>> from measurement.utils import guess
>>> m = guess(10, 'qt')
>>> repr(m)
Volume(qt=10.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM