Skip to content

IO

conatus.io.file.FileWriter

FileWriter(
    base_folder: Path | str | None = None,
    *,
    run_folder: Path | str | None = None,
    debug_folder: Path | str | None = None,
    log_folder: Path | str | None = None,
    out_folder: Path | str | None = None,
    run_id: str | None = None
)

Bases: BaseModel

File writer class.

This class is used to write data to files.

It is used to write data to files in the following folders:

  • debug: Debug information.
  • log: Log information.
  • out: Output information.
ATTRIBUTE DESCRIPTION
base_folder

The base folder to log run outputs to.

TYPE: Path

run_folder

The folder to write the outputs of a specific run to.

TYPE: Path

log_folder

The folder to write logs to.

TYPE: Path

out_folder

The folder to write outputs to.

TYPE: Path

debug_folder

The folder to write debug information to.

TYPE: Path

run_id

The ID of the run. Defaults to a Unix UTC timestamp.

TYPE: str | None

PARAMETER DESCRIPTION
base_folder

The base folder to log run outputs to. Defaults to the current working directory / "runs".

TYPE: Path | str | None DEFAULT: None

run_folder

The folder to write the outputs of a specific run to. Defaults to {base_folder} / {Unix UTC timestamp}.

TYPE: Path | str | None DEFAULT: None

debug_folder

The folder to write debug information to. Defaults to {base_folder} / "debug".

TYPE: Path | str | None DEFAULT: None

log_folder

The folder to write logs to. Defaults to {run_folder} / "log".

TYPE: Path | str | None DEFAULT: None

out_folder

The folder to write outputs to. Defaults to {run_folder} / "out".

TYPE: Path | str | None DEFAULT: None

run_id

The ID of the run. Defaults to a Unix UTC timestamp.

TYPE: str | None DEFAULT: None

Source code in conatus/io/file.py
def __init__(
    # It's fine to have more than 5 arguments
    self,
    base_folder: Path | str | None = None,
    *,
    run_folder: Path | str | None = None,
    debug_folder: Path | str | None = None,
    log_folder: Path | str | None = None,
    out_folder: Path | str | None = None,
    run_id: str | None = None,
) -> None:
    """Initialize the file writer.

    Args:
        base_folder: The base folder to log run outputs to. Defaults to
            the current working directory / "runs".
        run_folder: The folder to write the outputs of a specific run to.
            Defaults to {base_folder} / {Unix UTC timestamp}.
        debug_folder: The folder to write debug information to. Defaults to
            {base_folder} / "debug".
        log_folder: The folder to write logs to. Defaults to {run_folder} /
            "log".
        out_folder: The folder to write outputs to. Defaults to {run_folder}
            / "out".
        run_id: The ID of the run. Defaults to a Unix UTC timestamp.
    """
    # First, let's get the fallback base folder and the fallback run_id
    # TODO(lemeb): Check for weird cases of run_id and base_folder defined
    # together or separately
    # CTUS-17

    bf, rid = self._standard_base_folder()
    bf = bf if base_folder is None else Path(base_folder)
    run_id = rid if run_id is None else run_id
    base_folder = Path(bf)
    run_folder = (
        base_folder / run_id if run_folder is None else Path(run_folder)
    )
    debug_folder = (
        run_folder / "debug" if debug_folder is None else Path(debug_folder)
    )
    log_folder = (
        run_folder / "log" if log_folder is None else Path(log_folder)
    )
    out_folder = (
        run_folder / "out" if out_folder is None else Path(out_folder)
    )
    super().__init__(
        base_folder=base_folder,
        debug_folder=debug_folder,
        run_folder=run_folder,
        log_folder=log_folder,
        out_folder=out_folder,
        run_id=run_id,
    )

write

write(
    data: str | bytes | BytesIO | StringIO,
    file_name: str,
    output_type: OutputType = LOG,
    *,
    append: bool = False
) -> None

Write data to a file.

We only accept str, bytes, BytesIO, and StringIO objects.

We will create the folder if it does not exist, even if we need to create parents.

PARAMETER DESCRIPTION
data

The data to write.

TYPE: str | bytes | BytesIO | StringIO

file_name

The name of the file to write to.

TYPE: str

output_type

The type of output. Defaults to OutputType.LOG.

TYPE: OutputType DEFAULT: LOG

append

Whether to append to the file. Defaults to False.

TYPE: bool DEFAULT: False

Source code in conatus/io/file.py
def write(
    self,
    data: str | bytes | BytesIO | StringIO,
    file_name: str,
    output_type: OutputType = OutputType.LOG,
    *,
    append: bool = False,
) -> None:
    """Write data to a file.

    We only accept `str`, `bytes`, `BytesIO`, and `StringIO` objects.

    We will create the folder if it does not exist, even if we need to
    create parents.

    Args:
        data: The data to write.
        file_name: The name of the file to write to.
        output_type: The type of output. Defaults to `OutputType.LOG`.
        append: Whether to append to the file. Defaults to `False`.
    """
    content = data.read() if isinstance(data, BytesIO | StringIO) else data
    mode = "a" if append else "w"
    mode = mode if isinstance(data, str | StringIO) else f"{mode}b"
    folder_mapping: dict[OutputType, Path] = {
        OutputType.LOG: self.log_folder,
        OutputType.OUT: self.out_folder,
        OutputType.DEBUG: self.debug_folder,
    }
    folder = folder_mapping[output_type]
    Path.mkdir(folder, parents=True, exist_ok=True)
    with Path.open(folder / file_name, mode) as file:
        _ = file.write(content)