File: title_formatting.py

package info (click to toggle)
python-sigima 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 24,956 kB
  • sloc: python: 33,326; makefile: 3
file content (168 lines) | stat: -rw-r--r-- 5,393 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.

"""
Title formatting system for computation results
-----------------------------------------------

This module provides a configurable title formatting system for computation results.
It allows different applications (Sigima vs DataLab) to use different title formatting
strategies while maintaining compatibility.
"""

from __future__ import annotations

from typing import Protocol, runtime_checkable

__all__ = [
    "PlaceholderTitleFormatter",
    "SimpleTitleFormatter",
    "TitleFormatter",
    "get_default_title_formatter",
    "set_default_title_formatter",
]


@runtime_checkable
class TitleFormatter(Protocol):
    """Protocol for title formatting strategies used in computation functions.

    This protocol allows different title formatting approaches:
    - Simple descriptive titles for Sigima-only usage
    - Placeholder-based titles for DataLab integration
    - Custom formatting for specific use cases
    """

    def format_1_to_1_title(self, name: str, suffix: str | None = None) -> str:
        """Format title for 1-to-1 computation (single input → single output).

        Args:
            name: Name of the computation function
            suffix: Optional suffix to add to the title

        Returns:
            Formatted title string
        """

    def format_n_to_1_title(
        self, name: str, n_inputs: int, suffix: str | None = None
    ) -> str:
        """Format title for n-to-1 computation (multiple inputs → single output).

        Args:
            name: Name of the computation function
            n_inputs: Number of input objects
            suffix: Optional suffix to add to the title

        Returns:
            Formatted title string
        """

    def format_2_to_1_title(self, name: str, suffix: str | None = None) -> str:
        """Format title for 2-to-1 computation (two inputs → single output).

        Args:
            name: Name of the computation function
            suffix: Optional suffix to add to the title

        Returns:
            Formatted title string
        """


class SimpleTitleFormatter:
    """Simple descriptive title formatter for Sigima-only usage.

    Creates human-readable titles without placeholders, suitable for
    standalone Sigima usage where object relationships are less critical.
    """

    def format_1_to_1_title(self, name: str, suffix: str | None = None) -> str:
        """Format title for 1-to-1 computation."""
        # Convert function names to human-readable format
        readable_name = name.replace("_", " ").title()
        base_title = f"{readable_name} Result"

        if suffix:
            base_title += f" ({suffix})"
        return base_title

    def format_n_to_1_title(
        self, name: str, n_inputs: int, suffix: str | None = None
    ) -> str:
        """Format title for n-to-1 computation."""
        readable_name = name.replace("_", " ").title()
        base_title = f"{readable_name} of {n_inputs} Objects"

        if suffix:
            base_title += f" ({suffix})"
        return base_title

    def format_2_to_1_title(self, name: str, suffix: str | None = None) -> str:
        """Format title for 2-to-1 computation."""
        if len(name) == 1:  # This is an operator
            base_title = f"Binary Operation {name}"
        else:
            readable_name = name.replace("_", " ").title()
            base_title = f"{readable_name} Result"

        if suffix:
            base_title += f" ({suffix})"
        return base_title


class PlaceholderTitleFormatter:
    """Placeholder-based title formatter compatible with DataLab.

    Creates titles with placeholders that can be resolved later by DataLab's
    patch_title_with_ids() function.
    """

    def format_1_to_1_title(self, name: str, suffix: str | None = None) -> str:
        """Format title for 1-to-1 computation with placeholder."""
        title = f"{name}({{0}})"
        if suffix:
            title += "|" + suffix
        return title

    def format_n_to_1_title(
        self, name: str, n_inputs: int, suffix: str | None = None
    ) -> str:
        """Format title for n-to-1 computation with placeholders."""
        placeholders = ", ".join(f"{{{i}}}" for i in range(n_inputs))
        title = f"{name}({placeholders})"
        if suffix:
            title += "|" + suffix
        return title

    def format_2_to_1_title(self, name: str, suffix: str | None = None) -> str:
        """Format title for 2-to-1 computation with placeholders."""
        if len(name) == 1:  # This is an operator
            title = f"{{0}}{name}{{1}}"
        else:
            title = f"{name}({{0}}, {{1}})"
        if suffix:
            title += "|" + suffix
        return title


# Global default title formatter
_default_title_formatter: TitleFormatter = SimpleTitleFormatter()


def get_default_title_formatter() -> TitleFormatter:
    """Get the current default title formatter.

    Returns:
        Current default title formatter
    """
    return _default_title_formatter


def set_default_title_formatter(formatter: TitleFormatter) -> None:
    """Set the default title formatter.

    Args:
        formatter: Title formatter to use as default
    """
    global _default_title_formatter  # pylint: disable=global-statement
    _default_title_formatter = formatter