code: def to_format(options, view, output_format):
      view._table._state_manager.call_process(
          view._table._table.get_id()
      )
      options, column_names, data_slice = _to_format_helper(
          view, options
      )
  
      if output_format == "records":
          data = []
      elif output_format in ("dict", "numpy"):
          data = {}
          if options["index"]:
              data["__INDEX__"] = []
          if options["id"]:
              data["__ID__"] = []
  
      num_columns = len(view._config.get_columns())
      num_hidden = view._num_hidden_cols()
  
      for ridx in range(options["start_row"], options["end_row"]):
          row_path = (
              data_slice.get_row_path(ridx)
              if options["has_row_path"]
              else []
          )
          if options["leaves_only"] and len(row_path) < len(
              view._config.get_group_by()
          ):
              continue
  
          if output_format == "records":
              data.append({})
  
          for cidx in range(options["start_col"], options["end_col"]):
              name = column_names[cidx]
  
              if (
                  _mod(
                      (cidx - (1 if view._sides > 0 else 0)),
                      (num_columns + num_hidden),
                  )
                  >= num_columns
              ):
                  # don't emit columns used for hidden sort
                  continue
              elif cidx == options["start_col"] and view._sides > 0:
                  if options["has_row_path"]:
                      paths = [
                          scalar_to_py(path, False, False)
                          for path in reversed(row_path)
                      ]
                      if output_format == "records":
                          data[-1]["__ROW_PATH__"] = paths
                          if options["id"]:
                              data[-1]["__ID__"] = paths
                      elif output_format in ("dict", "numpy"):
                          if "__ROW_PATH__" not in data:
                              data["__ROW_PATH__"] = []
                          data["__ROW_PATH__"].append(paths)
                          if options["id"]:
                              data["__ID__"].append(paths)
              else:
                  if output_format in ("dict", "numpy") and (
                      name not in data
                  ):
                      # TODO: push into C++ for numpy
                      data[name] = []
                  if view._is_unit_context:
                      value = get_from_data_slice_unit(
                          data_slice, ridx, cidx
                      )
                  elif view._sides == 0:
                      value = get_from_data_slice_zero(
                          data_slice, ridx, cidx
                      )
                  elif view._sides == 1:
                      value = get_from_data_slice_one(
                          data_slice, ridx, cidx
                      )
                  else:
                      value = get_from_data_slice_two(
                          data_slice, ridx, cidx
                      )
  
                  if output_format == "records":
                      data[-1][name] = value
                  else:
                      data[name].append(value)
  
          if options["index"]:
              if view._is_unit_context:
                  pkeys = get_pkeys_from_data_slice_unit(
                      data_slice, ridx, cidx
                  )
              elif view._sides == 0:
                  pkeys = get_pkeys_from_data_slice_zero(
                      data_slice, ridx, 0
                  )
              elif view._sides == 1:
                  pkeys = get_pkeys_from_data_slice_one(
                      data_slice, ridx, 0
                  )
              else:
                  pkeys = get_pkeys_from_data_slice_two(
                      data_slice, ridx, 0
                  )
  
              if output_format == "records":
                  data[-1]["__INDEX__"] = []
                  for pkey in pkeys:
                      data[-1]["__INDEX__"].append(pkey)
              elif output_format in ("dict", "numpy"):
                  # ensure that `__INDEX__` has the same number of rows as
                  # returned dataset
                  if len(pkeys) == 0:
                      data["__INDEX__"].append([])
                  for pkey in pkeys:
                      data["__INDEX__"].append([pkey])
  
          if options["id"] and (
              view._is_unit_context or view._sides == 0
          ):
              if view._is_unit_context:
                  pkeys = get_pkeys_from_data_slice_unit(
                      data_slice, ridx, 0
                  )
              else:
                  pkeys = get_pkeys_from_data_slice_zero(
                      data_slice, ridx, 0
                  )
  
              if output_format == "records":
                  data[-1]["__ID__"] = []
                  for pkey in pkeys:
                      data[-1]["__ID__"].append(pkey)
              elif output_format in ("dict", "numpy"):
                  if len(pkeys) == 0:
                      data["__ID__"].append([])
                  for pkey in pkeys:
                      data["__ID__"].append([pkey])
  
      if output_format in ("dict", "numpy") and (
          not options["has_row_path"] and ("__ROW_PATH__" in data)
      ):
          del data["__ROW_PATH__"]
  
      if output_format == "numpy":
          for k, v in data.items():
              # TODO push into C++
              if k == "__ROW_PATH__":
                  data[k] = np.array(v, dtype="object")
              else:
                  data[k] = np.array(v)
  
      return data
  
imports: import numpy
  from math import floor, ceil, trunc
  from ._constants import COLUMN_SEPARATOR_STRING
  from .libpsppy import get_data_slice_unit, get_data_slice_zero, get_data_slice_one, get_data_slice_two, get_from_data_slice_unit, get_from_data_slice_zero, get_from_data_slice_one, get_from_data_slice_two, get_pkeys_from_data_slice_unit, get_pkeys_from_data_slice_zero, get_pkeys_from_data_slice_one, get_pkeys_from_data_slice_two, scalar_to_py
label_desc: Use image node_id 2 for calling a global function with example usage: to_format(options, view, output_format) and returns: data