Main Contenders
Looking at doing calculations with units? Let's see what's out there. Start by doing a quick Google search with Python + Units. The first site that looks like a match is Python Units.Python units 0.06
- Last updated: 2013-2-25
- Download: PyPI
- Documentation: None [1]
- Repository: Bitbucket
- Last commit: 2013-02-24
- Owner: Aran Donohue
Python quantities 0.10.1
- Last updated: 2011-09-27
- Download: PyPI
- Dependencies: NumPy
- Documentation: Sphinx @ pythonhoster
- Repository: Github
- Last commit: 2013-04-16
- Owner: Darren Dale
Python Pint 0.2
- Last updated: 2013-05-13
- Download: PyPI
- Documentation: Sphinx @ readthedocs
- Repository: Github
- Last commit: 2013-05-22
- Owner: Hernan Grecco
Python numericalunits 1.11
- Last updated: 2013-02-21
- Download: PyPI
- Documentation: None [1]
- Repository: Github
- Last commit: 2013-02-22
- Owner: Steve Byrnes
One package that was really hard to find, only saw it in a
SO post
was Unum.
Python Unum 4.1.1
- Last updated: 2010-06-19
- Download: PyPI
- Documentation: linked to from here
- Repository: Bitbucket
- Last commit: 2012-03-25
- Owners: Chris MacLeod, Pierre Denis
Others
There are probably several others, but I think these are the main contenders. I found some by using search within PyPI, eg: magnitude-0.9.1 (c. 2007). Several are listed in this SO question including buckingham.py. Finally, DimPy (c. 2008) just randomly appeared way down the list when I Googled how to add a new unit to quantities, which is possible, but not well documented.>>> US_cent = pq.UnitCurrency('cent', 1, u_symbol=u'¢') >>> US_dollar = pq.UnitCurrency('dollar', 100 * US_cent, 'cent', u_symbol=u'$') >>> cost = 10 * US_cent / pq.kWh >>> print cost
SciPy.constants
I think it's important to note that SciPy does have many physical constants and conversion-factors to SI units. In fact it's a bit disappointing to see such a flagrant violation of the DRY principle with numerous physical constants and CODATA files floating around. But SciPy does not really have a good representation of units and a framework for using units in calculations.
Usage
Most of the packages are the same, multiplication by the units, creates a new class instance of the units. Here a snippet from Pint's documentation:>>> distance = 24.0 * ureg.meter >>> print(distance) 24.0 meter >>> time = 8.0 * ureg.second >>> print(time) 8.0 second >>> speed = distance / time >>> print(speed) 3.0 meter / secondThe exception to this pattern is Python-units which uses a call to create objects.
>>> meters = unit('m') >>> distance = meters(10)Python-quantities is the only package with dependencies; it depends on NumPy, which really doesn't matter to me. Pint also supports NumPy arrays, which is important.
Thanks for posting this, Keep Sharing good content.
ReplyDeletePython Online Training