code: def build_example(example, api="python"):
      """Build a code example.
  
      Take a dict from a classes _example_ specification (list of dicts) and
      build a string with an api or cmd example (for use in cmd help or
      docstring).
  
      Parameters
      ----------
      api : {'python', 'cmdline'}
          If 'python', build Python example for docstring. If 'cmdline', build
          cmd example.
  
      Returns
      -------
      ex : str
          Concatenated examples for the given class.
      """
      if api == "python":
          code_field = "code_py"
          indicator = ">"
      elif api == "cmdline":
          code_field = "code_cmd"
          indicator = "%"
      else:
          raise ValueError("unknown API selection: {}".format(api))
      if code_field not in example:
          # only show an example if it exist for the API
          return ""
      description = textwrap.fill(example.get("text"))
      # this indent the code snippet to get it properly rendered as code
      # we are not using textwrap.fill(), because it would not acknowledge
      # any meaningful structure/formatting of code snippets. Instead, we
      # maintain line content as is.
      code = dedent_docstring(example.get(code_field))
      needs_indicator = not code.startswith(indicator)
      code = textwrap.indent(
          code, " " * (5 if needs_indicator else 3)
      ).lstrip()
  
      ex = """{}::\n\n   {}{}\n\n""".format(
          description,
          # disable automatic prefixing, if the example already has one
          # this enables providing more complex examples without having
          # to infer its inner structure
          "{} ".format(indicator) if needs_indicator
          # maintain spacing to avoid undesired relative indentation
          else "",
          code,
      )
  
      return ex
  
imports: import inspect
  import logging
  import os
  import re
  import textwrap
  import warnings
  from abc import ABC, abstractmethod
  from functools import wraps
  from importlib import import_module
  from typing import Callable, Dict, Generator, TypeVar, TYPE_CHECKING
  import datalad
  from datalad import cfg
  from datalad.core.local.resulthooks import get_jsonhooks_from_config, match_jsonhook2result, run_jsonhook
  from datalad.distribution.dataset import Dataset, resolve_path
  from datalad.interface.common_opts import eval_params
  from datalad.interface.results import known_result_xfms
  from datalad.interface.utils import get_result_filter, keep_result, render_action_summary, xfm_result, _process_results
  from datalad.support.exceptions import CapturedException, IncompleteResultsError
  from datalad.utils import get_wrapped_class
label_desc: Use image node_id 10 for calling a global function with example usage: build_example(example, api) and returns: ex, str