473,626 Members | 3,427 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 3513
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***********@h otmail.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 python objects method using PyImport_ImportModule, PyObject_GetAttrString and PyObject_CallMethod.
3
3810
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 must cross compile Python. I encountered errors when building Parser/pgen; I got around this by building it nativly and copying it. Hopefully this won't get me later... My current problem (and there are sure to be more ;) ) is when building...
4
2775
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 working with Python for a year or more now, but only doing simple extending in C/C++. I'm now attempting some embedding and several questions have come to mind. BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
1
3172
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 looking for, but nothing that really solves or explains all this. I'll admit to being stumped, hence my question here. I'm also trying to make this post as clear and detailed as possible. Unfortunately, that means it's come out like a book. I...
4
1870
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 extending python. And frankly I think there is: OCAML Fortunately there is already a project up and running allowing you to extend python with OCAMl: http://pycaml.sourceforge.net/ Since I haven't got actual experience programming CAML I'd like...
0
1172
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 mathematics to various systems, most functions are hard-coded but we want to embed a scripting language to allow arbitrary functions to be used in the numeric engines within the library, and am using Python for this.
6
2996
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 interface is providing a convenient scripting toolkit for the application. One example is that a user can write a python script like: player = Player() game.loadPlayer(player) player.moveTo(location)
2
3206
by: Gigs_ | last post by:
Is there any way to convert ocaml code to python? but not manually thx
5
3805
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 end the script, not only the script is terminated, but also the GUI application itself. This is not the intended behaviour. As in Python itself you can catch SystemExit, I think this should be
0
8272
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8713
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8644
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8514
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6126
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5579
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2632
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 we have to send another system
1
1817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.