Re: Python as a scripting language. Alternative to bash script?
I'm the OP btw. On 1 July 2010 18:10, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: I think that Python could be a alternative to bash and have some advantages, but it's a long way off from being fully implemented. While a somewhat klutzier language in aspects (the , is both an parameter separator, AND the continued line indicator, leading to lines that end in , ,) I'd consider a decent* REXX implementation first... Any program line that is not recognized as starting a REXX statement is automatically passed to the currently defined command processor. And one could ensure this by just quoting the first word on the line G Useful. However, I know Python, never looked at Rexx, so not much use to me. Tks -- Dave Pawson XSLT XSL-FO FAQ. Docbook FAQ. http://www.dpawson.co.uk -- http://mail.python.org/mailman/listinfo/python-list
Python as a scripting language. Alternative to bash script?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Python as a scripting language. Alternative to bash script?
Thanks for the replies (and Benjamin). Not met with the subprocess idea. On 28 June 2010 16:29, D'Arcy J.M. Cain da...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Python as a scripting language. Alternative to bash script?
On 28 June 2010 18:39, Michael Torrie torr...@gmail.com wrote: On 06/28/2010 05:48 AM, Dave Pawson wrote: 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? Sure. I've created a module called runcmd that does 90% of what I want (easy access to stdout, stderr, error code). I've attached it to this e-mail. Feel free to use it; this post puts my code into the public domain. Thanks Michael. 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? Python really isn't a shell scripting language. So there are things that Bash does much better, such as spawning processes and piping them together. I've tried over the years to create a pythonic library that would let me do that, but haven't found a good syntax that I like. I'm using xml quite a bit and tried xmlsh which does the job but has what I think of as irregular syntax? Bash is fine for smaller scripts, but I had to be really careful by the time I got to around 1200 loc. Hence my preference for Python. It turns out, though, that much of what I use piping for in Bash is to run external processes to do things that I could use python modules for. I kept thinking that.. Then I had to use Java/Python for some plain old calculations, so it kept nagging me that a cleaner approach would be a single language. Sure it's a couple more lines in this case, but in other cases, python's abilities make it simpler than bash. A great document on how you can exploit python's abilities (particularly generators) to replace bash pipelines is here: http://www.dabeaz.com/generators/ Thanks for the reference. I'm nearly there with the mix of return codes and output values, using subprocess. p1 = subprocess.Popen(['identify', '-format' , '%[fx:w]' , f ], stdout=subprocess.PIPE) resp1= p1.communicate() #print dir(p1) p2 = subprocess.Popen(['identify', '-format' , '%[fx:h]' , f ], stdout=subprocess.PIPE) resp2= p2.communicate() if (p1.returncode or p2.returncode): print Error ,p1.returncode sys.exit(2) else: width=int(resp1[0]) height=int(resp2[0]) print Height %s, width %s % (height, width) Not happy with the error handling yet, but this was the bit that I thought I'd struggle with. regards -- Dave Pawson XSLT XSL-FO FAQ. Docbook FAQ. http://www.dpawson.co.uk -- http://mail.python.org/mailman/listinfo/python-list