Skip to content

String formatter

Pretty formatting for Operation objects.

This module provides functionality to convert Operation objects into human-readable formatted strings. The format is Pythonic and preserves all information including nested operations, lists, tuples, and dicts.

ARCHITECTURE

format_value() - Single entry point for ALL formatting ├── _format_categorical() - Internal handler for Categorical ├── _format_float() - Internal handler for Float ├── _format_integer() - Internal handler for Integer ├── _format_resampled() - Internal handler for Resample ├── _format_repeated() - Internal handler for Repeated ├── _format_operation() - Internal handler for Operation ├── _format_sequence() - Internal handler for list/tuple └── _format_dict() - Internal handler for dict

All str methods should call format_value() directly. All internal formatters call format_value() for nested values.

FormatterStyle dataclass #

FormatterStyle(
    indent_str: str = "   ",
    max_line_length: int = 90,
    compact_threshold: int = 40,
    show_empty_args: bool = True,
)

Configuration for the formatting style.

format_value #

format_value(
    value: Any,
    indent: int = 0,
    style: FormatterStyle | None = None,
) -> str

Format any value with proper indentation and style.

This is the SINGLE entry point for all formatting in NePS. All str methods should delegate to this function.

PARAMETER DESCRIPTION
value

The value to format (any type)

TYPE: Any

indent

Current indentation level

TYPE: int DEFAULT: 0

style

Formatting style configuration

TYPE: FormatterStyle | None DEFAULT: None

RETURNS DESCRIPTION
str

Formatted string representation

Source code in neps\space\neps_spaces\string_formatter.py
def format_value(  # noqa: C901, PLR0911, PLR0912
    value: Any,
    indent: int = 0,
    style: FormatterStyle | None = None,
) -> str:
    """Format any value with proper indentation and style.

    This is the SINGLE entry point for all formatting in NePS.
    All __str__ methods should delegate to this function.

    Args:
        value: The value to format (any type)
        indent: Current indentation level
        style: Formatting style configuration

    Returns:
        Formatted string representation
    """
    from neps.space.neps_spaces.parameters import (
        ByName,
        Categorical,
        Fidelity,
        Float,
        Integer,
        Operation,
        Repeated,
        Resample,
    )

    if style is None:
        style = FormatterStyle()

    # Dispatch to appropriate internal formatter based on type
    if isinstance(value, Operation):
        return _format_operation(value, indent, style)

    if isinstance(value, Categorical):
        return _format_categorical(value, indent, style)

    if isinstance(value, Float):
        return _format_float(value, indent, style)

    if isinstance(value, Integer):
        return _format_integer(value, indent, style)

    if isinstance(value, Fidelity):
        # Use the __str__ method of Fidelity subclasses directly
        return str(value)

    if isinstance(value, ByName):
        return _format_by_name(value, indent, style)

    if isinstance(value, Resample):
        return _format_resampled(value, indent, style)

    if isinstance(value, Repeated):
        return _format_repeated(value, indent, style)

    if isinstance(value, list | tuple):
        return _format_sequence(value, indent, style)

    if isinstance(value, dict):
        return _format_dict(value, indent, style)

    # Check for PipelineSpace (import here to avoid circular dependency)
    from neps.space.neps_spaces.parameters import PipelineSpace

    if isinstance(value, PipelineSpace):
        return _format_pipeline_space(value, indent, style)

    # For callables (functions, methods), show their name
    if callable(value) and (name := getattr(value, "__name__", None)):
        return name

    # For identifier strings, don't add quotes
    if isinstance(value, str) and value.isidentifier():
        return value

    # For other values, use repr
    return repr(value)