← Back to team overview

python-quantities-developers team mailing list archive

Re: .units returns a string currently, is this set in stone?

 

On Sat, Jan 10, 2009 at 4:50 PM, John Salvatier
<jsalvati@xxxxxxxxxxxxxxxx>wrote:

> The way the properties .units and .dimensionality currently work is ok, but
> I feel as though they could be reworked so that doing common operations is
> easier and more obvious (however, I am not sure exactly how to do so).
>
> Here are some common operations:
>
> 1) check the equivalence of two sets of units
>
>    - currently x.units == y.units works, but works through string
>    comparison which seems inelegant, but maybe doesn't have any actual
>    drawbacks
>
> Its not clear to me what you think should be changed. Why can't you use
dimensionality:

>>> j=10*J
>>> j.dimensionality==J.dimensionality
True

>>> j=J.simplified
>>> j.dimensionality==j.dimensionality
True

>>> j.dimensionality==J.dimensionality
False

I think this works the way you expect it to.


>    -
>
> 2) check the compatibility of two sets of units
>
>    - currently x.simplified.units == y.simplified.units is the most
>    straight forward way to do this, but this adds unnecessary array
>    multiplication
>    - I think it would be intuitive if x.dimensionality == y.dimensionality
>    accomplished this but this would require some refactoring
>
> This is a good point. What if dimensionality also had a simplified method,
which constructed a quantity, called its simplified, and returned the
simplified quantity's dimensionality. Then you could do
x.dimensionality.simplified == y.dimensionality.simplified without having to
perform array operations.

3) check that quantity is dimensionless
>
>    - currently x.dimensionality = dimensionless.dimensionality
>    accomplishes this
>    - it would be intuitive if x.units == dimensionless accomplished this
>    (could be tricky since dimensionless is a Quantity)
>
> I think the simplest check for dimensionless is the one I have been using
for some time:

>>> if dimensionless.dimensionality:
>>>     print 1
>>> if not dimensionless.dimensionality:
>>>     print 2
2


I don't know if there is a way to accomplish all of these at once in an
> elegant way, I am just throwing out ideas and observations.
>
> I think that ".dimensionality" when contrasted with ".units" sounds like a
> way to compare the compatibility of the units ( e.g. #2)
>
> Thoughts?


How do my suggestions sound to you?

References