From 3fe6a65f8e51326c34959ae24c60025954979569 Mon Sep 17 00:00:00 2001 From: Harsh Vardhan Date: Thu, 8 Feb 2024 23:26:57 +0530 Subject: [PATCH] Added IOMixin for reporter --- src/nnbench/reporter/mixins.py | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/nnbench/reporter/mixins.py diff --git a/src/nnbench/reporter/mixins.py b/src/nnbench/reporter/mixins.py new file mode 100644 index 00000000..6ddb8b83 --- /dev/null +++ b/src/nnbench/reporter/mixins.py @@ -0,0 +1,45 @@ +import os +from typing import Any + + +class IOMixin: + """ + A mixin class providing file I/O operations for benchmark reporters. + + This mixin offers common file handling functionalities like opening, closing, reading and writing. + This class can be used to create any BenchmarkReporter which has requirement of reading file. + """ + + def open(self, file_path: str) -> None: + """ + Opens a existing file at a given file_path for read and write operations. + + Parameters + ---------- + file_path : str + The path to the target file or directory. + + Raises + ------ + FileNotFoundError + If the target file is not present at the file_path. + """ + if not os.path.exists(file_path): + raise FileNotFoundError + self.file_path: str = file_path + with open(file_path, "r") as file: + self.raw_records = file.read() + self.file = open(file_path, "w") + + def close(self, **kwargs: dict[str, Any]) -> None: + """Closes the currently open file.""" + if self.file: + self.file.close() + self.file = None + self.file_path = None + + def read_records(self) -> Any: + return self.raw_records + + def write_records(self, content: str) -> None: + self.file.write(content)