Removed try/except block in Quantity.dimensionality

As the Quantity class has an expensive __getattr__ this check was having
a measurable performance hit.

Close #251
This commit is contained in:
Hernan Grecco
2016-01-08 01:06:58 -03:00
parent 81f547c56b
commit 7edf3fee67
2 changed files with 59 additions and 3 deletions

56
docs/systems.rst Normal file
View File

@@ -0,0 +1,56 @@
.. systems_:
Different Unit Systems (and default units)
==========================================
Pint Unit Registry has the concept of system, which is a group of units
>>> import pint
>>> ureg = pint.UnitRegistry(system='mks')
>>> ureg.system
'mks'
This has an effect in the base units. For example:
>>> q = 3600. * ureg.meter / ureg.hour
>>> q.to_base_units()
<Quantity(1.0, 'meter / second')>
We can take a look for the available systems
>>> ureg.systems
frozenset({'US', 'mks', 'imperial', 'cgs'})
But if you change to cgs:
>>> ureg.system = 'cgs'
>>> q.to_base_units()
<Quantity(100.0, 'centimeter / second')>
or more drastically to:
>>> ureg.system = 'imperial'
>>> '{:.3f}'.format(q.to_base_units())
'1.094 yard / second'
..warning: In versions previous to 0.7 `to_base_units` returns quantities in the
units of the definition files (which are called root units). For the definition file
bundled with pint this is meter/gram/second. To get back this behaviour use `to_root_units`,
set `ureg.system = None`
You can also use system to narrow down the list of compatible units:
>>> ureg.system = 'mks'
>>> ureg.get_compatible_units('meter')
frozenset({<Unit('light_year')>, <Unit('angstrom')>, <Unit('US_survey_mile')>, <Unit('yard')>, <Unit('US_survey_foot')>, <Unit('US_survey_yard')>, <Unit('inch')>, <Unit('rod')>, <Unit('mile')>, <Unit('barleycorn')>, <Unit('foot')>, <Unit('mil')>})
>>> ureg.system = 'imperial'
>>> ureg.get_compatible_units('meter')
frozenset({<Unit('US_survey_mile')>, <Unit('angstrom')>, <Unit('inch')>, <Unit('light_year')>, <Unit('barleycorn')>, <Unit('mile')>, <Unit('US_survey_foot')>, <Unit('rod')>, <Unit('US_survey_yard')>, <Unit('yard')>, <Unit('mil')>, <Unit('foot')>})
>>> ureg.imperial.pint
bla
>>> ureg.us.pint

View File

@@ -198,13 +198,13 @@ class _Quantity(SharedRegistryObject):
return not bool(tmp.dimensionality)
_dimensionality = None
@property
def dimensionality(self):
"""Quantity's dimensionality (e.g. {length: 1, time: -1})
"""
try:
return self._dimensionality
except AttributeError:
if self._dimensionality is None:
self._dimensionality = self._REGISTRY._get_dimensionality(self._units)
return self._dimensionality