andreacortis / SemanticQuantities

MIT License
1 stars 0 forks source link

unbound local error when trying to make a new quantity from existing quantity #7

Open rckwzrd opened 3 years ago

rckwzrd commented 3 years ago

In [1]: from quantities import quantitymaker, Limits, Q ...: from pint import DimensionalityError, UnitRegistry ...: from uncertainties import ufloat ...: from typeguard import typechecked ...: ...: import numpy as np

In [2]:

...: # MLT = bed_top - bed_bot = log thickness of bed in deviated hole ...: # TVT = MLTcos(psi) = true vertical thickness of bed from hole dip ...: # TST = TVTcos(delta) = true stratigraphic thickness of bed from bed dip ...: ...: MeasuredDepth = quantity_maker("MD", "ft") ...: Thickness = quantity_maker("Thickness", "ft") ...: ...: bedtop = MeasuredDepth(name=r"bed{top}", quantity=Q_("8000 ft")) ...: bedbot = MeasuredDepth(name=r"bed{bot}", quantity=Q("8050 ft")) ...: ...: # how to convert from MD to thickness? ...: mlt = bed_bot - bedtop ...: mlt = Thickness(name="MLT", quantity=mlt) ...: ...: display(bed_top) ...: display(bed_bot) ...: display(mlt) ...:

--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) ~/projects/SemanticQuantities/.quant/lib/python3.8/site-packages/IPython/core/formatters.py in __call__(self, obj) 700 type_pprinters=self.type_printers, 701 deferred_pprinters=self.deferred_printers) --> 702 printer.pretty(obj) 703 printer.flush() 704 return stream.getvalue() ~/projects/SemanticQuantities/.quant/lib/python3.8/site-packages/IPython/lib/pretty.py in pretty(self, obj) 392 if cls is not object \ 393 and callable(cls.__dict__.get('__repr__')): --> 394 return _repr_pprint(obj, self, cycle) 395 396 return _default_pprint(obj, self, cycle) ~/projects/SemanticQuantities/.quant/lib/python3.8/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle) 698 """A pprint that just redirects to the normal repr function.""" 699 # Find newlines and replace them with p.break_() --> 700 output = repr(obj) 701 lines = output.splitlines() 702 with p.group(): ~/projects/SemanticQuantities/quantities/quantities.py in __repr__(self) 168 169 def __repr__(self): --> 170 s = f"$${self.__str__()}$$" 171 display(Latex(s)) 172 return "" ~/projects/SemanticQuantities/quantities/quantities.py in __str__(self) 163 h = f"{self.value.m:4.{self.__significant_digits__}g}" 164 u = self.value.units --> 165 s = f"{self.name} = {h} \\, \\rm{{[{u}]}}, \\, \\rm{{({self.__class__.__name__})}}" 166 s = s.replace('+/-',r'\pm') 167 return s UnboundLocalError: local variable 'h' referenced before assignment
rckwzrd commented 3 years ago

keep getting this error when trying to build a thickness quantity from the difference between two bed markers. it is very possible that I am doing something incorrectly. the error is thrown when trying to display the mlt quantity.

rckwzrd commented 3 years ago

found a work around or correct approach. accessed the value attribute of the of mlt_ and passed as a f string to Q_ to build a thickness quantity.

image