kapps_triplestore_interface.utils.pretty_print#

kapps_triplestore_interface.utils.pretty_print.shorten_block(s: str) → str[source]#

Search a text for IRI patterns with known namespaces and shorten them.

kapps_triplestore_interface.utils.pretty_print.format_result(result: list[tuple[str]] | tuple[tuple[str, ...], ...] | dict[Any, Any] | list[dict], variables: list[str] | None = None, grouping_variables: list[str] | None = None) → str[source]#

Return a human-readable string for SPARQL query results.

This utility formats different shapes of results produced by SPARQL SELECT queries into an easy-to-read textual representation used in logs or CLI output.

Parameters:
  • result (list[tuple[str]] | tuple[tuple[str, ...], ...] | dict[Any, Any] | list[dict]) –

    One of the following structures:

    • tuple of tuples: tabular rows, where each inner tuple corresponds to a row ordered by variables.

    • tuple of scalars: a single-column result (when variables has length 1).

    • dict: a nested mapping keyed by the values of grouping_variables where the leaves are tuples (or empty tuples) representing the non-grouped variables.

    • list[dict]: raw JSON bindings from a SPARQL endpoint (each dict is a binding); this is rendered as a compact pretty-printed JSON-like list.

  • variables (Optional[list[str]]) – Non-grouped variable names (without leading ?) that define the order and width of columns when rendering tabular/tuple results.

  • grouping_variables (Optional[list[str]]) – Grouped variable names (without leading ?) that define the nesting order for dictionary results. When present, headers are annotated by the current grouping key at each level.

Returns:

A formatted multi-line string. For empty inputs the string explicitly indicates which variables and/or grouping variables were expected.

Return type:

str

Notes

  • This function does not mutate the input data.

  • Widths for tabular output are computed from both headers (variables) and the values.