Build Path

Creates a complete file or folder path by combining folder/file names.

Build Path


Processing

Creates a complete file or folder path by combining a base path with up to three additional path components. The function validates input arguments to ensure proper path construction and provides flexible output format options.

Inputs

base path
The starting directory or path to build from
part 1
The first path component to append to the base path
part 2 (optional)
The second path component to append
part 3 (optional)
The third path component to append (requires part_2 to be provided)

Inputs Types

Input Types
base path Str, DirectoryPath
part 1 Str, Path
part 2 Str, Path
part 3 Str, Path

You can check the list of supported types here: Available Type Hints.

Outputs

result path
The complete constructed path combining all provided components

Outputs Types

Output Types
result path str, Path

You can check the list of supported types here: Available Type Hints.

Options

The Build Path brick contains some changeable options:

Return as a string
Controls whether the output path is returned as a string or Path object (default: True)
Verbose
Enables detailed logging of the path building process (default: True)
import logging
from coded_flows.types import Str, Path, DirectoryPath, Union

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def build_path(
    base_path: Union[Str, DirectoryPath],
    part_1: Union[Str, Path],
    part_2: Union[Str, Path] = None,
    part_3: Union[Str, Path] = None,
    options=None,
) -> Union[Str, Path]:
    brick_display_name = "Build Path"
    options = options or {}
    verbose = options.get("verbose", True)
    return_as_string: bool = options.get("return_as_string", False)
    result_path = None
    try:
        if verbose:
            logger.info(f"[{brick_display_name}] Starting path building process")
            logger.info(f"[{brick_display_name}] Base path: {base_path}")
            logger.info(f"[{brick_display_name}] Part_1: {part_1}")
            logger.info(f"[{brick_display_name}] Part_2: {part_2}")
            logger.info(f"[{brick_display_name}] Part_3: {part_3}")
        if part_3 is not None and part_2 is None:
            error_message = "Cannot provide part_3 without part_2"
            if verbose:
                logger.error(f"[{brick_display_name}] {error_message}")
            raise ValueError(error_message)
        if isinstance(base_path, str):
            base_path_obj = Path(base_path)
            verbose and logger.info(
                f"[{brick_display_name}] Converted string base path to Path object"
            )
        else:
            base_path_obj = base_path
            verbose and logger.info(
                f"[{brick_display_name}] Using provided base path Path object"
            )
        result_path = base_path_obj / part_1
        verbose and logger.info(
            f"[{brick_display_name}] Added part_1, current path: {result_path}"
        )
        if part_2 is not None:
            result_path = result_path / part_2
            verbose and logger.info(
                f"[{brick_display_name}] Added part_2, current path: {result_path}"
            )
        if part_3 is not None:
            result_path = result_path / part_3
            verbose and logger.info(
                f"[{brick_display_name}] Added part_3, current path: {result_path}"
            )
        if return_as_string:
            result_path = str(result_path)
            verbose and logger.info(
                f"[{brick_display_name}] Converted result to string format"
            )
        else:
            verbose and logger.info(
                f"[{brick_display_name}] Keeping result as Path object"
            )
        if verbose:
            logger.info(
                f"[{brick_display_name}] Successfully built path: {result_path}"
            )
            logger.info(f"[{brick_display_name}] Path building completed successfully")
    except Exception as e:
        if verbose:
            logger.error(f"[{brick_display_name}] Unexpected error occurred!")
        raise
    return result_path

Brick Info

version v0.1.4
python 3.10, 3.11, 3.12, 3.13
requirements
    -