473,509 Members | 11,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Embedding OCaml within Python

I've spent an interesting day searching the web for help with calling
OCaml functions from a python program and have come to the newsgroup
for some advice (unfortunately I'm learning both languages at the same
time, so maybe I've missed something obvious!)

The situation is this; I've got a large legacy program that is written
in ocaml, within which there's a parser. I'd like to be able to call
that parser from my python program (actually it's really a GUI using
wxPython), rather than reimplement it (and then have to maintain two
parsers into the future).

I've come across Viper, which looks like a python compiler/interpreter
implemented in OCaml, so I imagine that I could get it to do some
magic tricks to call a ocaml library from python, but I think that's
doomed to failure because of the C extensions of wxPython.

I've stumbled across Pycaml, but don't quite understand how to use it
to call ocaml from python. Alternatively I think I can wrap the ocaml
library in a C wrapper and then call that from python.

Can anyone give advice/share experiences? Is combining OCaml with
python relatively straightforward with a bit more effort, or am I in
for a world of pain?

Thanks,
Max
Jul 18 '05 #1
5 3504
Max Powers wrote:
I've spent an interesting day searching the web for help with calling
OCaml functions from a python program and have come to the newsgroup
for some advice


I stumbled over a similar task few months ago. I found no direct way to call
OCaml functions from Python directly (maybe there is and I didn't find it),
but I solved it by calling the OCaml code from C which itself is an
extension loaded by Python.

Python -> C extension -> OCaml -> callback to C -> callback to Python

This is sort of ugly, but works. Actually Python is supposed to be the glue
language, not C. :)

The passed values are simple types, not classes or arrays, so this isn't
overly complex, but can probably get if you need to pass non-trivial values
because C needs to translate between OCaml and Python types then.

I implemented the above without SWIG or similar tools, simply because I
prefer to write the boilerplate code at least once to understand how it
works. Plans are to use SWIG later.

I approached the task by writing some C program which calls the OCaml stuff
and implements required callbacks first. Then later adding the interface to
Python. It actually isn't that complex if you try to treat the Python <-> C
and the C <-> OCaml parts individually. Both Python and OCaml have excellent
documentation covering interfacing with C.

In reply to your last remark, I think you are not in a world of pain. But
it's probably not the most trivial thing to do either.

I would appreciate pointers how to do this in a more elegant way, though.
I am in no way an OCaml expert, so chances are high I missed something here.

Peter
Jul 18 '05 #2
Max Powers wrote:
I've spent an interesting day searching the web for help with calling
OCaml functions from a python program and have come to the newsgroup
for some advice (unfortunately I'm learning both languages at the same
time, so maybe I've missed something obvious!)

The situation is this; I've got a large legacy program that is written
in ocaml, within which there's a parser. I'd like to be able to call
that parser from my python program (actually it's really a GUI using
wxPython), rather than reimplement it (and then have to maintain two
parsers into the future).
I may say something stupid but... I suppose this OCaml program has a CLI
? If so, can't you just call the OCaml program from your wxPython GUI
and get result back via a pipe ?
I've come across Viper, which looks like a python compiler/interpreter
implemented in OCaml, so I imagine that I could get it to do some
magic tricks to call a ocaml library from python, but I think that's
doomed to failure because of the C extensions of wxPython.
AFAIK, wxWidget is C++, not C...
I've stumbled across Pycaml, but don't quite understand how to use it
to call ocaml from python. Alternatively I think I can wrap the ocaml
library in a C wrapper and then call that from python.
For what I saw (30' fast scan of the Pycaml page), it seems you would
need to write OCaml wrappers for your legacy OCaml code. But I dont have
any working experience in this kind of stuff, so I may have it all wrong.
Can anyone give advice/share experiences? Is combining OCaml with
python relatively straightforward with a bit more effort, or am I in
for a world of pain?
I guess you could ask help from the OCaml community too...
Thanks,

Err... Not sure I'm really helpful here :(

Bruno

Jul 18 '05 #3
Max Powers wrote:
The situation is this; I've got a large legacy program that is written
in ocaml, within which there's a parser. I'd like to be able to call
that parser from my python program
Doesn't the O'Caml compiler translate the code into C first?

If you got it to generate C code for your your library, chances are you
could dig into it and wrap whatever you got there with SWIG or BPL, and
then compile both the wrapper and the lib in C.

Then,
Python -> C extension -> OCaml -> callback to C -> callback to Python


becomes

Python -> C extension (O'Caml translation + callback to Python)
Of course, it might be the case that the O'Caml C code is too
complicated or too far removed from the O'Caml object interface...

hth

kuba

Jul 18 '05 #4
ma***********@hotmail.com (Max Powers) writes:
Alternatively I think I can wrap the ocaml library in a C wrapper
and then call that from python.


Not knowing much about the situation, this is probably the simplest
thing to do... depends on how complicated the data you want to pass
between the two parts of the program is.

Cheers,
mwh

--
... Windows proponents tell you that it will solve things that
your Unix system people keep telling you are hard. The Unix
people are right: they are hard, and Windows does not solve
them, ... -- Tim Bradshaw, comp.lang.lisp
Jul 18 '05 #5
Jakub Fast wrote:
Max Powers wrote:
The situation is this; I've got a large legacy program that is written
in ocaml, within which there's a parser. I'd like to be able to call
that parser from my python program

Doesn't the O'Caml compiler translate the code into C first?

AFAIK, no, it's a stand-alone native code compiler.

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
562
by: Pecks | last post by:
Hi, I'm having problems embedding Python in C++. (Background - I'm hacking a COTS app written in C++, and I'd like to write in some intelligence in Python) I understand that I can call a...
3
3799
by: Phil Frost | last post by:
Greetings all. I'm attempting to embed a python interpreter at a very low level in an OS I am writing. Currently I'm stuck with build issues. Firstly, as there is no working C compiler in our OS, I...
4
2760
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
1
3162
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
4
1863
by: Jelle Feringa // EZCT / Paris | last post by:
After reading about extending python with C/Fortran in the excellent Python Scripting for Computational Science book by Hans Langtangen, I'm wondering whether there's not a more pythonic way of...
0
1165
by: Fozzie | last post by:
Hi, I have a problem which is quite circular, and hopefully either someone has encountered something similar or has a reason why this will not work. We have a COM library providing...
6
2981
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
2
3189
by: Gigs_ | last post by:
Is there any way to convert ocaml code to python? but not manually thx
5
3796
by: Stefan Bellon | last post by:
Hi all! I am embedding Python into a GUI application in a way that the GUI is scriptable using Python. Now I have come to a problem that when the user puts a "sys.exit(0)" into his script to...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.