package pythonlib
Build not checkedInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=e7f789218f633e97468555dcb0453d8c8d871061c5c680a516416844859d2a4e
Description
This library helps exposing ocaml functions to python. The python runtime interaction is handled by pyml.
Added to opam-repository:
README
pythonlib makes it easier to write wrappers around ocaml functions so that they can be called from python.
Example
This example is taken from the examples directory. The ocaml code defines a function that takes as argument an integer n, performs some computations based on n and return a float value. This function is attached to a newly defined python module named ocaml_module.
open Base
let approx_pi =
let%map_open.Python_lib n = positional "n" int ~docstring:""
in
let sum =
List.init n ~f:(fun i -> let i = Float.of_int (1 + i) in 1.0 /. (i *. i))
|> List.reduce_exn ~f:(+.)
in
Float.sqrt (sum *. 6.) |> python_of_float
let () =
if not (Py.is_initialized ())
then Py.initialize ();
let mod_ = Py_module.create "example_module" in
Py_module.set mod_ "approx_pi" approx_piThis code is compiled to a static library ocaml.so, together with a small C library defining the PyInit_ocaml function that starts the ocaml runtime and exposes the example module. The python code then imports this library and can use the ocaml functions.
# This requires the ocaml.bc.so file to be copied as ocaml.so in the python path
from ocaml import example_module, toploop
# Import the module defined in the ocaml code and run the function.
import ocaml_module
print(ocaml_module.approx_pi(1000))pythonlib also handles keyword arguments as well as basic types such as int, float, string, list, etc. Further examples can be found in the examples directory.
Dependencies (9)
-
pyml
>= "20211015" -
dune
>= "2.0.0" -
typerep
>= "v0.15" & < "v0.16" -
stdio
>= "v0.15" & < "v0.16" -
ppx_python
>= "v0.15" & < "v0.16" -
ppx_let
>= "v0.15" & < "v0.16" -
ppx_expect
>= "v0.15" & < "v0.16" -
base
>= "v0.15" & < "v0.16" -
ocaml
>= "4.11.0" & < "4.13.0"
Dev Dependencies
None
Optional Dependencies
None
Used by
None
Conflicts
None
Build status
Results of building pythonlib with each compiler on check.ci.ocaml.org, run of against opam-repository 8d8d144. All results for this package.
Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.
This version is not in the latest run; the versions below are.
- good Package successfully built
- partial One of the dependencies failed to build
- bad Package failed to build
- n/a Package is not available in this environment
- internal Internal failure