Skip to content

Commit

Permalink
always return string from Structure.to() regardless of filename kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Aug 10, 2023
1 parent 580b760 commit 2781937
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2637,7 +2637,7 @@ def from_dict(cls, d: dict[str, Any], fmt: Literal["abivars"] | None = None) ->
charge = d.get("charge")
return cls.from_sites(sites, charge=charge)

def to(self, filename: str = "", fmt: str = "", **kwargs) -> str | None:
def to(self, filename: str = "", fmt: str = "", **kwargs) -> str:
"""
Outputs the structure to a file or string.
Expand All @@ -2655,7 +2655,8 @@ def to(self, filename: str = "", fmt: str = "", **kwargs) -> str | None:
CifWriter.__init__ method for generation of symmetric cifs.
Returns:
(str) if filename is None. None otherwise.
str: String representation of molecule in given format. If a filename
is provided, the same string is written to the file.
"""
fmt = fmt.lower()

Expand Down Expand Up @@ -2684,11 +2685,11 @@ def to(self, filename: str = "", fmt: str = "", **kwargs) -> str | None:
elif fmt == "xsf" or fnmatch(filename.lower(), "*.xsf*"):
from pymatgen.io.xcrysden import XSF

string = XSF(self).to_str()
res_str = XSF(self).to_str()
if filename:
with zopen(filename, "wt", encoding="utf8") as file:
file.write(string)
return string
file.write(res_str)
return res_str
elif (
fmt == "mcsqs"
or fnmatch(filename, "*rndstr.in*")
Expand All @@ -2697,24 +2698,24 @@ def to(self, filename: str = "", fmt: str = "", **kwargs) -> str | None:
):
from pymatgen.io.atat import Mcsqs

string = Mcsqs(self).to_str()
res_str = Mcsqs(self).to_str()
if filename:
with zopen(filename, "wt", encoding="ascii") as file:
file.write(string)
return string
file.write(res_str)
return res_str
elif fmt == "prismatic" or fnmatch(filename, "*prismatic*"):
from pymatgen.io.prismatic import Prismatic

return Prismatic(self).to_str()
elif fmt == "yaml" or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
yaml = YAML()
str_io = StringIO()
yaml.dump(self.as_dict(), str_io)
yaml_str = str_io.getvalue()
if filename:
with zopen(filename, "wt") as file:
yaml.dump(self.as_dict(), file)
return None
sio = StringIO()
yaml.dump(self.as_dict(), sio)
return sio.getvalue()
file.write(yaml_str)
return yaml_str
# fleur support implemented in external namespace pkg https://github.com/JuDFTteam/pymatgen-io-fleur
elif fmt == "fleur-inpgen" or fnmatch(filename, "*.in*"):
from pymatgen.io.fleur import FleurInput
Expand All @@ -2723,20 +2724,18 @@ def to(self, filename: str = "", fmt: str = "", **kwargs) -> str | None:
elif fmt == "res" or fnmatch(filename, "*.res"):
from pymatgen.io.res import ResIO

string = ResIO.structure_to_str(self)
res_str = ResIO.structure_to_str(self)
if filename:
with zopen(filename, "wt", encoding="utf8") as file:
file.write(string)
return None
return string
file.write(res_str)
return res_str
else:
if fmt == "":
raise ValueError(f"Format not specified and could not infer from {filename=}")
raise ValueError(f"Invalid format={fmt!r}")

if filename:
writer.write_file(filename)
return None
return str(writer)

@classmethod
Expand Down

0 comments on commit 2781937

Please sign in to comment.