1

Working with python-pint, let's suppose I have a quantity defined as

<Quantity(1.0, 'gram * kilopascal / joule')>

which is equivalent to kilogram/m3. Now, when I try to get that using to_base_units i get:

den.to_base_units()
<Quantity(1000.0, 'gram / meter ** 3')>

which is fine, except I wished the magnitude was still 1, and the units were, instead, <Quantity(1.0, 'kilogram / meter ** 3')>, for user-friendliness purposes. Is there a way to achieve that?

By the way, this happens with many different units throughout my program and there's no (easy) way to know which are the correct units for it to be converted to, which prevents me from using convert_to method. In this case it should change from 1000 gram to kilogram, for example, but in the next case it can be from 0.001 meter to 1 milimeter and so on.

I know that pint 0.7 (I'm still using 0.6) has a new system option, but I also couldn't solve it with that.

Cheers

TomCho
  • 3,204
  • 6
  • 32
  • 83
  • 1
    Is there a `.convert_to(unit)` method that you could call like `q.convert_to('kilogram / meter ** 3')` – Daenyth May 20 '16 at 18:00
  • That's a good idea for this case specifically, but this happens with many different units and there's no (easy) way to know which are the correct units for it to be converted to. I'll make that fact more explicit in my question. – TomCho May 20 '16 at 18:03
  • You need to redefine base mass unit from gram to kilogram. [GitHub issue](https://github.com/hgrecco/pint/issues/215) may be helpful. – Łukasz Rogalski May 20 '16 at 18:15
  • This is interesting, but I think this would eventually return `` for one gram, wouldn't it? I'd like to avoid any quantities from have magnitudes different from 1 (except, of course, when it's not possible). – TomCho May 20 '16 at 18:25

1 Answers1

2

First of all, gram is no longer treated as a base unit in Pint 0.8.1. Instead, Pint treats the kilogram as a base unit which is according to the SI standard:

>>> x = 0.1*ureg['g kpascal / J']
>>> print(x.to_base_units())
0.1 kilogram / meter ** 3

Second, I think you may have mixed up two things, getting a quantity in base units, and getting a convenient prefix. to_base_units() is not about getting a convenient prefix, but about getting the quantity in terms of base units (m, s, kg, A, K, mol and cd in the SI system). If you want a convenient prefix you may want to consider to_compact():

>>> print(x.to_base_units().to_compact())
100.0 gram / meter ** 3
sigvaldm
  • 564
  • 4
  • 15