Ruby-Python; using python from within ruby

Discussion in 'Ruby' started by Norjee, Jul 30, 2005.

  1. Norjee

    Norjee Guest

    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.
     
    Norjee, Jul 30, 2005
    #1
    1. Advertising

  2. Norjee

    Jim Freeze Guest

    * Norjee <> [2005-07-30 23:01:02 +0900]:

    > 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

    --
    Jim Freeze
     
    Jim Freeze, Jul 30, 2005
    #2
    1. Advertising

  3. Norjee

    Norjee Guest

    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.
     
    Norjee, Jul 30, 2005
    #3
  4. 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.



    --
    Best regards, emailto: scholz at scriptolutions dot com
    Lothar Scholz http://www.ruby-ide.com
    CTO Scriptolutions Ruby, PHP, Python IDE 's
     
    Lothar Scholz, Jul 30, 2005
    #4
  5. Norjee

    Norjee Guest

    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 ;)
     
    Norjee, Jul 30, 2005
    #5
  6. Norjee

    Norjee Guest

    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 ;)
     
    Norjee, Jul 30, 2005
    #6
  7. On Sun, Jul 31, 2005 at 03:41:03AM +0900, Norjee wrote:
    > 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 <> - http://www.foton.es
    EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es
     
    Esteban Manchado Velázquez, Jul 31, 2005
    #7
  8. Norjee

    Norjee Guest

    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.
     
    Norjee, Jul 31, 2005
    #8
  9. Norjee

    Norjee Guest

    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.
     
    Norjee, Jul 31, 2005
    #9
  10. 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()
     
    Lyndon Samson, Aug 2, 2005
    #10
  11. Norjee

    Norjee Guest

    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.
     
    Norjee, Aug 3, 2005
    #11
  12. Norjee

    Norjee Guest

    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.
     
    Norjee, Aug 3, 2005
    #12
  13. Norjee wrote:

    >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
     
    Devin Mullins, Aug 3, 2005
    #13
  14. On 8/3/05, Norjee <> wrote:
    > 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
     
    Lyndon Samson, Aug 3, 2005
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KatB
    Replies:
    0
    Views:
    455
  2. mark4asp
    Replies:
    2
    Views:
    2,243
    Harry Haller
    Nov 7, 2006
  3. Elmo Watson
    Replies:
    1
    Views:
    607
    Eliyahu Goldin
    May 8, 2007
  4. Ultrus
    Replies:
    3
    Views:
    390
    Stefan Behnel
    Jul 9, 2007
  5. Abe
    Replies:
    2
    Views:
    177
Loading...

Share This Page