Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_all_element_Chemicals():
things = [periodic_table.CAS_to_elements, periodic_table.name_to_elements, periodic_table.symbol_to_elements]
failed_CASs = []
for thing in things:
for i in thing.keys():
try:
Chemical(i)
except:
failed_CASs.append(periodic_table[i].name)
assert 0 == len(set(failed_CASs))
def test_periodic_table_variants():
'''Do a lookup in the periodic table and compare vs CAS_from_any.
'''
ids = [periodic_table.CAS_to_elements, periodic_table.name_to_elements, periodic_table.symbol_to_elements]
failed_CASs = []
for thing in ids:
for i in thing.keys():
try:
CAS_from_any(i)
except:
failed_CASs.append(periodic_table[i].name)
assert 0 == len(set(failed_CASs))
# Check only the 5 known diatomics have a diff case
failed_CASs = []
for thing in ids:
for i in thing.keys():
try:
assert CAS_from_any(i) == periodic_table[i].CAS
except:
ids = [periodic_table.CAS_to_elements, periodic_table.name_to_elements, periodic_table.symbol_to_elements]
failed_CASs = []
for thing in ids:
for i in thing.keys():
try:
CAS_from_any(i)
except:
failed_CASs.append(periodic_table[i].name)
assert 0 == len(set(failed_CASs))
# Check only the 5 known diatomics have a diff case
failed_CASs = []
for thing in ids:
for i in thing.keys():
try:
assert CAS_from_any(i) == periodic_table[i].CAS
except:
failed_CASs.append(periodic_table[i].name)
assert set(['Chlorine', 'Fluorine', 'Hydrogen', 'Nitrogen', 'Oxygen']) == set(failed_CASs)
for CAS, d in periodic_table.CAS_to_elements.items():
assert CAS_from_any(d.smiles) == CAS
for CAS, d in periodic_table.CAS_to_elements.items():
assert CAS_from_any('SMILES=' + d.smiles) == CAS
for CAS, d in periodic_table.CAS_to_elements.items():
assert CAS_from_any('InChI=1S/' + d.InChI) == CAS
for CAS, d in periodic_table.CAS_to_elements.items():
assert CAS_from_any('InChIKey=' + d.InChI_key) == CAS
def test_misc_elements():
assert periodic_table['H'].InChI == 'H' # 'InChI=1S/
assert periodic_table['H'].smiles == '[H]'
assert periodic_table[1].smiles == '[H]'
assert periodic_table['1'].smiles == '[H]'
assert periodic_table['Fm'].InChI_key == 'MIORUQGGZCBUGO-UHFFFAOYSA-N'
def test_elements_data():
tots_calc = [sum([getattr(i, att) for i in periodic_table if not getattr(i, att) is None]) for att in
['number', 'MW', 'period', 'group', 'AReneg', 'rcov', 'rvdw', 'maxbonds', 'elneg', 'ionization', 'elaffinity', 'electrons', 'protons']]
tots_exp = [7021, 17285.2137652, 620, 895, 109.91, 144.3100000000001, 179.4300000000001, 94, 163.27000000000007, 816.4238999999999, 67.50297235000001, 7021, 7021]
assert_allclose(tots_calc, tots_exp)
def load_elements(self):
if not self.elements:
return None
for ele in periodic_table:
CAS = int(ele.CAS.replace('-', '')) # Store as int for easier lookup
all_names = [ele.name.lower()]
obj = ChemicalMetadata(pubchemid=ele.PubChem, CAS=CAS,
formula=ele.symbol, MW=ele.MW, smiles=ele.smiles,
InChI=ele.InChI, InChI_key=ele.InChI_key,
iupac_name=ele.name.lower(),
common_name=ele.name.lower(),
all_names=all_names)
if self.create_InChI_key_index:
if ele.InChI_key in self.InChI_key_index:
if ele.number not in homonuclear_elemental_gases:
obj_old = self.InChI_key_index[ele.InChI_key]
'124-18-5'
>>> CAS_from_any('InChIKey=LFQSCWFLJHTTHZ-UHFFFAOYSA-N')
'64-17-5'
>>> CAS_from_any('pubchem=702')
'64-17-5'
>>> CAS_from_any('O') # only elements can be specified by symbol
'17778-80-2'
'''
ID = ID.strip()
ID_lower = ID.lower()
if ID in periodic_table:
if periodic_table[ID].number not in homonuclear_elemental_gases:
return periodic_table[ID].CAS
else:
for i in [periodic_table.symbol_to_elements,
periodic_table.number_to_elements,
periodic_table.CAS_to_elements]:
if i == periodic_table.number_to_elements:
if int(ID in i):
return periodic_table[int(ID)].CAS
else:
if ID in i:
return periodic_table[ID].CAS
if checkCAS(ID):
CAS_lookup = pubchem_db.search_CAS(ID, autoload)
if CAS_lookup:
return CAS_lookup.CASs
# handle the case of synonyms
CAS_alternate_loopup = pubchem_db.search_name(ID, autoload)
>>> CAS_from_any('InChIKey=LFQSCWFLJHTTHZ-UHFFFAOYSA-N')
'64-17-5'
>>> CAS_from_any('pubchem=702')
'64-17-5'
>>> CAS_from_any('O') # only elements can be specified by symbol
'17778-80-2'
'''
ID = ID.strip()
ID_lower = ID.lower()
if ID in periodic_table:
if periodic_table[ID].number not in homonuclear_elemental_gases:
return periodic_table[ID].CAS
else:
for i in [periodic_table.symbol_to_elements,
periodic_table.number_to_elements,
periodic_table.CAS_to_elements]:
if i == periodic_table.number_to_elements:
if int(ID in i):
return periodic_table[int(ID)].CAS
else:
if ID in i:
return periodic_table[ID].CAS
if checkCAS(ID):
CAS_lookup = pubchem_db.search_CAS(ID, autoload)
if CAS_lookup:
return CAS_lookup.CASs
# handle the case of synonyms
CAS_alternate_loopup = pubchem_db.search_name(ID, autoload)
if CAS_alternate_loopup: