python/ruby question..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bruce

    python/ruby question..

    hi...

    can someone point me to where/how i would go about calling a ruby app from a
    python app, and having the python app being able to get a returned value
    from the ruby script.

    something like

    test.py
    a = os.exec(testrub y.rb)


    testruby.py
    foo = 9
    return foo


    i know this doesn't work... but i've been searching for hours on this with
    no luck.... (and yeah, i'm relatively new to both ruby/python!!)

    thanks


  • Mensanator

    #2
    Re: python/ruby question..

    On Jun 18, 10:33�pm, "bruce" <bedoug...@eart hlink.netwrote:
    hi...
    >
    can someone point me to where/how i would go about calling a ruby app from a
    python app, and having the python app being able to get a returned value
    from the ruby script.
    >
    something like
    >
    test.py
    �a = os.exec(testrub y.rb)
    >
    testruby.py
    �foo = 9
    �return foo
    >
    i know this doesn't work... but i've been searching for hours on this with
    no luck.... (and yeah, i'm relatively new to both ruby/python!!)
    >
    thanks
    Well, I don't know anything about Ruby, but here's
    how I do it for C programs (compiled to .exe that
    write to stdout).


    import os
    factor_program = 'factor! -d200 ' # factor!.exe from MIRACL

    n =
    '50818429800343 305993022114330 311033271249313 957919046352679 206262204589342 623811236647989 889145173098650 749'

    # call external program and capture stdout
    the_output = os.popen(factor _program+n).rea dlines()

    print 'n: %s' % n
    for i in the_output:
    print i,

    ## n:
    508184298003433 059930221143303 110332712493139 579190463526792 062622045893426 238112366479898 891451730986507 49
    ## PRIME_FACTOR 37
    ## PRIME_FACTOR 43
    ## PRIME_FACTOR 167
    ## COMPOSITE_FACTO R 507787751
    ## PRIME_FACTOR 69847
    ## PRIME_FACTOR 30697
    ## PRIME_FACTOR 89017
    ## PRIME_FACTOR 3478697
    ## PRIME_FACTOR 434593
    ## PRIME_FACTOR 49998841
    ## PRIME_FACTOR 161610704597143
    ## PRIME_FACTOR 14064370273
    ## COMPOSITE_FACTO R 963039394703598 565337297
    ## PRIME_FACTOR 11927295803

    Comment

    • Matt Nordhoff

      #3
      Re: python/ruby question..

      Mensanator wrote:
      On Jun 18, 10:33�pm, "bruce" <bedoug...@eart hlink.netwrote:
      >hi...
      >>
      >can someone point me to where/how i would go about calling a ruby app from a
      >python app, and having the python app being able to get a returned value
      >from the ruby script.
      >>
      >something like
      >>
      >test.py
      >�a = os.exec(testrub y.rb)
      >>
      >testruby.py
      >�foo = 9
      >�return foo
      >>
      >i know this doesn't work... but i've been searching for hours on this with
      >no luck.... (and yeah, i'm relatively new to both ruby/python!!)
      >>
      >thanks
      >
      Well, I don't know anything about Ruby, but here's
      how I do it for C programs (compiled to .exe that
      write to stdout).
      >
      >
      import os
      factor_program = 'factor! -d200 ' # factor!.exe from MIRACL
      >
      n =
      '50818429800343 305993022114330 311033271249313 957919046352679 206262204589342 623811236647989 889145173098650 749'
      >
      # call external program and capture stdout
      the_output = os.popen(factor _program+n).rea dlines()
      >
      print 'n: %s' % n
      for i in the_output:
      print i,
      <snip output>

      You're supposed to use the subprocess module.

      In this case, something like:

      import subprocess
      factor_program = ['factor!', '-d200']

      ....

      p = subprocess.Pope n(factor_progra m + [n], stdout=subproce ss.PIPE)
      p.wait() # wait for it to finish; not sure how necessary it is
      the_output = p.stdout.readli nes()

      See subprocess's documentation [1], which includes guides on replacing
      os.popen* and other functions with it.

      [1] <http://docs.python.org/lib/module-subprocess.html >
      --

      Comment

      • Aahz

        #4
        Re: python/ruby question..

        In article <mailman.634.12 13870465.1044.p ython-list@python.org >,
        Matt Nordhoff <mnordhoff@matt nordhoff.comwro te:
        >
        >You're supposed to use the subprocess module.
        Really? Sez who?

        $ python
        Python 2.3.4 (#1, Feb 2 2005, 12:11:53)
        [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
        Type "help", "copyright" , "credits" or "license" for more information.
        >>import subprocess
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        ImportError: No module named subprocess
        --
        Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

        "as long as we like the same operating system, things are cool." --piranha

        Comment

        • Giampaolo Rodola'

          #5
          Re: python/ruby question..

          On 19 Giu, 18:49, a...@pythoncraf t.com (Aahz) wrote:
          In article <mailman.634.12 13870465.1044.p ython-l...@python.org >,
          Matt Nordhoff  <mnordh...@matt nordhoff.comwro te:
          >
          >
          >
          You're supposed to use the subprocess module.
          >
          Really?  Sez who?
          >
          $ python
          Python 2.3.4 (#1, Feb  2 2005, 12:11:53)
          [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
          Type "help", "copyright" , "credits" or "license" for more information.>>> import subprocess
          >
          Traceback (most recent call last):
            File "<stdin>", line 1, in ?
          ImportError: No module named subprocess
          --
          Aahz (a...@pythoncra ft.com)           <*       http://www.pythoncraft.com/
          >
          "as long as we like the same operating system, things are cool." --piranha
          That's because you're using a too old python version.


          --- Giampaolo

          Comment

          • Mike Driscoll

            #6
            Re: python/ruby question..

            On Jun 19, 11:49 am, a...@pythoncraf t.com (Aahz) wrote:
            In article <mailman.634.12 13870465.1044.p ython-l...@python.org >,
            Matt Nordhoff  <mnordh...@matt nordhoff.comwro te:
            >
            >
            >
            You're supposed to use the subprocess module.
            >
            Really?  Sez who?
            >
            $ python
            Python 2.3.4 (#1, Feb  2 2005, 12:11:53)
            [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
            Type "help", "copyright" , "credits" or "license" for more information.>>> import subprocess
            >
            Traceback (most recent call last):
              File "<stdin>", line 1, in ?
            ImportError: No module named subprocess
            --
            Aahz (a...@pythoncra ft.com)           <*       http://www.pythoncraft.com/
            >
            "as long as we like the same operating system, things are cool." --piranha
            The subprocess module supercedes os.popen*, os.system, commands and a
            few others in Python 2.4+. Thus, those others are deprecated. See the
            docs here:

            Source code: Lib/subprocess.py The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace seve...


            Mike

            Comment

            • Mensanator

              #7
              Re: python/ruby question..

              On Jun 19, 5:14 am, Matt Nordhoff <mnordh...@matt nordhoff.comwro te:
              Mensanator wrote:
              You're supposed to use the subprocess module.
              Yeah, I know, but I couldn't get it to work the last
              time I tried it.
              >
              In this case, something like:
              >
              import subprocess
              factor_program = ['factor!', '-d200']
              >
              ...
              >
              p = subprocess.Pope n(factor_progra m + [n], stdout=subproce ss.PIPE)
              p.wait() # wait for it to finish; not sure how necessary it is
              the_output = p.stdout.readli nes()
              Just like how this doesn't work either:

              Traceback (most recent call last):
              File "C:\Program Files\PyGTK\Pyt hon\user\factor _timing.py", line 26,
              in <module>
              p = subprocess.Pope n(factor_progra m + [the_comp[1]],
              stdout=subproce ss.PIPE)
              File "C:\Program Files\PyGTK\Pyt hon\lib\subproc ess.py", line 586, in
              __init__
              errread, errwrite) = self._get_handl es(stdin, stdout, stderr)
              File "C:\Program Files\PyGTK\Pyt hon\lib\subproc ess.py", line 681, in
              _get_handles
              p2cread = self._make_inhe ritable(p2cread )
              File "C:\Program Files\PyGTK\Pyt hon\lib\subproc ess.py", line 722, in
              _make_inheritab le
              DUPLICATE_SAME_ ACCESS)
              TypeError: an integer is required
              >
              See subprocess's documentation [1], which includes guides on replacing
              os.popen* and other functions with it.
              I have seen it - it's utterly incomprehensibl e.

              What do you suppose you did wrong?

              The complete program (with the deprecated code commented out -
              which works, BTW):


              #import os
              import time
              import subprocess

              #factor_program = 'factor! -d200 '
              factor_program = ['factor!','-d200']

              the_composites =
              [['COMPOSITE_FACT OR','5081842980 034330599302211 433031103327124 931395791904635 267920626220458 934262381123664 798988914517309 8650749']]

              the_primes = []
              the_intractable s = []

              phase = 1
              the_times = []
              while the_composites:
              print "="*40
              print 'Phase',phase
              the_comp = the_composites. pop(0)
              print the_comp
              print
              the_times.appen d(time.time()) # time how long it takes to run
              factor!.exe

              #the_output = os.popen(factor _program+the_co mp[1]).readlines()
              # change to subprocess
              p = subprocess.Pope n(factor_progra m + [the_comp[1]],
              stdout=subproce ss.PIPE)
              p.wait()
              the_output = p.stdout.readli nes()

              the_times.appen d(time.time())
              new_factors = [i.split() for i in the_output]
              for i in new_factors: print i
              print
              if len(new_factors ) == 1:
              if new_factors[0][0] == 'PRIME_FACTOR':
              the_primes.appe nd([new_factors[0][0],long(new_facto rs[0][1])])
              else:
              the_intractable s.append([new_factors[0][0],long(new_facto rs[0]
              [1])])
              new_factors.pop ()
              while new_factors:
              j = new_factors.pop (0)
              if j[0] == 'PRIME_FACTOR':
              the_primes.appe nd([j[0],long(j[1])])
              else:
              the_composites. append(j)
              print the_times[phase] - the_times[phase-1],'seconds'
              phase += 1

              print "="*40
              print
              print 'Factoring complete'
              print

              the_primes.sort ()
              the_intractable s.sort()
              the_primes.exte nd(the_intracta bles)

              for i in the_primes:
              print i[0],i[1]
              print
              print "="*40





              When working, it produces:


              ## =============== =============== ==========
              ## Phase 1
              ## ['COMPOSITE_FACT OR',
              '50818429800343 305993022114330 311033271249313 957919046352679 206262204589342 623811236647989 889145173098650 749']
              ##
              ## ['PRIME_FACTOR', '37']
              ## ['PRIME_FACTOR', '43']
              ## ['PRIME_FACTOR', '167']
              ## ['COMPOSITE_FACT OR', '507787751']
              ## ['PRIME_FACTOR', '69847']
              ## ['PRIME_FACTOR', '30697']
              ## ['PRIME_FACTOR', '89017']
              ## ['PRIME_FACTOR', '3478697']
              ## ['PRIME_FACTOR', '434593']
              ## ['PRIME_FACTOR', '49998841']
              ## ['PRIME_FACTOR', '16161070459714 3']
              ## ['PRIME_FACTOR', '14064370273']
              ## ['COMPOSITE_FACT OR', '96303939470359 8565337297']
              ## ['PRIME_FACTOR', '11927295803']
              ##
              ## 0.860000133514 seconds
              ## =============== =============== ==========
              ## Phase 2
              ## ['COMPOSITE_FACT OR', '507787751']
              ##
              ## ['PRIME_FACTOR', '29819']
              ## ['PRIME_FACTOR', '17029']
              ##
              ## 0.0780000686646 seconds
              ## =============== =============== ==========
              ## Phase 3
              ## ['COMPOSITE_FACT OR', '96303939470359 8565337297']
              ##
              ## ['PRIME_FACTOR', '518069464441']
              ## ['PRIME_FACTOR', '1858900129817']
              ##
              ## 0.0469999313354 seconds
              ## =============== =============== ==========
              ##
              ## Factoring complete
              ##
              ## PRIME_FACTOR 37
              ## PRIME_FACTOR 43
              ## PRIME_FACTOR 167
              ## PRIME_FACTOR 17029
              ## PRIME_FACTOR 29819
              ## PRIME_FACTOR 30697
              ## PRIME_FACTOR 69847
              ## PRIME_FACTOR 89017
              ## PRIME_FACTOR 434593
              ## PRIME_FACTOR 3478697
              ## PRIME_FACTOR 49998841
              ## PRIME_FACTOR 11927295803
              ## PRIME_FACTOR 14064370273
              ## PRIME_FACTOR 518069464441
              ## PRIME_FACTOR 1858900129817
              ## PRIME_FACTOR 161610704597143
              ##
              ## =============== =============== ==========



              Comment

              • Matimus

                #8
                Re: python/ruby question..

                On Jun 18, 8:33 pm, "bruce" <bedoug...@eart hlink.netwrote:
                hi...
                >
                can someone point me to where/how i would go about calling a ruby app from a
                python app, and having the python app being able to get a returned value
                from the ruby script.
                >
                something like
                >
                test.py
                 a = os.exec(testrub y.rb)
                >
                testruby.py
                 foo = 9
                 return foo
                >
                i know this doesn't work... but i've been searching for hours on this with
                no luck.... (and yeah, i'm relatively new to both ruby/python!!)
                >
                thanks
                Both Ruby and Python appear to support XMLRPC. I haven't used XMLRPC
                in Ruby, but in general you create a server and expose some functions.
                On the client end (Python) you would do something like this (assuming
                you are serving on port 8050):

                import xmlrpclib

                rubyserver = xmlrpclib.Serve r("http://localhost:8050" )
                x = rubyserver.foo( 1,2,3)

                where 'foo' is a function served by the ruby server and x is its
                return value.

                some links:

                Fast, searchable Ruby documentation for core and standard libraries. Plus, links to tutorials, guides, books, and related sites.


                Good python server and client examples on this page:



                I can't be of much help for ruby, and that link doesn't seem to help
                much other than to say 1. it exists and 2. its easy.

                Matt

                Comment

                • Matimus

                  #9
                  Re: python/ruby question..

                  On Jun 19, 4:00 pm, Matimus <mccre...@gmail .comwrote:
                  On Jun 18, 8:33 pm, "bruce" <bedoug...@eart hlink.netwrote:
                  >
                  >
                  >
                  hi...
                  >
                  can someone point me to where/how i would go about calling a ruby app from a
                  python app, and having the python app being able to get a returned value
                  from the ruby script.
                  >
                  something like
                  >
                  test.py
                   a = os.exec(testrub y.rb)
                  >
                  testruby.py
                   foo = 9
                   return foo
                  >
                  i know this doesn't work... but i've been searching for hours on this with
                  no luck.... (and yeah, i'm relatively new to both ruby/python!!)
                  >
                  thanks
                  >
                  Both Ruby and Python appear to support XMLRPC. I haven't used XMLRPC
                  in Ruby, but in general you create a server and expose some functions.
                  On the client end (Python) you would do something like this (assuming
                  you are serving on port 8050):
                  >
                  import xmlrpclib
                  >
                  rubyserver = xmlrpclib.Serve r("http://localhost:8050" )
                  x = rubyserver.foo( 1,2,3)
                  >
                  where 'foo' is a function served by the ruby server and x is its
                  return value.
                  >
                  some links:
                  >
                  Fast, searchable Ruby documentation for core and standard libraries. Plus, links to tutorials, guides, books, and related sites.

                  >
                  Good python server and client examples on this page:
                  >

                  >
                  I can't be of much help for ruby, and that link doesn't seem to help
                  much other than to say 1. it exists and 2. its easy.
                  >
                  Matt
                  Here is a more complete example.

                  The ruby server code:

                  require "xmlrpc/server"

                  s = XMLRPC::Server. new(8080)

                  s.add_handler(" add") do |a,b|
                  a + b
                  end

                  s.add_handler(" div") do |a,b|
                  if b == 0
                  raise XMLRPC::FaultEx ception.new(1, "division by zero")
                  else
                  a / b
                  end
                  end

                  s.set_default_h andler do |name, *args|
                  raise XMLRPC::FaultEx ception.new(-99, "Method #{name} missing" +
                  " or wrong number of parameters!")
                  end

                  s.serve

                  I put the above code into a file xmlrpctest.rb and ran it at the
                  command line. Then I opened the python interpreter in a separate
                  window and did this:
                  >>s = xmlrpclib.Serve r("http://localhost:8080" )
                  >>s.div(100,2.0 )
                  50.0
                  >>s.add(10000 0, 2)
                  100002
                  >>>
                  In the long run you may still want to use the subprocess module to
                  launch the ruby xmlrpc server, but once you do that communicating
                  between the two processes should be pretty simple.

                  Matt

                  Comment

                  • Paul McGuire

                    #10
                    Re: python/ruby question..

                    On Jun 18, 10:33 pm, "bruce" <bedoug...@eart hlink.netwrote:
                    hi...
                    >
                    can someone point me to where/how i would go about calling a ruby app from a
                    python app, and having the python app being able to get a returned value
                    from the ruby script.
                    >
                    I'm betting that Ruby is similar to Python in that a Ruby interpreter
                    can be embedded within other applications. Why not implement
                    something like ruby_exec(), similar to Python's exec, which takes a
                    string containing Ruby code, and a dict of variables that can be
                    accessed and updated from the executed Ruby? Then no messing around
                    with subprocess, XMLRPC, firing up processes, etc. - just create the
                    string and call it.

                    (Implementation left as an exercise for the reader.)

                    -- Paul

                    Comment

                    • Aahz

                      #11
                      Re: python/ruby question..

                      In article <86781600-4100-4413-9857-a9f6ec28f68d@f2 4g2000prh.googl egroups.com>,
                      Giampaolo Rodola' <gnewsg@gmail.c omwrote:
                      >On 19 Giu, 18:49, a...@pythoncraf t.com (Aahz) wrote:
                      >In article <mailman.634.12 13870465.1044.p ython-l...@python.org >,
                      >Matt Nordhoff =A0<mnordh...@m attnordhoff.com wrote:
                      >>
                      >>>You're supposed to use the subprocess module.
                      >>
                      >Really? =A0Sez who?
                      >>
                      >$ python
                      >Python 2.3.4 (#1, Feb =A02 2005, 12:11:53)
                      >[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
                      >Type "help", "copyright" , "credits" or "license" for more information.>>> =
                      import subprocess
                      >>
                      >Traceback (most recent call last):
                      >=A0 File "<stdin>", line 1, in ?
                      >ImportError: No module named subprocess
                      >
                      >That's because you're using a too old python version.
                      Who died and made you BDFL to decree that?
                      --
                      Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

                      "as long as we like the same operating system, things are cool." --piranha

                      Comment

                      Working...