Giter Site home page Giter Site logo

jonathan-laurent / pythonlib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from janestreet/pythonlib

0.0 2.0 0.0 186 KB

A library to help writing wrappers around ocaml code for python

License: MIT License

Makefile 0.18% OCaml 97.20% C 1.07% Python 1.55%

pythonlib's Introduction

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_pi

This 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.