Home / Function/ format_str() — anthropic-sdk-python Function Reference

format_str() — anthropic-sdk-python Function Reference

Architecture documentation for the format_str() function in ruffen-docs.py from the anthropic-sdk-python codebase.

Function python AnthropicClient SyncAPI calls 1 called by 1

Entity Profile

Dependency Diagram

graph TD
  1ef0ada9_3a11_ea8f_5e8f_176327d79666["format_str()"]
  3965d879_b0ae_9368_c20d_7ddc509b318b["ruffen-docs.py"]
  1ef0ada9_3a11_ea8f_5e8f_176327d79666 -->|defined in| 3965d879_b0ae_9368_c20d_7ddc509b318b
  b4018bb7_0839_2d9d_220a_52ca08d1a87c["format_file()"]
  b4018bb7_0839_2d9d_220a_52ca08d1a87c -->|calls| 1ef0ada9_3a11_ea8f_5e8f_176327d79666
  62c685ea_e05a_24b8_aec4_0a1e17749e4f["format_code_block()"]
  1ef0ada9_3a11_ea8f_5e8f_176327d79666 -->|calls| 62c685ea_e05a_24b8_aec4_0a1e17749e4f
  style 1ef0ada9_3a11_ea8f_5e8f_176327d79666 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

scripts/utils/ruffen-docs.py lines 33–104

def format_str(
    src: str,
) -> tuple[str, Sequence[CodeBlockError]]:
    errors: list[CodeBlockError] = []

    @contextlib.contextmanager
    def _collect_error(match: Match[str]) -> Generator[None, None, None]:
        try:
            yield
        except Exception as e:
            errors.append(CodeBlockError(match.start(), e))

    def _md_match(match: Match[str]) -> str:
        code = textwrap.dedent(match["code"])
        with _collect_error(match):
            code = format_code_block(code)
        code = textwrap.indent(code, match["indent"])
        return f"{match['before']}{code}{match['after']}"

    def _pycon_match(match: Match[str]) -> str:
        code = ""
        fragment = cast(Optional[str], None)

        def finish_fragment() -> None:
            nonlocal code
            nonlocal fragment

            if fragment is not None:
                with _collect_error(match):
                    fragment = format_code_block(fragment)
                fragment_lines = fragment.splitlines()
                code += f"{PYCON_PREFIX}{fragment_lines[0]}\n"
                for line in fragment_lines[1:]:
                    # Skip blank lines to handle Black adding a blank above
                    # functions within blocks. A blank line would end the REPL
                    # continuation prompt.
                    #
                    # >>> if True:
                    # ...     def f():
                    # ...         pass
                    # ...
                    if line:
                        code += f"{PYCON_CONTINUATION_PREFIX} {line}\n"
                if fragment_lines[-1].startswith(" "):
                    code += f"{PYCON_CONTINUATION_PREFIX}\n"
                fragment = None

        indentation = None
        for line in match["code"].splitlines():
            orig_line, line = line, line.lstrip()
            if indentation is None and line:
                indentation = len(orig_line) - len(line)
            continuation_match = PYCON_CONTINUATION_RE.match(line)
            if continuation_match and fragment is not None:
                fragment += line[continuation_match.end() :] + "\n"
            else:
                finish_fragment()
                if line.startswith(PYCON_PREFIX):
                    fragment = line[len(PYCON_PREFIX) :] + "\n"
                else:
                    code += orig_line[indentation:] + "\n"
        finish_fragment()
        return code

    def _md_pycon_match(match: Match[str]) -> str:
        code = _pycon_match(match)
        code = textwrap.indent(code, match["indent"])
        return f"{match['before']}{code}{match['after']}"

    src = MD_RE.sub(_md_match, src)
    src = MD_PYCON_RE.sub(_md_pycon_match, src)
    return src, errors

Subdomains

Called By

Frequently Asked Questions

What does format_str() do?
format_str() is a function in the anthropic-sdk-python codebase, defined in scripts/utils/ruffen-docs.py.
Where is format_str() defined?
format_str() is defined in scripts/utils/ruffen-docs.py at line 33.
What does format_str() call?
format_str() calls 1 function(s): format_code_block.
What calls format_str()?
format_str() is called by 1 function(s): format_file.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free