Ruby-Python; using python from within ruby

N

Norjee

At the moment I'm looking at rails, it seems like a great framework.
The only downside it has is its rather limited availability of 3rd
party libraries. At the moment there are at least two libraries I need
that are available to python but not to ruby (search engine, either
xapian or lucene( python has bindings to both, ruby has neither), pydns
and another custom python script)

After looking for a solution I came across ruby-python
(http://raa.ruby-lang.org/project/ruby-python/). Sadly enough it is a
little bit outdated. It seems to need ruby 2.6 and python 1.5 while
I'm using ruby 1.82 and python 2.4. I tried looking into the source
code, changing reference to python 1.5 to python 2.4, changed
PyObject_HEAD_INIT(&PyType_Type) to PyObject_HEAD_INIT(NULL) and set
ob_type to &PyType_Type in the init method (the new gcc compiler
doesn't like non constant elements outside function calls) and even got
it to compile. However, this is about as far as my knowledge of C goes,
and i end up with a segmentation fault. Thus somewhere there's a
pointer or reference not pointing to the right spot. I have absolutely
no clue how to solve this.

Are there any other libraries which allow ruby to execute python code,
or has anybody got ruby-python to work with recent versions of both
ruby and python?
If there is no solution i could of course shell execution to run some
python scripts, using that output, but I'd rather not do this as
it'll create quite some messy interaction. Moreover, I think having
ruby able to run use python libraries would definitely help others to
adapt to using ruby, as I'm sure that the lack of libraries is
holding them back to use ruby, just as it does for me. Being able to
use at least a part of the libraries available to python gives ruby a
lot more libraries, with just having one wrapper library.
 
J

Jim Freeze

* Norjee said:
At the moment I'm looking at rails, it seems like a great framework.
The only downside it has is its rather limited availability of 3rd
party libraries. At the moment there are at least two libraries I need
that are available to python but not to ruby (search engine, either
xapian or lucene( python has bindings to both, ruby has neither), pydns
and another custom python script)

You may want to check out Ruby Odeum:

http://www.zedshaw.com/projects/ruby_odeum/
http://www.zedshaw.com/projects/ruby_odeum/performance.html
 
N

Norjee

I'll take a look at odeum. Though that'll only solve a part of the
things i've been trying (though i have to admit, it'd be the most
complicated one). But still i'd really like to have the ability to use
python code inside ruby, just for the simple reason to extend the
availabilty of 3rd party libraries to ruby.
 
L

Lothar Scholz

Hello Norjee,

N> If there is no solution i could of course shell execution to run some
N> python scripts, using that output, but I'd rather not do this as
N> it'll create quite some messy interaction. Moreover, I think having
N> ruby able to run use python libraries would definitely help others to
N> adapt to using ruby, as I'm sure that the lack of libraries is
N> holding them back to use ruby, just as it does for me. Being able to
N> use at least a part of the libraries available to python gives ruby a
N> lot more libraries, with just having one wrapper library.

Instead of trying to generate something generic you maybe should
simply write a C extension that includes python.h and ruby.h and wrap
the API calls. If it is not very performance critical (i really think
it is not) you can simply generate the connectivity by passing string
arguments from one side to the other, together with a little parameter
encoding/decoding.

And yes, this requires C and gives you a good way to learn more about
it. Having knowlegde of C is IMHO an absolute "must have" for every
programmer.
 
N

Norjee

I kind of got the ruby-python module to work! Sadly enough, I'm still
only vaguely aware of how it works. And I haven't thoroughly tested
things.

But for now, it appears to be quite usable for my situation. I can
execute my custom python modules from within Ruby, what more could I
wish to achieve ;)
 
N

Norjee

I kind of got the ruby-python module to work! Sadly enough, I'm still
only vaguely aware of how it works. And I haven't thoroughly tested
things.

But for now, it appears to be quite usable for my situation. I can
execute my custom python modules from within Ruby, what more could I
wish to achieve ;)
 
E

Esteban Manchado Velázquez

I kind of got the ruby-python module to work! Sadly enough, I'm still
only vaguely aware of how it works. And I haven't thoroughly tested
things.

Do you mind posting it somewhere? I'm sure many people would find it
interesting...

Is the original author definitely not interested in maintaining the mo=
dule?
Perhaps with a little help from other people?

--=20
Esteban Manchado Vel=E1zquez <[email protected]> - http://www.foton.es
EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es
 
N

Norjee

I already send my changes to the original author. It weren't really ig
changes. Only changing references to python1.5 to python2.4. Initialize
two PyObjects in the init method instead of doing that in their
definition, and finally i removed a check for duplicate modules, as it
marked all objects as duplicates. Especial the removal of this check
is, ofcourse, a bad thing.

To get it to compile in windows i also had to change to sequence in
which the headers were included. But under windows it nly works
Partially. I can exeucte python statements with the methods created for
the Py object (Py.eval for example), trying to work on a real Python
module (Py::Sys for example) crashes the whole thing.

As i said, i'm not really a programmer, and to have a full understandig
of the code one would need to understand both the ruby and python way
of interacting with C. As i know neither, i'm not the person to update
the module.
 
N

Norjee

I already send my changes to the original author. It weren't really big

changes. Only changing references to python1.5 to python2.4. Initialize

two PyObjects in the init method instead of doing that in their
definition, and finally I removed a check for duplicate modules, as it
marked all objects as duplicates. Especial the removal of this check
is, of course, a bad thing.
To get it to compile in windows I also had to change to sequence in
which the headers were included. But under windows it only works
partially. I can execute python statements with the methods created for

the Py object (Py.eval for example), trying to work on a real Python
module (Py::Sys for example) crashes the whole thing.
As I said, I'm not really a programmer, and to have a full
understanding
of the code one would need to understand both the ruby and python way
of interacting with C. As I know neither, I'm not the person to
update
the module.
 
L

Lyndon Samson

This seems to work for me, Windows only though...

require 'Win32API'

PyInitialize =3D Win32API.new("python24", "Py_Initialize", [], 'V')
PyRun_SimpleString =3D Win32API.new("python24", "PyRun_SimpleString", ['P']=
, 'V')
Py_Finalize =3D Win32API.new("python24", "Py_Finalize", [], 'V')

pythonSrc =3D <<EOF

from time import time,ctime
print 'Hello at ',ctime(time())

EOF

PyInitialize.call()
PyRun_SimpleString.call(pythonSrc)
Py_Finalize.call()
 
N

Norjee

Thanks! That might come in handt sometime.

At the moment i manages to compile the ruby-python module againt my
windows installation. And guess what.. IT WORKS on both linux and
windows..

I still haven't got a reply from the original author though, so i'm not
sure whether or not, (and if i do where) i should publish my findings.
 
N

Norjee

Thanks! That might come in handy sometime. Though i mostly need my
stuff to run on linux. That it works on windows is a nice pluss, as i
develop on it ;)

But... I got ruby-python to run on win32. I messed up my entire windows
setup by installing all kinds compilers and ruby / python ports, but in
the end i got it working :)

I still have not recieved a response from the original author, so im
not sure what to do with my modifications. I might publish them
somewhere, though as i'm not able to maintain it that might not be too
usefull (in the past few day's i've really gone crazy reading
documentation that was just plain wrong. I don't want to add to that
mess). On the other hand, there might be more people like me that want
to use ruby, but the relatively little amount of avialable libraries
holds them back. Especial for those wanting to use Rails. (For plain
scripts one would ofcourse use "the right tool for the job", which
would be the language the libraries are written in). In that case this
extention might be a good solution for them too.
 
D

Devin Mullins

Norjee said:
At the moment there are at least two libraries I need
that are available to python but not to ruby (search engine, either
xapian or lucene( python has bindings to both, ruby has neither), pydns
and another custom python script)
While someone mentioned Odeum, nobody replied to your second. Ruby stdlib:
require 'resolv'

Resolv.getaddress("www.ruby-lang.org") #=> "210.163.138.100"
Resolv.getname("210.163.138.100") #=> "beryllium.ruby-lang.org"


Devin
 
L

Lyndon Samson

Thanks! That might come in handy sometime. Though i mostly need my
stuff to run on linux. That it works on windows is a nice pluss, as i
develop on it ;)
=20
But... I got ruby-python to run on win32. I messed up my entire windows
setup by installing all kinds compilers and ruby / python ports, but in
the end i got it working :)
=20
I still have not recieved a response from the original author, so im
not sure what to do with my modifications. I might publish them
somewhere, though as i'm not able to maintain it that might not be too
usefull (in the past few day's i've really gone crazy reading
documentation that was just plain wrong. I don't want to add to that
mess). On the other hand, there might be more people like me that want
to use ruby, but the relatively little amount of avialable libraries
holds them back. Especial for those wanting to use Rails. (For plain
scripts one would ofcourse use "the right tool for the job", which
would be the language the libraries are written in). In that case this
extention might be a good solution for them too.
=20
=20
=20
You could of course, await the arrival of python and ruby compilers
that target the parrot VM[1] :)

[1] www.parrotcode.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top