Re: bash/shell to python

2012-05-21 Thread Dan Stromberg
FWIW, I do manual argument parsing, because pylint understands how to
detect typos with manual argument parsing, but not the highly dynamic
modules that parse arguments.

On Wed, May 16, 2012 at 7:16 PM, Rita rmorgan...@gmail.com wrote:

 Hello,

 I currently build a lot of interfaces/wrappers to other applications using
 bash/shell. One short coming for it is it lacks a good method to handle
 arguments so I switched to python a while ago to use 'argparse' module. Its
 a great complement to subprocess module. I was wondering if there is a
 generic framework people follow to build python scripts which are replacing
 shell scripts? Is there a guide or a template to follow?





 --
 --- Get your facts first, then you can distort them as you please.--

 --
 http://mail.python.org/mailman/listinfo/python-list


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bash/shell to python

2012-05-20 Thread Cameron Simpson
On 19May2012 08:27, Michael Torrie torr...@gmail.com wrote:
| On 05/16/2012 08:16 PM, Rita wrote:
|  I currently build a lot of interfaces/wrappers to other applications
|  using bash/shell. One short coming for it is it lacks a good method
|  to handle arguments so I switched to python a while ago to use
|  'argparse' module.
| 
| Actually there is a great way of parsing command line options in
| bash, using the GNU getopt program.  See:
| http://www.manpagez.com/man/1/getopt/
| 
| This command is available on all Linux systems, and most BSD systems.
| There's also freegetopt, a BSD implementation for unix, MSDOS, or Windows.

And if you don't want to do option bundling it's easy enough to write a
simple option parser without getopt at all.

My standard option parsing looks somewhat like this:

  # some things to control via the options
  the_file=
  doit=1
  trace=
  # badness?
  badopts=
  while [ $# -gt 0 ]
  do
case $1 in
  -f)   the_file=$2; shift ;;
  -n)   doit= ;;
  -x)   trace=1 ;;
  --)   shift; break ;;
  -?*)  echo $0: unrecognised option: $1 2
badopts=1
;;
  *)break ;;
esac
shift
  done
  # ... parse non-option arguments ...
  [ $badopts ]  { echo $usage 2; exit 2; }
  # ... rest of program ...

There's nothing hard there.

Cheers,
-- 
Cameron Simpson c...@zip.com.au DoD#743
http://www.cskk.ezoshosting.com/cs/

Any profit should go to Arnie's `get the daemon carved on Mount Rushmore' fund.
- Marty Albini, DOD0550, mar...@sdd.hp.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bash/shell to python

2012-05-19 Thread Michael Torrie
On 05/16/2012 08:16 PM, Rita wrote:
 I currently build a lot of interfaces/wrappers to other applications
 using bash/shell. One short coming for it is it lacks a good method
 to handle arguments so I switched to python a while ago to use
 'argparse' module.

Actually there is a great way of parsing command line options in
bash, using the GNU getopt program.  See:
http://www.manpagez.com/man/1/getopt/

This command is available on all Linux systems, and most BSD systems.
There's also freegetopt, a BSD implementation for unix, MSDOS, or Windows.

 Its a great complement to subprocess module. I was wondering if there
 is a generic framework people follow to build python scripts which
 are replacing shell scripts? Is there a guide or a template to
 follow?

Besides the advice given by the other posters in this thread, here is a
very good document on some unique aspects of python that are well suited
to doing system programming or shell scripting in python:

http://www.dabeaz.com/generators/

This describes how generators can be used to replace pipes with
something that is quite efficient and very pythonic.  See the
presentation pdf first.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bash/shell to python

2012-05-17 Thread Chris Angelico
On Thu, May 17, 2012 at 12:16 PM, Rita rmorgan...@gmail.com wrote:
 Hello,

 I currently build a lot of interfaces/wrappers to other applications using
 bash/shell. One short coming for it is it lacks a good method to handle
 arguments so I switched to python a while ago to use 'argparse' module. Its
 a great complement to subprocess module. I was wondering if there is a
 generic framework people follow to build python scripts which are replacing
 shell scripts? Is there a guide or a template to follow?

It's generally accepted that it's easier to port a shell than a shell
script. That said, though, Python does have some good shell-like
functionality - look at the shutil module (along with subprocess which
you've already seen). To create optimal Python code, though, you're
going to have to look through the shell script, understand what it's
actually doing, and rewrite that logic in Python.

chrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bash/shell to python

2012-05-17 Thread Chris Rebert
On Wed, May 16, 2012 at 7:16 PM, Rita rmorgan...@gmail.com wrote:
 Hello,

 I currently build a lot of interfaces/wrappers to other applications using
 bash/shell. One short coming for it is it lacks a good method to handle
 arguments so I switched to python a while ago to use 'argparse' module. Its
 a great complement to subprocess module. I was wondering if there is a
 generic framework people follow to build python scripts which are replacing
 shell scripts? Is there a guide or a template to follow?

Not really.

The new Plumbum library looks pretty awesome though:
http://plumbum.readthedocs.org/en/latest/index.html

There are also a few 3rd-party wrapper libraries for the `subprocess`
module available to make using it more convenient (e.g. `envoy`).

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


bash/shell to python

2012-05-16 Thread Rita
Hello,

I currently build a lot of interfaces/wrappers to other applications using
bash/shell. One short coming for it is it lacks a good method to handle
arguments so I switched to python a while ago to use 'argparse' module. Its
a great complement to subprocess module. I was wondering if there is a
generic framework people follow to build python scripts which are replacing
shell scripts? Is there a guide or a template to follow?





-- 
--- Get your facts first, then you can distort them as you please.--
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bash shell to Python shell?

2009-09-28 Thread garabik-news-2005-05
Chris Rebert c...@rebertia.com wrote:
 On Sun, Sep 27, 2009 at 8:13 PM, edwithad edwit...@hotmail.com wrote:
 I am sure you have not read a question this basic in some time, but I am
 curious. Using Linux I open a terminal window and type: python.

 Does Bash Shell go away and to become a Python Shell, or is it still a Bash
 Shell with Python running inside? Thanks in advance.
 
 The latter. Press Ctrl+D or enter exit() or quit() to exit Python and
 return to bash.


Or, if you prefer the bash shell to be replaced with the python, just type:
exec python

-- 
 ---
| Radovan GarabĂ­k http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__garabik @ kassiopeia.juls.savba.sk |
 ---
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
-- 
http://mail.python.org/mailman/listinfo/python-list


Bash shell to Python shell?

2009-09-27 Thread edwithad
I am sure you have not read a question this basic in some time, but I 
am curious. Using Linux I open a terminal window and type: python.


Does Bash Shell go away and to become a Python Shell, or is it still a 
Bash Shell with Python running inside? Thanks in advance.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Bash shell to Python shell?

2009-09-27 Thread Chris Rebert
On Sun, Sep 27, 2009 at 8:13 PM, edwithad edwit...@hotmail.com wrote:
 I am sure you have not read a question this basic in some time, but I am
 curious. Using Linux I open a terminal window and type: python.

 Does Bash Shell go away and to become a Python Shell, or is it still a Bash
 Shell with Python running inside? Thanks in advance.

The latter. Press Ctrl+D or enter exit() or quit() to exit Python and
return to bash.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bash shell to Python shell?

2009-09-27 Thread Ned Deily
In article 2009092721134550073-edwit...@hotmailcom,
 edwithad edwit...@hotmail.com wrote:
 I am sure you have not read a question this basic in some time, but I 
 am curious. Using Linux I open a terminal window and type: python.
 
 Does Bash Shell go away and to become a Python Shell, or is it still a 
 Bash Shell with Python running inside? Thanks in advance.

See for youself:

$ ps
  PID TTY  TIME CMD
17478 pts/000:00:00 bash
17571 pts/000:00:00 ps

$ python
Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45) 
[GCC 4.3.3] on linux2
Type help, copyright, credits or license for more information.
 import os
 os.system('ps')
  PID TTY  TIME CMD
17478 pts/000:00:00 bash
17572 pts/000:00:00 python
17573 pts/000:00:00 sh
17574 pts/000:00:00 ps
0


So you can see that the top-level shell (process id 17478) starts new 
child processes to run other programs (17571 for the ps in the first 
example, 17572 for python in the second), just as the python os.system 
function starts a new shell (17573, a default /bin/sh) to process the 
'ps' argument string passed to it and that shell starts another process 
(17574) to run 'ps'.

For a more detailed explanation of what's going on, see, for example:

http://tldp.org/HOWTO/Unix-and-Internet-Fundamentals-HOWTO/running-progra
ms.html

-- 
 Ned Deily,
 n...@acm.org

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bash shell to Python shell?

2009-09-27 Thread Grant Edwards
On 2009-09-28, Chris Rebert c...@rebertia.com wrote:
 On Sun, Sep 27, 2009 at 8:13 PM, edwithad edwit...@hotmail.com wrote:
 I am sure you have not read a question this basic in some time, but I am
 curious. Using Linux I open a terminal window and type: python.

 Does Bash Shell go away and to become a Python Shell, or is it still a Bash
 Shell with Python running inside? Thanks in advance.

 The latter. Press Ctrl+D or enter exit() or quit() to exit Python and
 return to bash.

Bash doesn't really go away.  It's still there, it just hands
over the terminal to the python interpreter and then sits and
waits for the python interpreter to exit.  At that point the
Bash shell (the same one as before) resumes.

-- 
Grant


-- 
http://mail.python.org/mailman/listinfo/python-list