From 3ca511a9149fed4991b1cb87f6920642b6f7e4c2 Mon Sep 17 00:00:00 2001 From: Anselm Hahn Date: Tue, 1 Oct 2024 20:39:17 +0200 Subject: [PATCH] refactor: :recycle: File I/O operations for better readability and maintainability --- spectrafit/plugins/data_converter.py | 2 +- spectrafit/plugins/file_converter.py | 13 +++++-------- spectrafit/plugins/notebook.py | 13 ++++++------- spectrafit/plugins/pkl_converter.py | 2 +- spectrafit/plugins/pkl_visualizer.py | 4 ++-- spectrafit/plugins/pptx_converter.py | 2 +- spectrafit/plugins/rixs_converter.py | 6 ++---- spectrafit/plugins/rixs_visualizer.py | 4 ++-- spectrafit/tools.py | 12 ++++++------ 9 files changed, 26 insertions(+), 32 deletions(-) diff --git a/spectrafit/plugins/data_converter.py b/spectrafit/plugins/data_converter.py index ab3a36cde..55cbe8385 100644 --- a/spectrafit/plugins/data_converter.py +++ b/spectrafit/plugins/data_converter.py @@ -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( diff --git a/spectrafit/plugins/file_converter.py b/spectrafit/plugins/file_converter.py index 632308d67..4845808a1 100644 --- a/spectrafit/plugins/file_converter.py +++ b/spectrafit/plugins/file_converter.py @@ -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: diff --git a/spectrafit/plugins/notebook.py b/spectrafit/plugins/notebook.py index 3aaa50db5..53cb2655f 100644 --- a/spectrafit/plugins/notebook.py +++ b/spectrafit/plugins/notebook.py @@ -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) diff --git a/spectrafit/plugins/pkl_converter.py b/spectrafit/plugins/pkl_converter.py index e26b8650a..77524a02d 100644 --- a/spectrafit/plugins/pkl_converter.py +++ b/spectrafit/plugins/pkl_converter.py @@ -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: diff --git a/spectrafit/plugins/pkl_visualizer.py b/spectrafit/plugins/pkl_visualizer.py index b2e8955ea..5732c6558 100644 --- a/spectrafit/plugins/pkl_visualizer.py +++ b/spectrafit/plugins/pkl_visualizer.py @@ -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) diff --git a/spectrafit/plugins/pptx_converter.py b/spectrafit/plugins/pptx_converter.py index d2b067045..7c64ee92c 100644 --- a/spectrafit/plugins/pptx_converter.py +++ b/spectrafit/plugins/pptx_converter.py @@ -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} diff --git a/spectrafit/plugins/rixs_converter.py b/spectrafit/plugins/rixs_converter.py index 8cb91072b..34b18466f 100644 --- a/spectrafit/plugins/rixs_converter.py +++ b/spectrafit/plugins/rixs_converter.py @@ -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) diff --git a/spectrafit/plugins/rixs_visualizer.py b/spectrafit/plugins/rixs_visualizer.py index e1acc7c10..9c5a97369 100644 --- a/spectrafit/plugins/rixs_visualizer.py +++ b/spectrafit/plugins/rixs_visualizer.py @@ -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.") diff --git a/spectrafit/tools.py b/spectrafit/tools.py index 1f2ce59a9..0092afac5 100644 --- a/spectrafit/tools.py +++ b/spectrafit/tools.py @@ -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: @@ -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( @@ -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"]