Skip to content

output_file_handler

Classes¤

OutputFileHandler ¤

OutputFileHandler(
    working_dir, output_file_name="run_output.xlsx"
)

Bases: object

Parameters:

Name Type Description Default
working_dir str | Path

Directory, where the output file should be written.

required
output_file_name str

Name of the output file. Defaults to "run_output.xlsx".

'run_output.xlsx'
Source code in common/output_file_handler.py
def __init__(self, working_dir:str, output_file_name="run_output.xlsx") -> None:
    """Class for handling the output file, reading and writing to it.

    Args:
        working_dir (str | pathlib.Path): Directory, where the output file should be written.
        output_file_name (str, optional): Name of the output file. Defaults to "run_output.xlsx".
    """
    # Load main config file and set data directory
    self.config = configuration.load_config()
    self.data_dir = pathlib.Path(self.config["Paths"]["data_dir"])

    self.working_dir = self.data_dir.joinpath(working_dir)
    self.output_file = self.working_dir.joinpath(output_file_name)
    # obtain lock for the output file
    self.output_file_lock = filelock.FileLock(self.output_file.parent / (self.output_file.name + ".lock"))

    self.output_content = self.read_output_file()

Attributes¤

output_content property writable ¤
output_content

Content of the output file.

Returns:

Type Description
DataFrame

pd.DataFrame: Content of the output file in pd.DataFrame format.

Functions¤

add_output ¤
add_output(to_append)

Method add output to the output file.

Parameters:

Name Type Description Default
to_append dict | DataFrame

Data to append.

required
Source code in common/output_file_handler.py
def add_output(self, to_append:dict) -> None:
    """Method add output to the output file.

    Args:
        to_append (dict | pd.DataFrame): Data to append.
    """
    self.output_content = pd.concat([self.output_content, pd.DataFrame(to_append)])
    self.write_output_file()
read_output_file ¤
read_output_file()

Method to read the content of the output file.

Returns:

Type Description
DataFrame

pd.DataFrame: Content of the output file.

Source code in common/output_file_handler.py
def read_output_file(self) -> pd.DataFrame:
    """Method to read the content of the output file.

    Returns:
        pd.DataFrame: Content of the output file.
    """
    # if the file does not exist, return empty dataframe
    if not self.output_file.exists():
        return pd.DataFrame({"Run number": []})
    # make sure it loads correctly either from excel or csv
    if self.output_file.suffix == ".xlsx":
        return pd.read_excel(self.output_file, index_col=0)
    else:
        return pd.read_csv(self.output_file, index_col=0)
write_output_file ¤
write_output_file()

Method to write updated content to the output file.

Source code in common/output_file_handler.py
def write_output_file(self) -> None:
    """Method to write updated content to the output file.
    """
    with self.output_file_lock:
        if self.output_file.exists():
            file_data = self.read_output_file()
            self.output_content = pd.concat([self.output_content, file_data]).drop_duplicates().reset_index(drop=True)
            self.output_file.unlink()
        self.output_content = self.output_content.sort_values("Run number")
        if self.output_file.suffix == ".xlsx":
            self.output_content.to_excel(self.output_file)
        else:
            self.output_content.to_csv(self.output_file)

Last update: October 31, 2023
Created: October 31, 2023