GithubHelp home page GithubHelp logo

mooreryan / ocaml_python_bindgen Goto Github PK

View Code? Open in Web Editor NEW
30.0 3.0 2.0 5.97 MB

Generate Python bindings via pyml from OCaml value specifications

Home Page: https://mooreryan.github.io/ocaml_python_bindgen/

License: Apache License 2.0

Makefile 0.80% OCaml 94.66% Shell 0.08% Python 1.40% HTML 0.75% Perl 2.32%
ocaml python binding-generator bindings

ocaml_python_bindgen's Introduction

OCaml-Python Bindings Generator

Build and test Coverage Status

Generate Python bindings with pyml directly from OCaml value specifications.

While you could write all your Python bindings by hand, it can be tedious and it gets old real quick. While pyml_bindgen can't yet auto-generate all the bindings you may need, it can definitely take care of a lot of the tedious and repetitive work you need to do when writing bindings for a big Python library!! ๐Ÿ’–

Quick start

First, install 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: adder.py)

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_specs.txt)

val add : x:int -> y:int -> unit -> int

Then, you run pyml_bindgen.

$ pyml_bindgen val_specs.txt adder Adder --caml-module Adder > lib.ml

Now you can use your generated functions in your OCaml code. (Filename: run.ml)

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

Documentation

For information on installing and using pyml_bindgen, check out the docs.

Additionally, you can find examples in the examples directory. One neat thing about these examples is that you can see how to write Dune rules to automatically generate your pyml bindings.

You may also want to check out my blog post introducing pyml_bindgen.

Installing from sources

If you want to install from sources, e.g., to track the main branch or a development branch, but you do not want to install all the test and development packages, clone the repository, checkout the branch you want to follow and install:

$ git clone https://github.com/mooreryan/ocaml_python_bindgen.git
# Checkout whatever branch you want, in this case `dev`.
$ git checkout dev
$ make opam_install

This will save a lot of install time as it avoids some heavy packages.

Development

If instead, you want to work on pyml_bindgen development, will need to ensure you have the development and test dependencies.

E.g.,

$ git clone https://github.com/mooreryan/ocaml_python_bindgen.git
$ make deps
$ make deps_dev
# Start working!
$ dune test

License

license MIT or Apache 2.0

Copyright (c) 2021 - 2022 Ryan M. Moore.

Licensed under the Apache License, Version 2.0 or the MIT license, at your option. This program may not be copied, modified, or distributed except according to those terms.

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.