@@ -558,15 +558,16 @@ def get_sym_dict(form: str, factor: float) -> dict[str, float]:
558
558
raise ValueError (f"{ form } is an invalid formula!" )
559
559
return sym_dict
560
560
561
- m = re .search (r"\(([^\(\)]+)\)\s*([\.e\d]*)" , formula )
562
- if m :
561
+ match = re .search (r"\(([^\(\)]+)\)\s*([\.e\d]*)" , formula )
562
+ while match :
563
563
factor = 1.0
564
- if m .group (2 ) != "" :
565
- factor = float (m .group (2 ))
566
- unit_sym_dict = get_sym_dict (m .group (1 ), factor )
564
+ if match .group (2 ) != "" :
565
+ factor = float (match .group (2 ))
566
+ unit_sym_dict = get_sym_dict (match .group (1 ), factor )
567
567
expanded_sym = "" .join (f"{ el } { amt } " for el , amt in unit_sym_dict .items ())
568
- expanded_formula = formula .replace (m .group (), expanded_sym )
569
- return self ._parse_formula (expanded_formula )
568
+ expanded_formula = formula .replace (match .group (), expanded_sym , 1 )
569
+ formula = expanded_formula
570
+ match = re .search (r"\(([^\(\)]+)\)\s*([\.e\d]*)" , formula )
570
571
return get_sym_dict (formula , 1 )
571
572
572
573
@property
@@ -581,14 +582,14 @@ def anonymized_formula(self) -> str:
581
582
reduced /= gcd (* (int (i ) for i in self .values ()))
582
583
583
584
anon = ""
584
- for e , amt in zip (string .ascii_uppercase , sorted (reduced .values ())):
585
+ for elem , amt in zip (string .ascii_uppercase , sorted (reduced .values ())):
585
586
if amt == 1 :
586
587
amt_str = ""
587
588
elif abs (amt % 1 ) < 1e-8 :
588
589
amt_str = str (int (amt ))
589
590
else :
590
591
amt_str = str (amt )
591
- anon += f"{ e } { amt_str } "
592
+ anon += f"{ elem } { amt_str } "
592
593
return anon
593
594
594
595
@property
0 commit comments