Simplifying units in pint without changing the magnitude



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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










share|improve this question



















  • 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 may be helpful.

    – Łukasz Rogalski
    May 20 '16 at 18:15











  • This is interesting, but I think this would eventually return <Quantity(0.001, 'kilogram')> 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















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










share|improve this question



















  • 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 may be helpful.

    – Łukasz Rogalski
    May 20 '16 at 18:15











  • This is interesting, but I think this would eventually return <Quantity(0.001, 'kilogram')> 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












1








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










share|improve this question
















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







python units-of-measurement pint






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 20 '16 at 18:07







TomCho

















asked May 20 '16 at 17:58









TomChoTomCho

1,4691235




1,4691235







  • 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 may be helpful.

    – Łukasz Rogalski
    May 20 '16 at 18:15











  • This is interesting, but I think this would eventually return <Quantity(0.001, 'kilogram')> 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





    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 may be helpful.

    – Łukasz Rogalski
    May 20 '16 at 18:15











  • This is interesting, but I think this would eventually return <Quantity(0.001, 'kilogram')> 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




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






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





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 may be helpful.

– Łukasz Rogalski
May 20 '16 at 18:15





You need to redefine base mass unit from gram to kilogram. GitHub issue may be helpful.

– Łukasz Rogalski
May 20 '16 at 18:15













This is interesting, but I think this would eventually return <Quantity(0.001, 'kilogram')> 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





This is interesting, but I think this would eventually return <Quantity(0.001, 'kilogram')> 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 Answer
1






active

oldest

votes


















0














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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37353037%2fsimplifying-units-in-pint-without-changing-the-magnitude%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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





    share|improve this answer



























      0














      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





      share|improve this answer

























        0












        0








        0







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 9:00









        sigvaldmsigvaldm

        268110




        268110





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37353037%2fsimplifying-units-in-pint-without-changing-the-magnitude%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo