Skip to content

Commit

Permalink
Refactor #40: Function for unit replaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jon85p committed Jan 26, 2021
1 parent 076d5fa commit 5eea78d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
8 changes: 2 additions & 6 deletions entrada.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
sys.path.append(pyENL_dirpath)
from solver import solver
from utils import variables, random_lim, variables_string
from utils import ajustaUnidades
from numpy import inf
from CoolProp.CoolProp import PropsSI as prop
from CoolProp.CoolProp import HAPropsSI as haprop
from re import sub
from time import time
import optparse
from pint import _DEFAULT_REGISTRY as pyENLu
Expand Down Expand Up @@ -98,11 +98,7 @@ def entradaTexto(ecuaciones, pyENL_timeout, varsObj=None, tol=None, method='hybr
expresion = expresion.replace('\t','')
# Capacidad de interpretar pow
expresion = expresion.replace("^", "**")
if "hour" not in expresion:
expresion = sub(r'\[([A-z0-9\*{1,2}\/\^)]*)h([A-z0-9\*{1,2}\/\^)]*)]',
'[\g<1>hour\g<2>]', expresion)
expresion = sub(r'\[([A-z0-9\*{1,2}\/\^)]*)°C([A-z0-9\*{1,2}\/\^)]*)]',
'[\g<1>degree_Celsius\g<2>]', expresion)
expresion = ajustaUnidades(expresion)
izq_der = expresion.split('=')
operandos = ["+", "-", "*", "/", "("]
# Revisar que no haya operadores incompletos
Expand Down
7 changes: 1 addition & 6 deletions pyENL.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
pyENLu.load_definitions(pyENL_units_file)
from CoolProp.CoolProp import FluidsList, get_parameter_index, get_parameter_information, is_trivial_parameter
from pyENL_fcns.functions import dicc_coolprop
from re import sub
# Cargar ahora interfaz desde archivo .py haciendo conversión con:
# $ pyuic4 GUI/MainWindow.ui -o GUI/MainWindow.py
# Icono: QtWidgets.QPixmap(_fromUtf8("GUI/imgs/icon.ico")
Expand Down Expand Up @@ -858,11 +857,7 @@ def actualizaVarsTable(self):
lowerlim = float(self.varsTable.item(i, 2).text())
upperlim = float(self.varsTable.item(i, 3).text())
units = self.varsTable.item(i, 4).text()
if not "hour" in units:
units = sub(r'([A-z0-9\*{1,2}\/\^)]*)h([A-z0-9\*{1,2}\/\^)]*)',
'\g<1>hour\g<2>', units)
units = sub(r'([A-z0-9\*{1,2}\/\^)]*)°C([A-z0-9\*{1,2}\/\^)]*)',
'\g<1>degree_Celsius\g<2>', units)
units = ajustaUnidades(units)
comment = self.varsTable.item(i, 5).text()
if lowerlim >= upperlim:
raise Exception(self.traduccion['El número '] + str(lowerlim) +
Expand Down
16 changes: 15 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ def variablesProperties(texto_eqn,vars_ecuacion,posibles):
if variable_unique in terminos_eqn:
to_eval = texto_eqn.replace(variable_unique,'')
to_eval =to_eval.replace("[", "*pyENLu.parse_units('").replace("]", "')")
to_eval = ajustaUnidades(to_eval)
result_var = eval(to_eval)

dic_vars = {variable_unique: {}}
Expand All @@ -559,4 +560,17 @@ def variablesProperties(texto_eqn,vars_ecuacion,posibles):
else:
dic_vars[variable_unique]['guess'] = - result_var

return dic_vars
return dic_vars

def ajustaUnidades(texto):
'''
Regresa el texto con unidades modificadas para soporte de
pyENL, como h para hour y °C para degree_Celsius
'''
salida = texto
if 'hour' not in salida:
salida = re.sub(r'([A-z0-9\*{1,2}\/\^)]*)h([A-z0-9\*{1,2}\/\^)]*)',
'\g<1>hour\g<2>', salida)
salida = re.sub(r'([A-z0-9\*{1,2}\/\^)]*)°C([A-z0-9\*{1,2}\/\^)]*)',
'\g<1>degree_Celsius\g<2>', salida)
return salida

0 comments on commit 5eea78d

Please sign in to comment.