units, try to find if a base quantity has a unit

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
User avatar
bernd
Posts: 3869
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

units, try to find if a base quantity has a unit

Postby bernd » Mon Mar 13, 2017 11:24 am

I would like to know if a base quantity has a unit or not to get around a value errror if a base.quantity has no unit ... Has anyone an idea how to do this?

Code: Select all

tc_new_unit = "W/m/K"

withUnit = u'43.0 W/m/K'
tc = FreeCAD.Units.Quantity(withUnit)
tc
tc_with_new_unit = tc.getValueAs(tc_new_unit)
tc_with_new_unit


withoutUnit = u'0.000009'
tc = FreeCAD.Units.Quantity(withoutUnit)
tc
tc_with_new_unit = tc.getValueAs(tc_new_unit)


bernd
User avatar
bernd
Posts: 3869
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: units, try to find if a base quantity has a unit

Postby bernd » Tue Mar 14, 2017 3:57 pm

Found something, but it does not work as expected :( An empty base unit does return True :(

Code: Select all

tc_new_unit = "W/m/K"

withUnit = u'43.0 W/m/K'
FreeCAD.Units.Quantity(withUnit)
FreeCAD.Units.Unit(withUnit)
if FreeCAD.Units.Unit(withUnit):
    print 'has unit'
else:
    print 'has no unit'



withoutUnit = u'0.000009'
FreeCAD.Units.Quantity(withoutUnit)
FreeCAD.Units.Unit(withoutUnit)
if FreeCAD.Units.Unit(withoutUnit):
    print 'has unit'
else:
    print 'has no unit'


output:

Code: Select all

>>>
>>> tc_new_unit = "W/m/K"
>>>
>>> withUnit = u'43.0 W/m/K'
>>> FreeCAD.Units.Quantity(withUnit)
43000 mm*kg/(s^3*K)
>>> FreeCAD.Units.Unit(withUnit)
Unit: mm*kg/(s^3*K) (1,1,-3,0,-1,0,0,0) [ThermalConductivity]
>>> if FreeCAD.Units.Unit(withUnit):
...     print 'has unit'
... else:
...     print 'has no unit'
...
has unit
>>>
>>>
>>> withoutUnit = u'0.000009'
>>> FreeCAD.Units.Quantity(withoutUnit)
9e-06
>>> FreeCAD.Units.Unit(withoutUnit)
Unit:  (0,0,0,0,0,0,0,0)
>>> if FreeCAD.Units.Unit(withoutUnit):
...     print 'has unit'
... else:
...     print 'has no unit'
...
has unit
>>>



Still I don not know how to check if a base quantity has no unit to get around the following problem:

Code: Select all

FreeCAD.Units.Quantity(withUnit).getValueAs(tc_new_unit)
FreeCAD.Units.Quantity(withoutUnit).getValueAs(tc_new_unit)


output

Code: Select all

>>>
>>> FreeCAD.Units.Quantity(withUnit).getValueAs(tc_new_unit)
43
>>> FreeCAD.Units.Quantity(withoutUnit).getValueAs(tc_new_unit)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: Unit mismatch
>>>
Last edited by bernd on Fri Mar 17, 2017 10:52 am, edited 1 time in total.
ian.rees
Posts: 524
Joined: Sun Jun 15, 2014 3:28 am
Contact:

Re: units, try to find if a base quantity has a unit

Postby ian.rees » Fri Mar 17, 2017 6:09 am

I think the Pythonic thing to wrap it in a try/catch block, but maybe I'm misunderstanding the issue? So, in your first example:

Code: Select all

try:
    tc_with_new_unit = tc.getValueAs(tc_new_unit)
    print "it has a unit"
except ValueError:
    print "it doesn't have a unit"
User avatar
bernd
Posts: 3869
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: units, try to find if a base quantity has a unit

Postby bernd » Fri Mar 17, 2017 10:48 am

ian.rees wrote:I think the Pythonic thing to wrap it in a try/catch block, but maybe I'm misunderstanding the issue? So, in your first example:

Code: Select all

try:
    tc_with_new_unit = tc.getValueAs(tc_new_unit)
    print "it has a unit"
except ValueError:
    print "it doesn't have a unit"

This is exactly what I would like to avoid, add dozens of try except int the code just to check if a Base.Quantity has a unit.
User avatar
bernd
Posts: 3869
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: units, try to find if a base quantity has a unit

Postby bernd » Fri Mar 17, 2017 10:57 am

found a possibility, search the unit type in the string ...

Code: Select all

withUnit = u'43.0 W/m/K'
FreeCAD.Units.Quantity(withUnit)
FreeCAD.Units.Unit(withUnit)
if 'ThermalConductivity' in str(FreeCAD.Units.Unit(withUnit)):
    print 'has unit for ThermalConductivity'
else:
    print 'has NO unit for ThermalConductivity'


withoutUnit = u'0.000009'
FreeCAD.Units.Quantity(withoutUnit)
FreeCAD.Units.Unit(withoutUnit)
if 'ThermalConductivity' in str(FreeCAD.Units.Unit(withoutUnit)):
    print 'has unit for ThermalConductivity'
else:
    print 'has NO unit for ThermalConductivity'




output:

Code: Select all

>>>
>>> withUnit = u'43.0 W/m/K'
>>> FreeCAD.Units.Quantity(withUnit)
43000 mm*kg/(s^3*K)
>>> FreeCAD.Units.Unit(withUnit)
Unit: mm*kg/(s^3*K) (1,1,-3,0,-1,0,0,0) [ThermalConductivity]
>>> if 'ThermalConductivity' in str(FreeCAD.Units.Unit(withUnit)):
...     print 'has unit for ThermalConductivity'
... else:
...     print 'has NO unit for ThermalConductivity'
...
has unit for ThermalConductivity
>>>
>>> withoutUnit = u'0.000009'
>>> FreeCAD.Units.Quantity(withoutUnit)
9e-06
>>> FreeCAD.Units.Unit(withoutUnit)
Unit:  (0,0,0,0,0,0,0,0)
>>> if 'ThermalConductivity' in str(FreeCAD.Units.Unit(withoutUnit)):
...     print 'has unit for ThermalConductivity'
... else:
...     print 'has NO unit for ThermalConductivity'
...
has NO unit for ThermalConductivity
>>>
ian.rees
Posts: 524
Joined: Sun Jun 15, 2014 3:28 am
Contact:

Re: units, try to find if a base quantity has a unit

Postby ian.rees » Fri Mar 17, 2017 7:30 pm

I see, it seems like there are at least a couple options then:

1) The getValueAs() method could be made to "succeed" if it's run on an object where the Unit is (0,0,0,0,0,0,0,0). I feel like this is maybe a little too sloppy.
2) Could make a new method "hasUnit()" that returns a boolean like:

Code: Select all

>>> withoutUnit = u'0.000009'
>>> tc = FreeCAD.Units.Quantity(withoutUnit)
>>> tc
9e-06
>>> tc.hasUnit()
False
 


But, I don't really understand what the situation is, that's creating objects that have Unit == (0,0,0,0,0,0,0,0) when they really mean something like ThermalConductivity. It feels like there might be an earlier problem to be solved, which would prevent the issue you're having. -Ian-