package py

  1. Overview
  2. Docs
Ctypes bindings to Python 3.5 or greater

Install

Dune Dependency

Authors

Maintainers

Sources

v1.2.tar.gz
md5=2c6774d3156e994182753394a7ad624d
sha512=94f06d73d079d7c715b596de73eab884560dca98e36a5b6257b41d51d58b99aa724edd43ea1e17e33d4fe007b2926be0b5e0b8ebc882297014b46602adb21c30

Description

Tags

python

Published: 06 Mar 2019

README

py — OCaml interface to Python

%%VERSION%%

py is a ctypes interface to Python 3.5+ for OCaml

py is distributed under the ISC license.

Homepage: https://github.com/zshipko/ocaml-py

Installation

py can be installed with opam:

$ opam install py

If your Python installation is not in the typical location you may have to set OCAML_PY_VERSION to point to the Python .so file.

For example, one way of finding this path:

$ find `python3 -c 'import sys, os; print(os.path.join(sys.prefix, "lib"))'` -name 'libpython*.so'

(That seems to be the most straight forward way, but let me know if there's something better!)

If you'd like to run the tests:

$ dune runtest

Introduction

Simple conversion from OCaml to Python:

    open Py
    let s = !$(String "a string")
    let f = !$(Float 12.3)
    let i = !$(Int 123)

See src/py.mli for a full list of types.

Call a function defined in a module and return the result:

    let np = PyModule.import "numpy" in
    let np_array = np $. (String "array") in
    let arr = np_array $ [List [Int 1; Int 2; Int 3]] in
    ...

Which is shorthand for

    let np = PyModule.import "numpy" in
    let np_array = Object.get_attr_s np "array" in
    let arr = run np_array [List [Int 1; Int 2; Int 3]] in
    ...

Evaluate a string and return the result:

    let arr = eval "[1, 2, 3]" in
    ...

Get object index:

    let a = arr $| Int 0 in
    let b = arr $| Int 1 in
    let c = arr $| Int 2 in
    ...

Set object index:

    let _ = (a_list, Int 0) <-$| Int 123 in
    let _ = (a_dict, String "key") <-$| String "value" in
    ...

Execute a string and return true/false depending on the status returned by Python:

    if exec "import tensorflow" then
        let tf = PyModule.get "tensorflow" in  (* Load an existing module *)
        ...

Dependencies (5)

  1. conf-python-3-dev
  2. ctypes-foreign >= "0.4.0"
  3. ctypes >= "0.13.0"
  4. dune
  5. ocaml >= "4.05.0"

Dev Dependencies

None

Used by

None

Conflicts

None