Skip to content

Commit

Permalink
Merge pull request #1621 from Anselmoo/Anselmoo/issue1620
Browse files Browse the repository at this point in the history
refactor: ♻️ File I/O operations for better readability and maintainability
  • Loading branch information
Anselmoo authored Oct 1, 2024
2 parents 6349d58 + 3ca511a commit 699fe00
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion spectrafit/plugins/data_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_athena_column(fname: Path, comment: str = "#") -> Optional[List[str]]:
Optional[List[str]]: The column names of the data file as a list.
"""
with open(fname, encoding="utf-8") as f:
with fname.open(encoding="utf-8") as f:
data = f.read()
lines = data.splitlines()
return next(
Expand Down
13 changes: 5 additions & 8 deletions spectrafit/plugins/file_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,17 @@ def save(self, data: Any, fname: Path, export_format: str) -> None:
)

if export_format == "json":
with open(
fname.with_suffix(f".{export_format}"), "w", encoding="utf-8"
with fname.with_suffix(f".{export_format}").open(
"w", encoding="utf-8"
) as f:
json.dump(data, f, indent=4)
elif export_format in {"yaml", "yml"}:
with open(
fname.with_suffix(f".{export_format}"), "w", encoding="utf-8"
with fname.with_suffix(f".{export_format}").open(
"w", encoding="utf-8"
) as f:
yaml.dump(data, f, default_flow_style=False)
elif export_format in {"toml", "lock"}:
with open(
fname.with_suffix(f".{export_format}"),
"wb+",
) as f:
with fname.with_suffix(f".{export_format}").open("wb+") as f:
tomli_w.dump(dict(**data), f)

def __call__(self) -> None:
Expand Down
13 changes: 6 additions & 7 deletions spectrafit/plugins/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,12 @@ def export_report(self, report: Dict[Any, Any], args: FnameAPI) -> None:
args (FnameAPI): Arguments for the file export including the path, prefix,
and suffix.
"""
with open(
self.fname2path(
fname=args.fname,
prefix=args.prefix,
suffix=args.suffix,
folder=args.folder,
),
with self.fname2path(
fname=args.fname,
prefix=args.prefix,
suffix=args.suffix,
folder=args.folder,
).open(
"wb+",
) as f:
tomli_w.dump(report, f)
Expand Down
2 changes: 1 addition & 1 deletion spectrafit/plugins/pkl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def to_numpy(self) -> None:
def to_pickle(self) -> None:
"""Export the data to a pickle file."""
if self.export_format.lower() == "pkl":
with open(self.fname, "wb") as f:
with self.fname.open("wb") as f:
pickle.dump(self.data, f)
elif self.export_format.lower() == pkl_gz:
with gzip.open(self.fname, "wb") as f:
Expand Down
4 changes: 2 additions & 2 deletions spectrafit/plugins/pkl_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def save(self, data: Any, fname: Path, export_format: str) -> None:
format=export_format,
)

with open(
pure_fname(fname).with_suffix(".json"), "w+", encoding="utf-8"
with pure_fname(fname).with_suffix(".json").open(
"w+", encoding="utf-8"
) as outfile:
json.dump(data, outfile, indent=4)

Expand Down
2 changes: 1 addition & 1 deletion spectrafit/plugins/pptx_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def convert(infile: Path, file_format: str) -> MutableMapping[str, Any]:
f"File format '{infile.suffix}' is not supported; it must be '.lock'"
)

with open(infile, "rb") as f:
with infile.open("rb") as f:
data = PPTXDataAPI(**tomli.load(f))

return {file_format: data}
Expand Down
6 changes: 2 additions & 4 deletions spectrafit/plugins/rixs_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,13 @@ def save(self, data: Any, fname: Path, export_format: str) -> None:
)

if export_format == "json":
with open(
pure_fname(fname).with_suffix(f".{export_format}"),
with pure_fname(fname).with_suffix(f".{export_format}").open(
"w",
encoding="utf-8",
) as f:
json.dump(self.numpydict2listdict(data), f, indent=4)
elif export_format in {"toml", "lock"}:
with open(
pure_fname(fname).with_suffix(f".{export_format}"),
with pure_fname(fname).with_suffix(f".{export_format}").open(
"wb",
) as f:
tomli_w.dump(self.numpydict2listdict(data), f, multiline_strings=False)
Expand Down
4 changes: 2 additions & 2 deletions spectrafit/plugins/rixs_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,10 @@ def load_data(infile: Path) -> RIXSModelAPI:
elif infile.suffix == ".npz":
data = np.load(infile, allow_pickle=True)
elif infile.suffix == ".json":
with open(infile, encoding="utf-8") as f:
with infile.open(encoding="utf-8") as f:
data = json.load(f)
elif infile.suffix in {".toml", ".lock"}:
with open(infile, "rb") as f:
with infile.open("rb") as f:
data = tomli.load(f)
else:
raise ValueError(f"File type {infile.suffix} is not supported.")
Expand Down
12 changes: 6 additions & 6 deletions spectrafit/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ def save_as_csv(self) -> None:
def save_as_json(self) -> None:
"""Save the fitting result as json file."""
if self.args["outfile"]:
with open(
Path(f"{self.args['outfile']}_summary.json"), "w", encoding="utf-8"
with Path(f"{self.args['outfile']}_summary.json").open(
"w", encoding="utf-8"
) as f:
json.dump(transform_nested_types(self.args), f, indent=4)
else:
Expand All @@ -539,13 +539,13 @@ def read_input_file(fname: Path) -> MutableMapping[str, Any]:
fname = Path(fname)

if fname.suffix == ".toml":
with open(fname, "rb") as f:
with fname.open("rb") as f:
args = tomli.load(f)
elif fname.suffix == ".json":
with open(fname, encoding="utf-8") as f:
with fname.open(encoding="utf-8") as f:
args = json.load(f)
elif fname.suffix in {".yaml", ".yml"}:
with open(fname, encoding="utf-8") as f:
with fname.open(encoding="utf-8") as f:
args = yaml.load(f, Loader=yaml.FullLoader)
else:
raise OSError(
Expand Down Expand Up @@ -654,7 +654,7 @@ def pkl2any(pkl_fname: Path, encoding: str = "latin1") -> Any:
with gzip.open(pkl_fname, "rb") as f:
return unicode_check(f, encoding=encoding)
elif pkl_fname.suffix == ".pkl":
with open(pkl_fname, "rb") as f:
with pkl_fname.open("rb") as f:
return unicode_check(f, encoding=encoding)
else:
choices = [".pkl", ".pkl.gz"]
Expand Down

0 comments on commit 699fe00

Please sign in to comment.