Manually writing bindings to Python libraries can get tedious pretty quickly.
pyml_bindgen aims to help you avoid a lot of the repetitive work when binding Python libraries by letting you focus on the OCaml side of things and (mostly) not worrying about the implementation of the pyml bindings.
pyml_bindgen. It is available on Opam.
$ opam install pyml_bindgen
Say you have a Python class you want to bind and use in OCaml. (Filename:
class Adder: @staticmethod def add(x, y): return x + y
To do so, you write OCaml value specifications for the class and methods you want to bind. (Filename:
val add : x:int -> y:int -> unit -> int
Then, you run
$ pyml_bindgen val_specs.txt adder Adder --caml-module Adder > lib.ml
Now you can use your generated functions in your OCaml code. (Filename:
open Lib let () = Py.initialize () let result = Adder.add ~x:1 ~y:2 () let () = assert (result = 3)
Finally, set up a dune file and run it.
(executable (name run) (libraries pyml))
$ dune exec ./run.exe
For more information on installing and using
pyml_bindgen, check out the docs. There you will find lots of tips and examples to help you get started!