From: Dave Pawson on
I've a fairly long bash script and I'm wondering
how easy it would be to port to Python.

Main queries are:
Ease of calling out to bash to use something like imageMagick or Java?
Ease of grabbing return parameters? E.g. convert can return both
height and width of an image. Can this be returned to the Python program?
Can Python access the exit status of a program?

I'd prefer the advantages of using Python, just wondering if I got
so far with the port then found it wouldn't do something?

Has anyone made this comparison please?

TIA

--
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
http://www.dpawson.co.uk
From: Benjamin Kaplan on
On Mon, Jun 28, 2010 at 4:48 AM, Dave Pawson <dave.pawson(a)gmail.com> wrote:
> I've a fairly long bash script and I'm wondering
> how easy it would be to port to Python.
>
> Main queries are:
> Ease of calling out to bash to use something like imageMagick or Java?

Easiest way is os.system, most flexible way is subprocess.Popen.

> Ease of grabbing return parameters? E.g. convert can return both
> height and width of an image. Can this be returned to the Python program?

How does a program return anything other than an exit code? Subprocess
allows you to read the program's stdout if that's what you're looking
for. In the case of ImageMagick, you can use a Python wrapper to the
library instead of calling the program from the command line, and then
you can get all the return values you want.

> Can Python access the exit status of a program?

proc = subprocess.Popen(args)
retcode = proc.wait()

There's a shortcut of
retcode = subprocess.call(args), but that doesn't give you access to
stdout, just the return code.
>
> I'd prefer the advantages of using Python, just wondering if I got
> so far with the port then found it wouldn't do something?
>

If there's anything it can't do that bash can, you can always just
call the shell command.

> Has anyone made this comparison please?
>

If you already have a working shell script, it's probably not worth
your time. But if you've having trouble getting bash to cooperate,
it's not that difficult to rewrite a shell script in Python.

> TIA
>
> --
> Dave Pawson
> XSLT XSL-FO FAQ.
> Docbook FAQ.
> http://www.dpawson.co.uk
> --
> http://mail.python.org/mailman/listinfo/python-list
>
From: D'Arcy J.M. Cain on
On Mon, 28 Jun 2010 12:48:51 +0100
Dave Pawson <dave.pawson(a)gmail.com> wrote:
> I've a fairly long bash script and I'm wondering
> how easy it would be to port to Python.

That's too big a question without seeing more of what your script
does. I will try to suggest some direction though.

First, if you have a complicated bash script that works, the best
choice may be to just leave it alone. Think about Python for your next
project instead.

> Main queries are:
> Ease of calling out to bash to use something like imageMagick or Java?

You don't need to call bash to call an external program. Check out the
subprocess module. If you do need a shell to simplify calling a
program (environment and wild card expansione.g.) don't call bash.
Just use a basic sh. You won't be using the bash control structures so
keep to whatever is supplied by your OS. If that turns out to be bash
anyway then no harm.

Another option is to write small Python scripts that you can call from
your bash script. You can even create them in your bash script. Here
is a silly example.

uc="import sys
s = sys.argv[1]
print s.upper()
"
....
echo -n "Upper case of $SOMESTRING is "; python -c "$uc" $SOMESTRING

> Ease of grabbing return parameters? E.g. convert can return both
> height and width of an image. Can this be returned to the Python program?

Just to set the terminology straight, a parameter is what you call the
function with. The return value is what it returns. The program
output is what it emits (prints.)

Programs return an integer value. This is also called the exxit
status. On success this is 0 but can be otherwise on failure. You can
use this, for example, with diff to determine if two files differ when
you don't care how they differ.

What you want is the output of the program. For this you need to
capture the output and parse it.

Look at the subprocess module.

--
D'Arcy J.M. Cain <darcy(a)druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
From: Dave Pawson on
Thanks for the replies (and Benjamin).
Not met with the subprocess idea.

On 28 June 2010 16:29, D'Arcy J.M. Cain <darcy(a)druid.net> wrote:

>> Main queries are:
>> Ease of calling out to bash to use something like imageMagick or Java?
>
> You don't need to call bash to call an external program.  Check out the
> subprocess module.

Will do.

 If you do need a shell to simplify calling a
> program (environment and wild card expansione.g.) don't call bash.

I can get what I want from Python. No envars needed.



>> Ease of grabbing return parameters? E.g. convert can return both
>> height and width of an image. Can this be returned to the Python program?
>
> Just to set the terminology straight, a parameter is what you call the
> function with.  The return value is what it returns.  The program
> output is what it emits (prints.)

My bad. I mean return values, though I do want
program out from (for example) identify


>
> Programs return an integer value.  This is also called the exit
> status.

Sheer greed, for identify I may get either a return value or an exit
status (bad input etc) :-)
Looks like subprocess can hack it though.

>
> What you want is the output of the program.  For this you need to
> capture the output and parse it.
>
> Look at the subprocess module.

Will do.
tks D'Arcy (and Benjamin)





--
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
http://www.dpawson.co.uk
From: Thomas Jollans on
On 06/28/2010 06:08 PM, Dave Pawson wrote:
> Thanks for the replies (and Benjamin).
> Not met with the subprocess idea.
>
> On 28 June 2010 16:29, D'Arcy J.M. Cain <darcy(a)druid.net> wrote:
>
>>> Main queries are:
>>> Ease of calling out to bash to use something like imageMagick or Java?
>>
>> You don't need to call bash to call an external program. Check out the
>> subprocess module.
>
> Will do.
>
> If you do need a shell to simplify calling a
>> program (environment and wild card expansione.g.) don't call bash.
>
> I can get what I want from Python. No envars needed.
>
>
>
>>> Ease of grabbing return parameters? E.g. convert can return both
>>> height and width of an image. Can this be returned to the Python program?
>>
>> Just to set the terminology straight, a parameter is what you call the
>> function with. The return value is what it returns. The program
>> output is what it emits (prints.)
>
> My bad. I mean return values, though I do want
> program out from (for example) identify

If you're working with images, have a look at the PIL (Python Imaging
Library).

>
>
>>
>> Programs return an integer value. This is also called the exit
>> status.
>
> Sheer greed, for identify I may get either a return value or an exit
> status (bad input etc) :-)
> Looks like subprocess can hack it though.
>
>>
>> What you want is the output of the program. For this you need to
>> capture the output and parse it.
>>
>> Look at the subprocess module.
>
> Will do.
> tks D'Arcy (and Benjamin)
>
>
>
>
>