Skip to content

Commit

Permalink
Merge 474f029 into 4145ffd
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Sep 10, 2021
2 parents 4145ffd + 474f029 commit efad9b1
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import warnings
from abc import ABCMeta, abstractmethod
from fnmatch import fnmatch
from typing import Dict, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, Union, Callable
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, Union, Callable, Literal

import numpy as np
from monty.dev import deprecated
Expand Down Expand Up @@ -405,7 +405,7 @@ def to(self, fmt: str = None, filename: str = None):

@classmethod
@abstractmethod
def from_str(cls, input_string: str, fmt: str):
def from_str(cls, input_string: str, fmt: Any):
"""
Reads in SiteCollection from a string.
"""
Expand Down Expand Up @@ -2332,13 +2332,21 @@ def to(self, fmt: str = None, filename=None, **kwargs) -> Optional[str]:
return writer.__str__()

@classmethod
def from_str(cls, input_string: str, fmt: str, primitive=False, sort=False, merge_tol=0.0):
def from_str(
cls,
input_string: str,
fmt: Literal["cif", "poscar", "cssr", "json", "yaml", "xsf", "mcsqs"],
primitive=False,
sort=False,
merge_tol=0.0,
):
"""
Reads a structure from a string.
Args:
input_string (str): String to parse.
fmt (str): A format specification.
fmt (str): A file format specification. One of "cif", "poscar", "cssr",
"json", "yaml", "xsf", "mcsqs".
primitive (bool): Whether to find a primitive cell. Defaults to
False.
sort (bool): Whether to sort the sites in accordance to the default
Expand All @@ -2356,27 +2364,27 @@ def from_str(cls, input_string: str, fmt: str, primitive=False, sort=False, merg
from pymatgen.io.vasp import Poscar
from pymatgen.io.xcrysden import XSF

fmt = fmt.lower()
if fmt == "cif":
fmt_low = fmt.lower()
if fmt_low == "cif":
parser = CifParser.from_string(input_string)
s = parser.get_structures(primitive=primitive)[0]
elif fmt == "poscar":
elif fmt_low == "poscar":
s = Poscar.from_string(input_string, False, read_velocities=False).structure
elif fmt == "cssr":
elif fmt_low == "cssr":
cssr = Cssr.from_string(input_string)
s = cssr.structure
elif fmt == "json":
elif fmt_low == "json":
d = json.loads(input_string)
s = Structure.from_dict(d)
elif fmt == "yaml":
elif fmt_low == "yaml":
d = yaml.safe_load(input_string)
s = Structure.from_dict(d)
elif fmt == "xsf":
elif fmt_low == "xsf":
s = XSF.from_string(input_string).structure
elif fmt == "mcsqs":
elif fmt_low == "mcsqs":
s = Mcsqs.structure_from_string(input_string)
else:
raise ValueError("Unrecognized format `%s`!" % fmt)
raise ValueError(f"Unrecognized format `{fmt}`!")

if sort:
s = s.get_sorted_structure()
Expand Down

0 comments on commit efad9b1

Please sign in to comment.