Day 58 — I miss the Python standard library

Today I worked on translating cutypr to Rust.

I found that I could use pyo3 to start the IPython kernel from Rust! So I wrote a start_kernel() Python function to start the IPython kernel, returned some useful information (the key required to sign messages + ports for all kernel channels) from it, and then called it from Rust using pyo3!


  fn start_kernel(py: Python) -> Value {
      let locals = [("jupyterm", py.import("jupyterm").unwrap())].into_py_dict(py);
      let code = "jupyterm.start_kernel()";
      let kernel_info_str: &str = py
          .eval(code, None, Some(&locals))
          .unwrap()
          .extract()
          .unwrap();
      let kernel_info: Value = serde_json::from_str(kernel_info_str).unwrap();
      kernel_info
  }

  fn main() {
      let mut kernel_info: Value = serde_json::from_str("{}").unwrap();

      // start the Python kernel
      // TODO: also shut it down
      Python::with_gil(|py| {
          kernel_info = start_kernel(py);
      });
  }

I still need to figure out how to shutdown the kernel after everything is done though!

After that I looked into how to create a REPL in Rust. These are some things I missed from Python and its standard library:


  use std::io::{self, Write};

  fn main() {
      let mut code = String::new();
      loop {
          code.clear();

          print!(">>> ");
          io::stdout().flush().unwrap();
          io::stdin().read_line(&mut code).unwrap();
      }
  }


  fn print_type_of(_: &T) {
      println!("{}", std::any::type_name::())
  }


  >> print_type_of(&code_bytes);
  generic_array::GenericArray, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>>