473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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(testruby.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
Jun 27 '08 #1
10 1478
On Jun 18, 10:33�pm, "bruce" <bedoug...@earthlink.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(testruby.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 =
'5081842980034330599302211433031103327124931395791 90463526792062622045893426238112366479898891451730 98650749'

# call external program and capture stdout
the_output = os.popen(factor_program+n).readlines()

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

## n:
50818429800343305993022114330311033271249313957919 04635267920626220458934262381123664798988914517309 8650749
## PRIME_FACTOR 37
## PRIME_FACTOR 43
## PRIME_FACTOR 167
## COMPOSITE_FACTOR 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_FACTOR 963039394703598565337297
## PRIME_FACTOR 11927295803
Jun 27 '08 #2
Mensanator wrote:
On Jun 18, 10:33�pm, "bruce" <bedoug...@earthlink.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(testruby.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 =
'5081842980034330599302211433031103327124931395791 90463526792062622045893426238112366479898891451730 98650749'

# call external program and capture stdout
the_output = os.popen(factor_program+n).readlines()

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.Popen(factor_program + [n], stdout=subprocess.PIPE)
p.wait() # wait for it to finish; not sure how necessary it is
the_output = p.stdout.readlines()

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>
--
Jun 27 '08 #3
In article <ma*************************************@python.or g>,
Matt Nordhoff <mn*******@mattnordhoff.comwrote:
>
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 (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha
Jun 27 '08 #4
On 19 Giu, 18:49, a...@pythoncraft.com (Aahz) wrote:
In article <mailman.634.1213870465.1044.python-l...@python.org>,
Matt Nordhoff *<mnordh...@mattnordhoff.comwrote:
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...@pythoncraft.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
http://code.google.com/p/pyftpdlib/
Jun 27 '08 #5
On Jun 19, 11:49*am, a...@pythoncraft.com (Aahz) wrote:
In article <mailman.634.1213870465.1044.python-l...@python.org>,
Matt Nordhoff *<mnordh...@mattnordhoff.comwrote:
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...@pythoncraft.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:

http://docs.python.org/lib/module-subprocess.html

Mike
Jun 27 '08 #6
On Jun 19, 5:14*am, Matt Nordhoff <mnordh...@mattnordhoff.comwrote:
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.Popen(factor_program + [n], stdout=subprocess.PIPE)
p.wait() # wait for it to finish; not sure how necessary it is
the_output = p.stdout.readlines()
Just like how this doesn't work either:

Traceback (most recent call last):
File "C:\Program Files\PyGTK\Python\user\factor_timing.py", line 26,
in <module>
p = subprocess.Popen(factor_program + [the_comp[1]],
stdout=subprocess.PIPE)
File "C:\Program Files\PyGTK\Python\lib\subprocess.py", line 586, in
__init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "C:\Program Files\PyGTK\Python\lib\subprocess.py", line 681, in
_get_handles
p2cread = self._make_inheritable(p2cread)
File "C:\Program Files\PyGTK\Python\lib\subprocess.py", line 722, in
_make_inheritable
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 incomprehensible.

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_FACTOR','508184298003433059930221143303 11033271249313957919046352679206262204589342623811 236647989889145173098650749']]

the_primes = []
the_intractables = []

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

#the_output = os.popen(factor_program+the_comp[1]).readlines()
# change to subprocess
p = subprocess.Popen(factor_program + [the_comp[1]],
stdout=subprocess.PIPE)
p.wait()
the_output = p.stdout.readlines()

the_times.append(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.append([new_factors[0][0],long(new_factors[0][1])])
else:
the_intractables.append([new_factors[0][0],long(new_factors[0]
[1])])
new_factors.pop()
while new_factors:
j = new_factors.pop(0)
if j[0] == 'PRIME_FACTOR':
the_primes.append([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_intractables.sort()
the_primes.extend(the_intractables)

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

When working, it produces:
## ========================================
## Phase 1
## ['COMPOSITE_FACTOR',
'5081842980034330599302211433031103327124931395791 90463526792062622045893426238112366479898891451730 98650749']
##
## ['PRIME_FACTOR', '37']
## ['PRIME_FACTOR', '43']
## ['PRIME_FACTOR', '167']
## ['COMPOSITE_FACTOR', '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_FACTOR', '963039394703598565337297']
## ['PRIME_FACTOR', '11927295803']
##
## 0.860000133514 seconds
## ========================================
## Phase 2
## ['COMPOSITE_FACTOR', '507787751']
##
## ['PRIME_FACTOR', '29819']
## ['PRIME_FACTOR', '17029']
##
## 0.0780000686646 seconds
## ========================================
## Phase 3
## ['COMPOSITE_FACTOR', '963039394703598565337297']
##
## ['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
##
## ========================================

Jun 27 '08 #7
On Jun 18, 8:33*pm, "bruce" <bedoug...@earthlink.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(testruby.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.Server("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:

http://www.ruby-doc.org/stdlib/libdo...doc/index.html

Good python server and client examples on this page:

http://docs.python.org/lib/simple-xmlrpc-servers.html

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
Jun 27 '08 #8
On Jun 19, 4:00*pm, Matimus <mccre...@gmail.comwrote:
On Jun 18, 8:33*pm, "bruce" <bedoug...@earthlink.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(testruby.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.Server("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:

http://www.ruby-doc.org/stdlib/libdo...doc/index.html

Good python server and client examples on this page:

http://docs.python.org/lib/simple-xmlrpc-servers.html

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::FaultException.new(1, "division by zero")
else
a / b
end
end

s.set_default_handler do |name, *args|
raise XMLRPC::FaultException.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.Server("http://localhost:8080")
s.div(100,2.0)
50.0
>>s.add(100000, 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
Jun 27 '08 #9
On Jun 18, 10:33*pm, "bruce" <bedoug...@earthlink.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
Jun 27 '08 #10
In article <86**********************************@f24g2000prh. googlegroups.com>,
Giampaolo Rodola' <gn****@gmail.comwrote:
>On 19 Giu, 18:49, a...@pythoncraft.com (Aahz) wrote:
>In article <mailman.634.1213870465.1044.python-l...@python.org>,
Matt Nordhoff =A0<mnordh...@mattnordhoff.comwrote:
>>>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 (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha
Jun 27 '08 #11

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
13
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
0
by: Robert | last post by:
After failing on a yield/iterator-continuation problem in Python (see below) I tried the Ruby (1.8.2) language first time on that construct: The example tries to convert a block callback interface...
13
by: aum | last post by:
Hi, I'm a Python programmer, just starting to get into javascript. On reading some of the js guides, and not liking any of the OO usage patterns I saw, I've cooked up something which python...
22
by: lennart | last post by:
Hi, I'm planning to learn a language for 'client' software. Until now, i 'speak' only some web based languages, like php. As a kid i programmed in Basic (CP/M, good old days :'-) ) Now i want to...
0
by: bruce | last post by:
hey guys... i managed to solve what i was attempting.. my goal was rather simple, to be able to have a python script, call a ruby app, and be able to return a value from the ruby (child) app to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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
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
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,...

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.