def is_exchange(r_out: 'list(Compound)') -> (bool): if Compound("H2O") in r_out: # water return True elif find_if(r_out, lambda c: is_gase(c)) != -1: # gases return True elif find_if(r_out, lambda c: not is_ionic_soluble(c)) != -1: # insoluble return True else: return False
def _iSaAc_oxs(salt): # MeHAn val = salt.formula.value is_dig_up_br = lambda c: c.isdigit() or c.isupper() or c == "(" me: str me_q: int me_oxs: int h_q: int anion: str an_q: int an_oxs: int me_end_pos = find_if(val[1:], is_dig_up_br) me = val[0:me_end_pos + 1] consist = deepcopy(salt.formula.consist) me_q = consist[Element(me)] del consist[Element(me)] h_q = consist[Element("H")] del consist[Element("H")] anh_beg_pos = find_if(val[1:], lambda c: c.isupper()) + 1 anh_end_pos = find_if(val[anh_beg_pos:], lambda c: c == ')') + anh_beg_pos if anh_end_pos == anh_beg_pos - 1: anh_end_pos = len(val) anionh = val[anh_beg_pos:anh_end_pos] # HAn an_beg_pos = find_if(anionh[1:], lambda c: c.isupper()) + 1 an_end_pos = find_if(anionh[an_beg_pos:], lambda c: c == ')') + an_beg_pos if an_end_pos == an_beg_pos - 1: an_end_pos = len(val) anion = anionh[an_beg_pos:an_end_pos] # An an = Compound(anion) if len(an.formula.consist) == 1: # MeNm anion = list(an.formula.consist.keys())[0].name an = Compound(anion) nonme = list(consist.keys())[0] an_q = int(consist[nonme] / an.formula.consist[nonme]) if anion in db_anions: an_oxs = db_anions[anion] me_oxs = (an_oxs * an_q - h_q) / me_q elif me in db_oxs and db_oxs[me] != [0]: me_oxs = get_me_oxs(me) an_oxs = (me_oxs * me_q + h_q) / an_q else: me_oxs = 1 an_oxs = (me_oxs * me_q + h_q / an_q) anion = "H" + index(_h_q) + anion return ((me, int(me_oxs)), (anion, int(an_oxs)))
def multiply(self, par: str, mult: str): iter_value = par[1 : -1] new_value = "" while iter_value: num_pos = find_if(iter_value, lambda c: c.isdigit()) bef_num, num_value = iter_value[0 : num_pos], iter_value[num_pos : ] num_end = -1 for i in range(len(num_value)): if num_value[i].isdigit(): num_end += 1 else: break if num_end == len(num_value): num_value = str(int(num_value)) new_value += bef_num + num_value break iter_value = num_value[num_end + 1 : ] num_value = num_value[0 : num_end + 1] num_value = str(int(num_value) * int(mult)) new_value += bef_num + num_value return new_value
def iAc_oxs(comp: 'Formula') -> (str, int): val = comp.value.replace('[', "").replace(']', "") consist = deepcopy(comp.consist) # ElAn an_oxs = consist[Element("H")] end = val[1:] anion = "" anion = end[find_if(end, lambda x: x.isupper()):] if '[' in comp.value: anion = '[' + anion + ']' return (anion, int(an_oxs))
def separate(self): iter_value = self.ext_value new_consist = dict() while iter_value: up_pos = find_if(iter_value[1 : ], lambda c: c.isupper()) + 1 sub_str = iter_value[0 : up_pos] if up_pos == 0: sub_str = iter_value num_pos = find_if(sub_str, lambda c: c.isdigit()) el_value = sub_str[0 : num_pos] num_value = int(sub_str[num_pos : ]) element = Element(el_value) new_consist[element] = new_consist.get(element, 0) + num_value if up_pos == 0: break iter_value = iter_value[up_pos : ] return new_consist
def open_brackets(self, value): iter_value = value new_value = "" while iter_value: par_pos = iter_value.find('(') bef_par, par_value = iter_value[0 : par_pos], iter_value[par_pos : ] if par_pos == -1: new_value += iter_value break brackets = 0 par_end = 0 for i in range(len(par_value)): if par_value[i] == '(': brackets += 1 elif par_value[i] == ')': brackets -= 1 if brackets == 0: par_end = i break num_value = par_value[par_end + 1 : ] par_value = par_value[0 : par_end + 1] num_end = -1 for i in range(len(num_value)): if num_value[i].isdigit(): num_end += 1 else: break iter_value = num_value[num_end + 1 : ] num_value = num_value[0 : num_end + 1] have_par = find_if(par_value[1 : -1], lambda c: c in "()") if have_par != -1: par_value = "(" + self.open_brackets(par_value[1 : -1]) + ")" new_par_value = self.multiply(par_value, num_value) new_value += bef_par + new_par_value return new_value
def add1(self, value): iter_value = value new_value = "" while iter_value: is_up = lambda c: c.isupper() or (c in "()") up_pos = find_if(iter_value[1 : ], is_up) + 1 sub_str = "" if up_pos == 0: sub_str = iter_value else: sub_str = iter_value[0 : up_pos] if not sub_str[-1].isdigit() and sub_str[-1] != '(': sub_str += "1" new_value += sub_str if up_pos == 0: break iter_value = iter_value[up_pos : ] return new_value
def iSaCo_in(sph: str): is_up = lambda c: c.isupper() or c == "(" # central atom me_end = find_if(sph[1:], is_up) + 1 me = sph[0:me_end] me_oxs = get_me_oxs(me) if me_oxs == 0: me_oxs == 1 sph = sph.replace(me, "") # neutral molecules neu = "" neu_q = 0 if "(H2O)" in sph: neu = "H2O" elif "(NH3)" in sph: neu = "NH3" sph = sph.replace(neu, "") if neu != "": neu_end = sph.find("()") + 2 neu_qend = find_if(sph[neu_end], is_up) if neu_qend == -1: neu_qend = len(sph) else: neu_qend += neu_end neu_q = sph[neu_end:neu_qend] if neu_q == "": neu_q = 1 else: neu_q = int(neu_q) sph = sph[0:neu_end - 2] + sph[neu_qend:] hoh = "" hoh_q = 0 if "(OH)" in sph: hoh = "OH" elif "H" in sph: hoh = "H" sph = sph.replace("OH", "").replace("H", "()") if hoh != "": hoh_end = sph.find("()") + 2 hoh_qend = find_if(sph[hoh_end], is_up) if hoh_qend == -1: hoh_qend = len(sph) else: hoh_qend += hoh_end hoh_q = sph[hoh_end:hoh_qend] if hoh_q == "": hoh_q = 1 else: hoh_q = int(hoh_q) sph = sph[0:hoh_end - 2] + sph[hoh_qend:] # anions an = "" an_oxs = 0 an_q = 0 if len(sph) != 0 and sph[0] != '(': if count_if(sph, lambda c: c.isupper()) > 1: an = sph if an in db_anions: an_oxs = db_anions[an] else: an_oxs = 1 an_q = 1 else: an_end = find_if(sph, lambda c: c.isdigit()) if an_end == -1: an_end = len(sph) an = sph[0:an_end] if an in db_anions: an_oxs = db_anions[an] else: an_oxs = 1 an_q = sph[an_end:] if an_q == "": an_q = 1 else: an_q = int(an_q) elif len(sph) != 0: an_end = sph.find(')') an = sph[1:an_end] if an in db_anions: an_oxs = db_anions[an] else: an_oxs = 1 an_q = sph[an_end + 1:] if an_q == "": an_q = 1 else: an_q = int(an_q) if an == "": an = None if hoh == "": hoh = None if neu == "": neu = None cxs = [(me, me_oxs, 1), (an, -an_oxs, an_q), (hoh, -1, hoh_q), (neu, 0, neu_q)] return cxs
def _iSaBa_oxs(salt): # MeOHAn val = salt.formula.value is_dig_up_br = lambda c: c.isdigit() or c.isupper() or c == "(" me: str me_q: int me_oxs: int oh_q: int anion: str an_q: int an_oxs: int if val[0] != '(': # MeOHAn me_end_pos = find_if(val[1:], is_dig_up_br) + 1 me = val[0:me_end_pos] else: # (MeOH)An me_end_pos = find_if(val[2:], is_dig_up_br) + 2 me = val[1:me_end_pos] oh = val.find("OH") oh_end_pos = find_if(val[oh:], is_dig_up_br) + 1 consist = deepcopy(salt.formula.consist) me_q = consist[Element(me)] del consist[Element(me)] oh_q = consist[Element("H")] del consist[Element("H")] an_cont = val[oh + oh_end_pos:] # )An an_beg_pos = find_if(an_cont[1:], lambda c: c.isupper()) + 1 an_end_pos = find_if(an_cont[an_beg_pos : ], \ lambda c: c == ')') + an_beg_pos if an_end_pos == an_beg_pos - 1: an_end_pos = len(an_cont) anion = an_cont[an_beg_pos:an_end_pos] # An an = Compound(anion) if len(an.formula.consist) == 1: # MeNm anion = list(an.formula.consist.keys())[0].name an = Compound(anion) nonme = Element(anion) elif Element("O") in an.formula.consist: # MeNmO an_cons = deepcopy(an.formula.consist) del an_cons[Element("O")] nonme = list(an_cons.keys())[0] else: # MeNmNm nonme = list(consist.keys())[0] an_q = int(consist[nonme] / an.formula.consist[nonme]) if anion in db_anions: an_oxs = db_anions[anion] me_oxs = (an_oxs * an_q + oh_q) / me_q elif me in db_oxs and db_oxs[me] != [0]: me_oxs = get_me_oxs(me) an_oxs = (me_oxs * me_q - oh_q) / an_q else: me_oxs = 1 an_oxs = (me_oxs * me_q - oh_q) / an_q return ((me, int(me_oxs)), (anion, int(an_oxs)))
def reacts(cs: 'list(str)', is_input): functs = load_functs() cs = sorted(set(cs)) comps = list(map(lambda c: Compound(c), cs)) for (x, cond) in db_reacts: (r_input, r_output) = x.split("->", 2) if is_input: react = True for c in comps: val: str if c.comp_type[0] == "o": val = c.skeleton.value else: val = c.formula.value if f"`{val}`" not in r_input: react = False break else: r_input = r_input.replace(f"`{val}`", "", 1) else: react = True for c in comps: val: str if c.comp_type[0] == "o": val = c.skeleton.value else: val = c.formula.value if f"`{val}`" not in r_output: react = False break else: r_output = r_output.replace(f"`{val}`", "", 1) if not react: continue r = Reaction(x.replace("`", ""), cond) yield r comp_types = set([ "iOxAlk", "iOxBa", "iOxAm", "iOxAc", "iOxNs", "iBaAlk", "iBaBa", "iBaAm", "iBaCo" "iAcNox", "iAcOx", "iAcCo", "iSaNo", "iSaAc", "iSaBa", "iSaAmm", "iSaAmac", "iSaCo", "iSaPer", "iSiMe", "iSiNme", "iBi", "iWa", "oH*", "oHAa", "oHAe", "oHAy", "oHDi", "oHCa", "oHAr", "oO*", "oOAc", "oOAlc", "oOEth", "oOAld", "oOKet", "oN*", "oNAc", "oNAm", "oNNi", "oBioHy", "oBioLi", "oHal", "oP*", "oS*", "oHe*", "nil" ]) is_space = lambda c: c in " ,)" for f in functs: (r_input, r_output) = f.__doc__.split("->", 2) # analyse documentation string if is_input: react = True for c in comps: c_t = c.comp_type val: str if c_t[0] == "o": val = c.skeleton.value else: val = c.formula.value if f"`{val}`" not in r_input: # formula if f"`{c_t}" not in r_input: # type react = False break else: if f"`{c_t}_" in r_input: temp = r_input type_str = temp.find(f"`{c_t}") temp = temp.replace(f"`{c_t}", "", 1) sp_str = find_if(temp[type_str:], is_space) sp_q = sp_str sp = "_" * sp_q for i in comp_types: r_input = r_input.replace(f"`{i}{sp},", "") r_input = r_input.replace(f"`{i}{sp})", "") for i in comps: v = i.formula.value r_input = r_input.replace(f"`{v}`{sp},", "") r_input = r_input.replace(f"`{v}`{sp})", "") else: r_input = r_input.replace(f"`{c_t}", "", 1) else: if True: if f"`{val}`_" in r_input: temp = r_input type_str = temp.find(f"`{val}`") temp = temp.replace(f"`{val}`", "", 1) sp_str = find_if(temp[type_str:], is_space) sp_q = sp_str sp = "_" * sp_q for i in comp_types: r_input = r_input.replace(f"`{i}{sp},", "") r_input = r_input.replace(f"`{i}{sp})", "") for i in comps: v = i.formula.value r_input = r_input.replace(f"`{v}`{sp},", "") r_input = r_input.replace(f"`{v}`{sp})", "") else: r_input = r_input.replace(f"`{val}`", "", 1) else: react = True for c in comps: c_t = c.comp_type val: str if c_t[0] == "o": val = c.skeleton.value else: val = c.formula.value if f"`{val}`" not in r_output: # formula if f"`{c_t}" not in r_output: # type react = False break else: if f"`{c_t}_" in r_output: temp = r_output type_str = temp.find(f"`{c_t}") temp = temp.replace(f"`{c_t}", "", 1) sp_str = find_if(temp[type_str:], is_space) sp_q = sp_str sp = "_" * sp_q for i in comp_types: r_output = r_output.replace(f"`{i}{sp},", "") r_output = r_output.replace(f"`{i}{sp})", "") for i in comps: v = i.formula.value r_output = r_output.replace(f"`{v}`{sp},", "") r_output = r_output.replace(f"`{v}`{sp})", "") else: r_output = r_output.replace(f"`{c_t}", "", 1) else: if True: if f"`{val}`_" in r_output: temp = r_output type_str = temp.find(f"`{val}`") temp = temp.replace(f"`{val}`", "", 1) sp_str = find_if(temp[type_str:], is_space) sp_q = sp_str sp = "_" * sp_q for i in comp_types: r_output = r_output.replace(f"`{i}{sp},", "") r_output = r_output.replace(f"`{i}{sp})", "") for i in comps: v = i.formula.value r_output = r_output.replace(f"`{v}`{sp},", "") r_output = r_output.replace(f"`{v}`{sp})", "") else: r_output = r_output.replace(f"`{val}`", "", 1) if not react: continue r = f(comps, is_input) if type(r) == list: for i in r: if i != "" and i.value != "": yield i elif r != "" and r.value != "": yield r for c in comps: for uns, prod in db_unstable.items(): if c.formula.value in prod: _cs = [x for x in cs if x != c.formula.value] _cs.append(uns) rs = reacts(_cs, is_input) for r in rs: yield r