@@ -151,13 +151,7 @@ def __iter__(self) -> Iterator[Species | Element | DummySpecies]:
151
151
def __contains__ (self , key ) -> bool :
152
152
try :
153
153
sp = get_el_sp (key )
154
- # First check for species
155
- if sp in self ._data :
156
- return True
157
- # If not found, check for parent element (if it's a species)
158
- if isinstance (sp , Species ):
159
- return sp .element in self ._data
160
- return False
154
+ return sp in self ._data
161
155
except ValueError as exc :
162
156
raise TypeError (f"Invalid { key = } for Composition" ) from exc
163
157
@@ -405,14 +399,14 @@ def get_integer_formula_and_factor(
405
399
Li0.5O0.25 returns (Li2O, 0.25). O0.25 returns (O2, 0.125)
406
400
"""
407
401
el_amt = self .get_el_amt_dict ()
408
- g = gcd_float (list (el_amt .values ()), 1 / max_denominator )
402
+ gcd = gcd_float (list (el_amt .values ()), 1 / max_denominator )
409
403
410
- d = {k : round (v / g ) for k , v in el_amt .items ()}
411
- formula , factor = reduce_formula (d , iupac_ordering = iupac_ordering )
404
+ dct = {k : round (v / gcd ) for k , v in el_amt .items ()}
405
+ formula , factor = reduce_formula (dct , iupac_ordering = iupac_ordering )
412
406
if formula in Composition .special_formulas :
413
407
formula = Composition .special_formulas [formula ]
414
408
factor /= 2
415
- return formula , factor * g
409
+ return formula , factor * gcd
416
410
417
411
@property
418
412
def reduced_formula (self ) -> str :
@@ -434,8 +428,8 @@ def hill_formula(self) -> str:
434
428
no carbon, all the elements, including hydrogen, are listed
435
429
alphabetically.
436
430
"""
437
- c = self .element_composition
438
- elements = sorted (el .symbol for el in c )
431
+ elem_comp = self .element_composition
432
+ elements = sorted (el .symbol for el in elem_comp )
439
433
hill_elements = []
440
434
if "C" in elements :
441
435
hill_elements .append ("C" )
@@ -445,7 +439,7 @@ def hill_formula(self) -> str:
445
439
elements .remove ("H" )
446
440
hill_elements += elements
447
441
448
- formula = [f"{ el } { formula_double_format (c [el ]) if c [el ] != 1 else '' } " for el in hill_elements ]
442
+ formula = [f"{ el } { formula_double_format (elem_comp [el ]) if elem_comp [el ] != 1 else '' } " for el in hill_elements ]
449
443
return " " .join (formula )
450
444
451
445
@property
@@ -621,8 +615,10 @@ def valid(self) -> bool:
621
615
"""
622
616
return not any (isinstance (el , DummySpecies ) for el in self .elements )
623
617
624
- def __repr__ (self ) -> str :
625
- return "Comp: " + self .formula
618
+ def __repr__ (self ):
619
+ formula = " " .join (f"{ k } { ':' if hasattr (k , 'oxi_state' ) else '' } { v :g} " for k , v in self .items ())
620
+ cls_name = type (self ).__name__
621
+ return f"{ cls_name } ({ formula !r} )"
626
622
627
623
@classmethod
628
624
def from_dict (cls , d ) -> Composition :
@@ -1299,9 +1295,3 @@ def __repr__(self):
1299
1295
1300
1296
class CompositionError (Exception ):
1301
1297
"""Exception class for composition errors."""
1302
-
1303
-
1304
- if __name__ == "__main__" :
1305
- import doctest
1306
-
1307
- doctest .testmod ()
0 commit comments