60
60
61
61
from pymatgen .util .typing import CompositionLike , SpeciesLike
62
62
63
+ FileFormats = Literal ["cif" , "poscar" , "cssr" , "json" , "yaml" , "yml" , "xsf" , "mcsqs" , "res" , "" ]
64
+
63
65
64
66
class Neighbor (Site ):
65
67
"""Simple Site subclass to contain a neighboring atom that skips all the unnecessary checks for speed. Can be
@@ -447,13 +449,13 @@ def is_valid(self, tol: float = DISTANCE_TOLERANCE) -> bool:
447
449
return np .min (all_dists ) > tol
448
450
449
451
@abstractmethod
450
- def to (self , filename : str = "" , fmt : str = "" ) -> str | None :
452
+ def to (self , filename : str = "" , fmt : FileFormats = "" ) -> str | None :
451
453
"""Generates string representations (cif, json, poscar, ....) of SiteCollections (e.g.,
452
454
molecules / structures). Should return str or None if written to a file.
453
455
"""
454
456
raise NotImplementedError
455
457
456
- def to_file (self , filename : str = "" , fmt : str = "" ) -> str | None :
458
+ def to_file (self , filename : str = "" , fmt : FileFormats = "" ) -> str | None :
457
459
"""A more intuitive alias for .to()."""
458
460
return self .to (filename , fmt )
459
461
@@ -2653,7 +2655,7 @@ def from_dict(cls, dct: dict[str, Any], fmt: Literal["abivars"] | None = None) -
2653
2655
charge = dct .get ("charge" )
2654
2656
return cls .from_sites (sites , charge = charge , properties = dct .get ("properties" ))
2655
2657
2656
- def to (self , filename : str | Path = "" , fmt : str = "" , ** kwargs ) -> str :
2658
+ def to (self , filename : str | Path = "" , fmt : FileFormats = "" , ** kwargs ) -> str :
2657
2659
"""Outputs the structure to a file or string.
2658
2660
2659
2661
Args:
@@ -2663,7 +2665,7 @@ def to(self, filename: str | Path = "", fmt: str = "", **kwargs) -> str:
2663
2665
fmt (str): Format to output to. Defaults to JSON unless filename
2664
2666
is provided. If fmt is specifies, it overrides whatever the
2665
2667
filename is. Options include "cif", "poscar", "cssr", "json",
2666
- "xsf", "mcsqs", "prismatic", "yaml", "fleur-inpgen".
2668
+ "xsf", "mcsqs", "prismatic", "yaml", "yml", " fleur-inpgen".
2667
2669
Non-case sensitive.
2668
2670
**kwargs: Kwargs passthru to relevant methods. E.g., This allows
2669
2671
the passing of parameters like symprec to the
@@ -2673,7 +2675,7 @@ def to(self, filename: str | Path = "", fmt: str = "", **kwargs) -> str:
2673
2675
str: String representation of molecule in given format. If a filename
2674
2676
is provided, the same string is written to the file.
2675
2677
"""
2676
- filename , fmt = str (filename ), fmt .lower ()
2678
+ filename , fmt = str (filename ), cast ( FileFormats , fmt .lower () )
2677
2679
2678
2680
if fmt == "cif" or fnmatch (filename .lower (), "*.cif*" ):
2679
2681
from pymatgen .io .cif import CifWriter
@@ -2722,7 +2724,7 @@ def to(self, filename: str | Path = "", fmt: str = "", **kwargs) -> str:
2722
2724
from pymatgen .io .prismatic import Prismatic
2723
2725
2724
2726
return Prismatic (self ).to_str ()
2725
- elif fmt == "yaml" or fnmatch (filename , "*.yaml*" ) or fnmatch (filename , "*.yml*" ):
2727
+ elif fmt in ( "yaml" , "yml" ) or fnmatch (filename , "*.yaml*" ) or fnmatch (filename , "*.yml*" ):
2726
2728
yaml = YAML ()
2727
2729
str_io = StringIO ()
2728
2730
yaml .dump (self .as_dict (), str_io )
@@ -2747,7 +2749,7 @@ def to(self, filename: str | Path = "", fmt: str = "", **kwargs) -> str:
2747
2749
else :
2748
2750
if fmt == "" :
2749
2751
raise ValueError (f"Format not specified and could not infer from { filename = } " )
2750
- raise ValueError (f"Invalid format= { fmt !r } " )
2752
+ raise ValueError (f"Invalid { fmt = } , valid options are { get_args ( FileFormats ) } " )
2751
2753
2752
2754
if filename :
2753
2755
writer .write_file (filename )
@@ -2757,7 +2759,7 @@ def to(self, filename: str | Path = "", fmt: str = "", **kwargs) -> str:
2757
2759
def from_str ( # type: ignore[override]
2758
2760
cls ,
2759
2761
input_string : str ,
2760
- fmt : Literal [ "cif" , "poscar" , "cssr" , "json" , "yaml" , "xsf" , "mcsqs" , "res" ] ,
2762
+ fmt : FileFormats ,
2761
2763
primitive : bool = False ,
2762
2764
sort : bool = False ,
2763
2765
merge_tol : float = 0.0 ,
@@ -2768,7 +2770,7 @@ def from_str( # type: ignore[override]
2768
2770
Args:
2769
2771
input_string (str): String to parse.
2770
2772
fmt (str): A file format specification. One of "cif", "poscar", "cssr",
2771
- "json", "yaml", "xsf", "mcsqs".
2773
+ "json", "yaml", "yml", " xsf", "mcsqs", "res ".
2772
2774
primitive (bool): Whether to find a primitive cell. Defaults to
2773
2775
False.
2774
2776
sort (bool): Whether to sort the sites in accordance to the default
@@ -2797,12 +2799,12 @@ def from_str( # type: ignore[override]
2797
2799
cssr = Cssr .from_str (input_string , ** kwargs )
2798
2800
struct = cssr .structure
2799
2801
elif fmt_low == "json" :
2800
- d = json .loads (input_string )
2801
- struct = Structure .from_dict (d )
2802
- elif fmt_low == "yaml" :
2802
+ dct = json .loads (input_string )
2803
+ struct = Structure .from_dict (dct )
2804
+ elif fmt_low in ( "yaml" , "yml" ) :
2803
2805
yaml = YAML ()
2804
- d = yaml .load (input_string )
2805
- struct = Structure .from_dict (d )
2806
+ dct = yaml .load (input_string )
2807
+ struct = Structure .from_dict (dct )
2806
2808
elif fmt_low == "xsf" :
2807
2809
from pymatgen .io .xcrysden import XSF
2808
2810
@@ -2825,7 +2827,7 @@ def from_str( # type: ignore[override]
2825
2827
2826
2828
struct = ResIO .structure_from_str (input_string , ** kwargs )
2827
2829
else :
2828
- raise ValueError (f"Unrecognized format ` { fmt } `! " )
2830
+ raise ValueError (f"Invalid { fmt = } , valid options are { get_args ( FileFormats ) } " )
2829
2831
2830
2832
if sort :
2831
2833
struct = struct .get_sorted_structure ()
0 commit comments