AguaClara / aguaclara

An open-source Python package for designing and performing research on AguaClara water treatment plants.
https://aguaclara.github.io/aguaclara/
MIT License
24 stars 13 forks source link

sig fig function call only works once #146

Closed monroews closed 5 years ago

monroews commented 5 years ago

aguaclara.core.units has a function that allows setting the number of significant digits to print. If you call that function more than once it causes an error because that function includes reloading the unit registry. The unit registry load should not be part of the significant digit function.

Below is the error result on the second call to the function.

DefinitionSyntaxError Traceback (most recent call last)

in () ----> 1 aguaclara.core.units.set_sig_figs(2) C:\Users\mw24\Anaconda3\lib\site-packages\aguaclara\core\units.py in set_sig_figs(n) 56 57 unit_registry.load_definitions(os.path.join(os.path.dirname(__file__), ---> 58 "data", "unit_definitions.txt")) C:\Users\mw24\Anaconda3\lib\site-packages\pint\registry.py in load_definitions(self, file, is_resource) 363 if e.filename is None: 364 e.filename = file --> 365 raise e 366 except Exception as e: 367 msg = getattr(e, 'message', '') or str(e) C:\Users\mw24\Anaconda3\lib\site-packages\pint\registry.py in load_definitions(self, file, is_resource) 359 else: 360 with open(file, encoding='utf-8') as fp: --> 361 return self.load_definitions(fp, is_resource) 362 except (RedefinitionError, DefinitionSyntaxError) as e: 363 if e.filename is None: C:\Users\mw24\Anaconda3\lib\site-packages\pint\registry.py in load_definitions(self, file, is_resource) 401 if ex.lineno is None: 402 ex.lineno = no --> 403 raise ex 404 except Exception as ex: 405 logger.error("In line {0}, cannot add '{1}' {2}".format(no, line, ex)) C:\Users\mw24\Anaconda3\lib\site-packages\pint\registry.py in load_definitions(self, file, is_resource) 397 else: 398 try: --> 399 self.define(Definition.from_string(line)) 400 except DefinitionSyntaxError as ex: 401 if ex.lineno is None: C:\Users\mw24\Anaconda3\lib\site-packages\pint\registry.py in define(self, definition) 233 self._define(Definition.from_string(line)) 234 else: --> 235 self._define(definition) 236 237 def _define(self, definition): C:\Users\mw24\Anaconda3\lib\site-packages\pint\registry.py in _define(self, definition) 1341 # this adds all units to the `root` group. 1342 -> 1343 definition, d, di = super(SystemRegistry, self)._define(definition) 1344 1345 if isinstance(definition, UnitDefinition): C:\Users\mw24\Anaconda3\lib\site-packages\pint\registry.py in _define(self, definition) 889 """ 890 --> 891 definition, d, di = super(NonMultiplicativeRegistry, self)._define(definition) 892 893 # define additional units for units with an offset C:\Users\mw24\Anaconda3\lib\site-packages\pint\registry.py in _define(self, definition) 259 if dimension in self._dimensions: 260 if dimension != '[]': --> 261 raise DefinitionSyntaxError('only one unit per dimension can be a base unit.') 262 continue 263 DefinitionSyntaxError: While opening C:\Users\mw24\Anaconda3\lib\site-packages\aguaclara\core\data\unit_definitions.txt, in line 12: only one unit per dimension can be a base unit.
monroews commented 5 years ago

This error is related to another error. Something in the unit registry creation process has broken, and units.py cannot find unit_definitions.txt to import the additional units (NTU, USD, HNL).

This is because the only time that the unit_definitions.txt is called is INSIDE the function for setting the number of significant digits!

fletchapin commented 5 years ago

I fixed this in a branch called fletch/miscFixes which I made to address all these issues. I'm waiting to resolve a few more of them before creating the PR and merging.