Skip to content

Question: to_compact for Measurements #584

@DAsh0244

Description

@DAsh0244

I guess this is somewhat related to #350, #566.

A Quantity object has the method to_compact, however if using a Measurement instance, the inherited to_compact method returns without any formatting because the magnitude is a uncertainties.ufloat which is not a numbers.Number type object. This is caused by the following lines:

if not isinstance(self.magnitude, numbers.Number):
    msg = ("to_compact applied to non numerical types "
           "has an undefined behavior.")
    w = RuntimeWarning(msg)
    warnings.warn(w, stacklevel=2)
    return self

demo of behavior (Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 running on win7x64 pro):

# current behavior
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> M_ = ureg.Measurement
>>> m1 = M_(10, 0.5, ureg.ohm)*1e3
>>> m1.to_compact()
__main__:1: RuntimeWarning: to_compact applied to non numerical types has an undefined behavior.
<Measurement(10000.00, 500.00, ohm)>

Unfortunately I found the docs less than helpful in this case. Am I missing something on how to get the following output?:

# desirable behavior
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> M_ = ureg.Measurement
>>> m1 = M_(10, 0.5, ureg.ohm)*1e3
>>> m1.to_compact()  #  what method to call?
<Measurement(10.00, 0.50, kiloohm)>

If there is no built in method/support for this I think it shouldn't be too hard to implement as a new/overridden method in the Measurement class.
The major issue with this approach that comes to mind is:

  • as in More readable to_compact() #566, if the Quantity.to_compact method changes behavior, then this new Measurement.to_compact method will have to be modified as well to mirror that behavior in order to provide a uniform experience.

I wouldn't mind sitting down and taking a crack at it. But before I go poking too far into things I haven't fully read through, I figured I would ask if I was just missing the correct way of presenting Measurements in a more convenient to understand form, as just display formatting will not perform the unit conversion that is desired here.

It looks like the Measurement class will be getting an overhaul coming soon so if this is something that is already planned then great. I've recently taken in interest in including tolerances/variances in demo examples to illustrate more realistic system models and pint/uncertainties has been an intuitive and flexible answer to the goal thus far.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions