The Python bridge (37a, 37b)

A Python script is a capability: something you get from a directory you were granted, and pass to the code that may use it.

fn main(sys):
  match dir(sys, "."):
    Err(e) => print(e)
    Ok(d) =>
      match python(d, "helper.py"):        # needs --allow-python (and read access to ".")
        Err(e) => print(e)
        Ok(py) =>
          match py_call(py, "mean", "[[1, 2, 3, 4]]"):
            Ok(v) => print(v)             # "2.5": the result as JSON text
            Err(e) => print(e)            # "ValueError: ..." from the script

Any module, objects by handle (37b)

fn report(py: Py) -> Result[Unit, Str]:
  let pd = py_import(py, "pandas")?
  let df = py_invoke(py_get(pd, "DataFrame")?, [JObj({"units": JArr([JNum(12.0), JNum(7.0)])})])?
  let total = py_json(py_method(py_index(df, JStr("units"))?, "sum", [])?)?
  print(json_int(total) ?? 0)                     # 19
  Ok(())

fn main(sys):
  match py_start(sys):                            # needs --allow-python
    Ok(py) => print(report(py))
    Err(e) => print(e)

examples/python/pandas_report.tr builds a DataFrame from Trine records, groups it and brings the numbers back; a test runs it on the interpreter, the VM and native code.