python-quantities/python-quantities — Issue #37: dollar sign ("$") cannot be used as a unit symbol
Author: multicore
Created: 2012-02-10 18:44:31+00:00
State: open

The dollar sign ("$") cannot be used as a unit symbol with the quantities package. For example, the following code fails:

import quantities as pq
unitsDollars = pq.UnitQuantity('dollars', 1, '$')
unitsDollars = pq.UnitQuantity('$', 1, '$')
x = pq.Quantity(5, '$')

Traceback (most recent call last):
File "\", line 1, in \
File "C:\Python27\lib\site-packages\quantities\quantity.py", line 127, in \__new__
ret._dimensionality.update(validate_dimensionality(units))
File "C:\Python27\lib\site-packages\quantities\quantity.py", line 34, in validate_dimensionality
return unit_registry[value].dimensionality
File "C:\Python27\lib\site-packages\quantities\registry.py", line 61, in \__getitem__
return self.\__registry[label]
File "C:\Python27\lib\site-packages\quantities\registry.py", line 21, in \__getitem__
return eval(string, self.__context)
File "\", line 1
$
^
SyntaxError: unexpected EOF while parsing

The problem appears to occur because the method ``quantities.registry.UnitRegistry.__Registry.__getitem__(self, string)`` makes a call to Python's ``eval()`` method as ``eval(string, self.__context)``. For simple units, this can simply be done as a dictionary lookup in ``self.__context``. However, this won't work for compound units. Maybe a simple ``if`` check could be added to see if the symbol is in ``self.__context`` before calling ``eval()``. This would at least support the dollar sign as a simple unit. Even though this will still fail for compound units, at least it provides partial functionality.