def test_is_valid_formula_1(self): input = '(NH3)2' result = cf.is_valid_formula(input) expected = True self.assertEqual(result, expected)
def test_is_valid_formula_14(self): input = '(3)Na+' result = cf.is_valid_formula(input) expected = False self.assertEqual(result, expected)
def test_is_valid_formula_16(self): input = 'CH3C(O)CH3' result = cf.is_valid_formula(input) expected = True self.assertEqual(result, expected)
def isValidate_molecular_formula(value): from pyEQL.chemical_formula import is_valid_formula if not is_valid_formula(value): return False return True
def test_is_valid_formula_12(self): input = 'Mg)(OH)2' result = cf.is_valid_formula(input) expected = False self.assertEqual(result, expected)
def validate_molecular_formula(value): if not is_valid_formula(value): raise ValidationError(_('%(value)s is not a valid molecular formula'), params={'value': value})
def search_parameters(self,formula): '''Each time a new solute species is created in a solution, this function: 1) searches to see whether a list of parameters for the species has already been compiled from the database 2) searches all files in the specified database directory(ies) for the species 3) creates a Parameter object for each value found 4) compiles these objects into a set 5) adds the set to a dictionary indexed by species name (formula) 6) points the new solute object to the dictionary formula : str String representing the chemical formula of the species. ''' # get the hill_order() and is_valid_formula() methods from the chemistry module import pyEQL.chemical_formula as chem # if the formula is already in the database, then we've already searched # and compiled parameters, so there is no need to do it again. if formula in self.parameters_database: pass else: # add an entry to the parameters database self.parameters_database[formula] = set() # search all the files in each database directory for directory in self.database_dir: for file in os.listdir(directory): # reset the line number count line_num = 0 # ignore the template file if file == 'template.tsv': continue # look at only .csv files if ".tsv" in file: # open each file current_file = open(directory+'/'+file,'r') # read each line of the file, looking for the formula try: for line in current_file: line_num += 1 try: # look for keywords in the first column of each file. If found, # store the entry from the 2nd column if 'Name' in line: param_name = _parse_line(line)[1] elif 'Description' in line: param_desc = _parse_line(line)[1] elif 'Unit' in line: param_unit = _parse_line(line)[1] elif 'Reference' in line: param_ref = _parse_line(line)[1] elif 'Temperature' in line: param_temp = _parse_line(line)[1] elif 'Pressure' in line: param_press = _parse_line(line)[1] elif 'Ionic Strength' in line: param_ionic = _parse_line(line)[1] elif 'Comment' in line: param_comment = _parse_line(line)[1] # use the hill_order() function to standardize the # supplied formula. Then standardize teh formulas in the # database and see if they match. # this allows a database entry for 'MgCl2' to be matched # even if the user enters 'Mg(Cl)2', for example elif chem.is_valid_formula(_parse_line(line)[0]): if chem.hill_order(formula) == chem.hill_order(_parse_line(line)[0]): # if there are multiple columns, pass the values as a list. # If a single column, then just pass the value if len(_parse_line(line)) >2: param_value = _parse_line(line)[1:] else: param_value = _parse_line(line)[1] # Create a new parameter object parameter = pm.Parameter(param_name,param_value,param_unit, \ reference=param_ref,pressure=param_press,temperature=param_temp,ionic_strength=param_ionic,description=param_desc,comment=param_comment) # Add the parameter to the set for this species self.parameters_database[formula].add(parameter) except ValueError: logger.warning('Error encountered when reading line %s in %s' % (line_num,file)) continue # log a warning if an invalid character prevents reading a line except UnicodeDecodeError: logger.warning('Invalid character found when reading %s. File skipped.' % file) current_file.close()
def test_is_valid_formula_9(self): input = "NaOH" result = cf.is_valid_formula(input) expected = True self.assertEqual(result, expected)
def test_is_valid_formula_15(self): input = "()" result = cf.is_valid_formula(input) expected = False self.assertEqual(result, expected)
def test_is_valid_formula_3(self): input = 'Na^+' result = cf.is_valid_formula(input) expected = False self.assertEqual(result,expected)
def isValidate_molecular_formula(value): if not is_valid_formula(value): return False return True
def test_is_valid_formula_1(self): input = '(NH3)2' result = cf.is_valid_formula(input) expected = True self.assertEqual(result,expected)
def test_is_valid_formula_16(self): input = 'CH3C(O)CH3' result = cf.is_valid_formula(input) expected = True self.assertEqual(result,expected)
def test_is_valid_formula_12(self): input = 'Mg)(OH)2' result = cf.is_valid_formula(input) expected = False self.assertEqual(result,expected)
def test_is_valid_formula_2(self): input = '3CO3-' result = cf.is_valid_formula(input) expected = False self.assertEqual(result, expected)
def test_is_valid_formula_17(self): input = "CH3(CH)2(CH)2" result = cf.is_valid_formula(input) expected = True self.assertEqual(result, expected)
def search_parameters(self, formula): '''Each time a new solute species is created in a solution, this function: 1) searches to see whether a list of parameters for the species has already been compiled from the database 2) searches all files in the specified database directory(ies) for the species 3) creates a Parameter object for each value found 4) compiles these objects into a set 5) adds the set to a dictionary indexed by species name (formula) 6) points the new solute object to the dictionary formula : str String representing the chemical formula of the species. ''' # get the hill_order() and is_valid_formula() methods from the chemistry module import pyEQL.chemical_formula as chem # if the formula is already in the database, then we've already searched # and compiled parameters, so there is no need to do it again. if formula in self.parameters_database: pass else: # add an entry to the parameters database self.parameters_database[formula] = set() # search all the files in each database directory for directory in self.database_dir: for file in os.listdir(directory): # reset the line number count line_num = 0 # ignore the template file if file == 'template.tsv': continue # look at only .csv files if ".tsv" in file: # open each file current_file = open(directory + '/' + file, 'r') # read each line of the file, looking for the formula try: for line in current_file: line_num += 1 try: # look for keywords in the first column of each file. If found, # store the entry from the 2nd column if 'Name' in line: param_name = _parse_line(line)[1] elif 'Description' in line: param_desc = _parse_line(line)[1] elif 'Unit' in line: param_unit = _parse_line(line)[1] elif 'Reference' in line: param_ref = _parse_line(line)[1] elif 'Temperature' in line: param_temp = _parse_line(line)[1] elif 'Pressure' in line: param_press = _parse_line(line)[1] elif 'Ionic Strength' in line: param_ionic = _parse_line(line)[1] elif 'Comment' in line: param_comment = _parse_line(line)[1] # use the hill_order() function to standardize the # supplied formula. Then standardize teh formulas in the # database and see if they match. # this allows a database entry for 'MgCl2' to be matched # even if the user enters 'Mg(Cl)2', for example elif chem.is_valid_formula( _parse_line(line)[0]): if chem.hill_order( formula) == chem.hill_order( _parse_line(line)[0]): # if there are multiple columns, pass the values as a list. # If a single column, then just pass the value if len(_parse_line(line)) > 2: param_value = _parse_line( line)[1:] else: param_value = _parse_line( line)[1] # Create a new parameter object parameter = pm.Parameter(param_name,param_value,param_unit, \ reference=param_ref,pressure=param_press,temperature=param_temp,ionic_strength=param_ionic,description=param_desc,comment=param_comment) # Add the parameter to the set for this species self.parameters_database[ formula].add(parameter) except ValueError: logger.warning( 'Error encountered when reading line %s in %s' % (line_num, file)) continue # log a warning if an invalid character prevents reading a line except UnicodeDecodeError: logger.warning( 'Invalid character found when reading %s. File skipped.' % file) current_file.close()
def isValidate_molecular_formula(value): if not is_valid_formula(value): return False return True;