Is pyton for me?

Discussion in 'Python' started by Mark de+la+Fuente, Jun 10, 2005.

  1. I need to write simple scripts for executing command line functions. Up till now I've used C-Shell scripts for this, but I'm looking for a better alternative. And I keep reading about how “easy” it is to program with python.

    Unfortunately after reading "diveintopython" and the python tutorial, I'm not sure how to do what I need to do.

    Here is an example of the type of thing I would like to be able to do. Can I do this with python? How do I get python to execute command line functions?

    >From the command prompt (not from the python interpreter – running linux for example) I would type:


    gensky 3 21 12 > sky.rad

    This would call the non-python program “gensky” which creates a computer model of a sky on march 21st at 12:00 PM and puts this information into a file called sky.rad.

    If I create a CSH script like the one below, I can automate tasks by building loops along the lines of:

    #!/bin/csh -fv

    # simple script to create multiple sky files.

    foreach hour (10 12 14)
    gensky 3 21 $hour > sky$hour.rad
    end

    If I try and do a gensky command from the python interpreter or within a python.py file, I get an error message:

    NameError: name ‘gensky’ is not defined

    So even if I get the python syntax correct, I can’t get these commands to execute.

    If anyone has any suggestions on how to get python scripts to execute this sort of thing, what I should be looking at, or if there is something else I might consider besides python, please let me know.

    Thanks for your help.

    Mark

    _______________________________________________
    Join Excite! - http://www.excite.com
    The most personalized portal on the Web!
    Mark de+la+Fuente, Jun 10, 2005
    #1
    1. Advertising

  2. Mark de+la+Fuente

    Dan Bishop Guest

    Mark de+la+Fuente wrote:
    > I need to write simple scripts for executing command line functions. Up till
    > now I've used C-Shell scripts for this, but I'm looking for a better
    > alternative. And I keep reading about how "easy" it is to program with
    > python.
    >
    > Unfortunately after reading "diveintopython" and the python tutorial, I'm not
    > sure how to do what I need to do.
    >
    > Here is an example of the type of thing I would like to be able to do.
    > Can I do this with python? How do I get python to execute command line
    > functions?


    import os
    os.system('gensky 3 21 12 > sky.rad')
    Dan Bishop, Jun 10, 2005
    #2
    1. Advertising

  3. Mark de+la+Fuente

    Andrew Dalke Guest

    Mark de la Fuente wrote:
    > Here is an example of the type of thing I would like to be able to do.
    > Can I do this with python? How do I get python to execute command line
    > functions?

    ...
    > # simple script to create multiple sky files.
    >
    > foreach hour (10 12 14)
    > gensky 3 21 $hour > sky$hour.rad
    > end


    Dan Bishop gave one example using os.system. The important
    thing to know is that in the shell all programs can be used
    as commands while in Python there isn't a direct connection.
    Instead you need to call a function which translates a
    request into something which calls the command-line program.

    There are several ways to do that. In Python before 2.4
    the easiest way is with os.system(), which takes the command-line
    text as a string. For example,

    import os
    os.system("gensky 3 21 10 > sky10.rad")

    You could turn this into a Python function rather easily

    import os

    def gensky(hour):
    os.system("gensky 3 21 %d > sky%d.rad" % (hour, hour))

    for hour in (10, 12, 14):
    gensky(hour)


    Python 2.4 introduces the subprocess module which makes it
    so much easier to avoid nearly all the mistakes that can
    occur in using os.system(). You could replace the 'gensky'
    python function with

    import subprocess
    def gensky(hour):
    subprocess.check_call(["gensky", "3", "21", str(hour)],
    stdout = open("sky%d.rad" % (hour,), "w"))


    The main differences here are:
    - the original code didn't check the return value of os.system().
    It should do this because, for example, the gensky program might
    not be on the path. The check_call does that test for me.

    - I needed to do the redirection myself. (I wonder if the
    subprocess module should allow

    if isinstance(stdout, basestring):
    stdout = open(stdout, "wb")

    Hmmm....)


    > If I try and do a gensky command from the python interpreter or within a
    > python.py file, I get an error message:
    >
    > NameError: name ‘gensky’ is not defined


    That's because Python isn't set up to search the command path
    for an executable. It only knows about variable names defined
    in the given Python module or imported from another Python
    module.

    > If anyone has any suggestions on how to get python scripts to execute
    > this sort of thing, what I should be looking at, or if there is
    > something else I might consider besides python, please let me know.


    You'll have to remember that Python is not a shell programming
    language. Though you might try IPython - it allows some of the
    things you're looking for, though not all.

    You should also read through the tutorial document on Python.org
    and look at some of the Python Cookbook.. Actually, start with
    http://wiki.python.org/moin/BeginnersGuide

    Andrew
    Andrew Dalke, Jun 10, 2005
    #3
  4. On Thu, 9 Jun 2005 22:50:20 -0400 (EDT), "Mark de+la+Fuente"
    <> declaimed the following in comp.lang.python:


    >
    > foreach hour (10 12 14)
    > gensky 3 21 $hour > sky$hour.rad
    > end
    >


    import os

    for hour in [10, 12, 14]:
    os.system("gensky 3 21 %s > sky%s.rad" % (hour, hour))


    Python is a stand-alone language. External programs have to be
    explicitly invoked.


    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Jun 10, 2005
    #4
  5. Mark de+la+Fuente

    Kent Johnson Guest

    Andrew Dalke wrote:
    > import subprocess
    > def gensky(hour):
    > subprocess.check_call(["gensky", "3", "21", str(hour)],
    > stdout = open("sky%d.rad" % (hour,), "w"))
    >
    >
    > The main differences here are:
    > - the original code didn't check the return value of os.system().
    > It should do this because, for example, the gensky program might
    > not be on the path. The check_call does that test for me.


    Where do you find check_call()? It's not in the docs and I get
    >>> import subprocess
    >>> subprocess.check_call

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'module' object has no attribute 'check_call'

    with Python 2.4.1.

    Kent
    Kent Johnson, Jun 10, 2005
    #5
  6. Mark de+la+Fuente

    Andrew Dalke Guest

    Kent Johnson wrote:
    > Where do you find check_call()? It's not in the docs and I get
    > >>> import subprocess
    > >>> subprocess.check_call

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > AttributeError: 'module' object has no attribute 'check_call'
    >
    > with Python 2.4.1.


    Interesting. I got my subprocess.py from CVS. The CVS log

    revision 1.12
    date: 2005/01/01 09:36:34; author: astrand; state: Exp; lines: +39 -1
    New subprocess utility function: check_call. Closes #1071764.

    The bug tracker is
    http://sourceforge.net/tracker/index.php?func=detail&aid=1071764&group_id=5470&atid=305470

    which says it's a 2.5ism. Oops! Sorry about that post
    from the future. I didn't realize it.

    Andrew
    Andrew Dalke, Jun 10, 2005
    #6
  7. Mark de+la+Fuente

    Mike Meyer Guest

    "Mark de+la+Fuente" <> writes:

    > I need to write simple scripts for executing command line functions.
    > Up till now I've used C-Shell scripts for this, but I'm looking for
    > a better alternative. And I keep reading about how “easy” it is to
    > program with python.


    Lots of answers - but everyone concentrated on telling you how to do
    what you were trying to do, and no one answered your question. Which
    is actually typical of the c.l.python, but may not be what you want.

    As others pointed out, Python isn't a shell, or even a shell scripting
    language, so it doesn't handle what you're doing in a "natural"
    way. Because of that, it may not be the language for you. Python has
    features that work well for building large systems, but those tend to
    cause extra work when you want to do things that other languages make
    simple.

    One thing you might consider is changing shells. See <URL:
    http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ > for reasons why
    you might want to drop csh. There's a similar paper discussing the
    problems with later versions of sh (bash, late ksh, etc.), but from
    what I recall the problems mostly come from trying to use the extra
    features that have been added to those shells. So when writing sh
    scripts, I tend to stay with fundamental bourne features.

    Other languages have less overhead in invoking external commands. Rexx
    and Perl come to mind. You might want to give them a look.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Jun 10, 2005
    #7
  8. Mark de+la+Fuente

    Drazen Gemic Guest

    Honestly, I don't think that Python is for you. Python is an object
    programming language, and you are looking for simple automation tool,
    in order to avoid programming. If shell scripting is no good enough, try
    tcl.

    DG
    Drazen Gemic, Jun 11, 2005
    #8
  9. On Fri, 10 Jun 2005 17:50:15 -0500, Mike Meyer <> declaimed
    the following in comp.lang.python:


    > Other languages have less overhead in invoking external commands. Rexx
    > and Perl come to mind. You might want to give them a look.
    >

    Especially Rexx -- since any "statement" that is not recognized
    as a part of the Rexx language will be automatically kicked out to the
    currently addressed command host (typically a shell). Though one may
    need to be wary of Rexx's tendency to uppercase everything <G>


    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Jun 11, 2005
    #9
  10. Mike Meyer <> writes:

    > "Mark de+la+Fuente" <> writes:
    >
    > > I need to write simple scripts for executing command line functions.
    > > Up till now I've used C-Shell scripts for this, but I'm looking for
    > > a better alternative. And I keep reading about how “easy” it is to
    > > program with python.

    >
    > As others pointed out, Python isn't a shell, or even a shell scripting
    > language, so it doesn't handle what you're doing in a "natural"
    > way. Because of that, it may not be the language for you. Python has
    > features that work well for building large systems, but those tend to
    > cause extra work when you want to do things that other languages make
    > simple.


    I agree with this approach. For me, there has to be a certain level of
    complexity before I reach for python as a tool. This usually involves
    one or more of the following:

    1: text processing involving multiple files. For example, running
    statistics on file A, looking up values stored in file B.

    2: cases where the overhead of repeatedly opening processes and pipes
    becomes unacceptable.
    For example:
    for f in file/*; do
    cp $f $f.bak
    sed -e 'something' < $f.bak > $f
    done

    This works well with small numbers of files. But even though sed is
    quicker than python, starting a new sed process with every iteration
    quickly stacks up. Rewriting the entire thing to run as a single
    process can dramatically improve performance. Although this might be a
    case of premature optimization.

    3: cases where figuring out how to do something using one of the POSIX
    shell utilities makes my head hurt.

    Personally, I hate popen and avoid using it when possible. There is
    nothing wrong with sh as a glue language when all you need is something
    like: grep text file | filter | filter > output_file.



    > <mike
    > --
    > Mike Meyer <> http://www.mired.org/home/mwm/
    > Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


    --
    Kirk Job-Sluder
    "The square-jawed homunculi of Tommy Hilfinger ads make every day an
    existential holocaust." --Scary Go Round
    Kirk Job Sluder, Jun 11, 2005
    #10
  11. Mark de+la+Fuente

    Peter Hansen Guest

    Kirk Job Sluder wrote:
    > I agree with this approach. For me, there has to be a certain level of
    > complexity before I reach for python as a tool.


    For me as well. In my case, the key question is "is this bash script
    going to be longer than two lines?". If it would be, the Python
    approach will be done faster and will be more maintainable, since I
    often don't look at those scripts again for months and would tend to
    forget any bash syntax that I've learned in the meantime.

    :)

    -Peter
    Peter Hansen, Jun 11, 2005
    #11
  12. Mark de+la+Fuente

    Mike Meyer Guest

    Peter Hansen <> writes:

    > Kirk Job Sluder wrote:
    >> I agree with this approach. For me, there has to be a certain level of
    >> complexity before I reach for python as a tool.

    >
    > For me as well. In my case, the key question is "is this bash script
    > going to be longer than two lines?". If it would be, the Python
    > approach will be done faster and will be more maintainable, since I
    > often don't look at those scripts again for months and would tend to
    > forget any bash syntax that I've learned in the meantime.


    I tend to switch from sh to python when I start writing a loop. A long
    list of repeated commands - even with conditionals - isn't to bad. But
    when you start forking processes in a loop, it's time to use a more
    powerful tool.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Jun 11, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen VanDahm

    Securing the Pyton Interpreter?

    Stephen VanDahm, Jul 15, 2003, in forum: Python
    Replies:
    6
    Views:
    360
    Vinoo Vasudevan
    Jul 17, 2003
  2. Steffen Kunze
    Replies:
    2
    Views:
    391
    Steffen Kunze
    Jul 28, 2004
  3. Philippe C. Martin

    parsing pyton code

    Philippe C. Martin, Nov 6, 2004, in forum: Python
    Replies:
    1
    Views:
    344
    Jean Brouwers
    Nov 6, 2004
  4. mike
    Replies:
    3
    Views:
    449
    Christopher De Vries
    Feb 1, 2005
  5. Daniel Nogradi
    Replies:
    0
    Views:
    410
    Daniel Nogradi
    May 4, 2006
Loading...

Share This Page