Skip to content

Commit ffae4ea

Browse files
committed
support writing structures to compressed JSON files
.json .json.gz .json.bz2 .json.xz .json.lzma
1 parent 61c20b1 commit ffae4ea

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

pymatgen/core/structure.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,19 +2493,19 @@ def to(self, filename: str = "", fmt: str = "", **kwargs) -> str | None:
24932493
from pymatgen.io.cssr import Cssr
24942494

24952495
writer = Cssr(self) # type: ignore
2496-
elif fmt == "json" or fnmatch(filename.lower(), "*.json"):
2497-
s = json.dumps(self.as_dict())
2496+
elif fmt == "json" or fnmatch(filename.lower(), "*.json*"):
2497+
dct = json.dumps(self.as_dict())
24982498
if filename:
2499-
with zopen(filename, "wt") as f:
2500-
f.write(s)
2501-
return s
2499+
with zopen(filename, "wt") as file:
2500+
file.write(dct)
2501+
return dct
25022502
elif fmt == "xsf" or fnmatch(filename.lower(), "*.xsf*"):
25032503
from pymatgen.io.xcrysden import XSF
25042504

25052505
s = XSF(self).to_string()
25062506
if filename:
2507-
with zopen(filename, "wt", encoding="utf8") as f:
2508-
f.write(s)
2507+
with zopen(filename, "wt", encoding="utf8") as file:
2508+
file.write(s)
25092509
return s
25102510
elif (
25112511
fmt == "mcsqs"
@@ -2517,8 +2517,8 @@ def to(self, filename: str = "", fmt: str = "", **kwargs) -> str | None:
25172517

25182518
s = Mcsqs(self).to_string()
25192519
if filename:
2520-
with zopen(filename, "wt", encoding="ascii") as f:
2521-
f.write(s)
2520+
with zopen(filename, "wt", encoding="ascii") as file:
2521+
file.write(s)
25222522
return s
25232523
elif fmt == "prismatic" or fnmatch(filename, "*prismatic*"):
25242524
from pymatgen.io.prismatic import Prismatic
@@ -2528,8 +2528,8 @@ def to(self, filename: str = "", fmt: str = "", **kwargs) -> str | None:
25282528
elif fmt == "yaml" or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
25292529
yaml = YAML()
25302530
if filename:
2531-
with zopen(filename, "wt") as f:
2532-
yaml.dump(self.as_dict(), f)
2531+
with zopen(filename, "wt") as file:
2532+
yaml.dump(self.as_dict(), file)
25332533
return None
25342534
sio = StringIO()
25352535
yaml.dump(self.as_dict(), sio)
@@ -2543,8 +2543,8 @@ def to(self, filename: str = "", fmt: str = "", **kwargs) -> str | None:
25432543

25442544
s = ResIO.structure_to_str(self)
25452545
if filename:
2546-
with zopen(filename, "wt", encoding="utf8") as f:
2547-
f.write(s)
2546+
with zopen(filename, "wt", encoding="utf8") as file:
2547+
file.write(s)
25482548
return None
25492549
return s
25502550
else:

0 commit comments

Comments
 (0)