Re: 'indent'ing Python in windows bat

Discussion in 'Python' started by Albert Hopkins, Sep 19, 2012.

  1. On Tue, 2012-09-18 at 22:12 -0600, Jason Friedman wrote:
    > > I'm converting windows bat files little by little to Python 3 as I find time
    > > and learn Python.
    > > The most efficient method for some lines is to call Python like:
    > > python -c "import sys; sys.exit(3)"
    > >
    > > How do I "indent" if I have something like:
    > > if (sR=='Cope'): sys.exit(1) elif (sR=='Perform') sys.exit(2) else
    > > sys.exit(3)

    >
    > Some months ago I posted what I think is a similar question in the
    > Unix world: I wanted to call a small portion of Python from within a
    > Bash script.
    >
    > Someone on this list answered (for Bash):
    >
    > #!/bin/bash
    > command1
    > command2
    > python -c "if True:
    > import module
    > if condition:
    > do_this
    > else:
    > do_that
    > "
    > command4
    > # end code


    A better way (in *nix) would be, e.g.:

    #!/bin/sh

    read -p 'Enter a number ' count

    python << EOF
    print 'Odd numbers between 0 and ${count}'
    for i in range(${count}):
    if i % 2:
    print i
    EOF

    Horribly bad example, but you get the idea.
     
    Albert Hopkins, Sep 19, 2012
    #1
    1. Advertising

  2. Albert Hopkins

    Hans Mulder Guest

    On 19/09/12 19:51:44, Albert Hopkins wrote:
    > On Tue, 2012-09-18 at 22:12 -0600, Jason Friedman wrote:
    >>> I'm converting windows bat files little by little to Python 3 as I find time
    >>> and learn Python.
    >>> The most efficient method for some lines is to call Python like:
    >>> python -c "import sys; sys.exit(3)"
    >>>
    >>> How do I "indent" if I have something like:
    >>> if (sR=='Cope'): sys.exit(1) elif (sR=='Perform') sys.exit(2) else
    >>> sys.exit(3)

    >>
    >> Some months ago I posted what I think is a similar question in the
    >> Unix world: I wanted to call a small portion of Python from within a
    >> Bash script.
    >>
    >> Someone on this list answered (for Bash):
    >>
    >> #!/bin/bash
    >> command1
    >> command2
    >> python -c "if True:
    >> import module
    >> if condition:
    >> do_this
    >> else:
    >> do_that
    >> "
    >> command4
    >> # end code

    >
    > A better way (in *nix) would be, e.g.:
    >
    > #!/bin/sh
    >
    > read -p 'Enter a number ' count
    >
    > python << EOF
    > print 'Odd numbers between 0 and ${count}'
    > for i in range(${count}):
    > if i % 2:
    > print i
    > EOF
    >
    > Horribly bad example, but you get the idea.


    If you do it like that, you have to remember to not use certain
    punctuation characters in your Python code, because they are
    meaningful to the shell, even inside a <<EOF construct.

    I'd prefer:

    #!/bin/sh
    read -p 'Enter a number ' count

    python - $count <<'EOF'
    import sys

    count = int(sys.argv[1])

    print "Odd numbers between 0 and %d" % count
    for i in range(count):
    if i % 2:
    print i
    EOF

    When I use <<'EOF', I don't have to worry about the shell
    interpreting some punctuation character in my Python code.

    The downside is, I can't use ${count} to interpolate a shell
    variable in my code; I have to pass it in as an explicit
    argument. Or maybe that's an upside: explicit is better
    than implicit, and all that.


    Hope this helps,

    -- HansM
     
    Hans Mulder, Sep 19, 2012
    #2
    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. David Smith

    'indent'ing Python in windows bat

    David Smith, Sep 18, 2012, in forum: Python
    Replies:
    0
    Views:
    155
    David Smith
    Sep 18, 2012
  2. Dave Angel

    Re: 'indent'ing Python in windows bat

    Dave Angel, Sep 18, 2012, in forum: Python
    Replies:
    0
    Views:
    219
    Dave Angel
    Sep 18, 2012
  3. Ian Kelly
    Replies:
    2
    Views:
    158
    Hans Mulder
    Sep 19, 2012
  4. Dennis Lee Bieber

    Re: 'indent'ing Python in windows bat

    Dennis Lee Bieber, Sep 18, 2012, in forum: Python
    Replies:
    0
    Views:
    225
    Dennis Lee Bieber
    Sep 18, 2012
  5. Terry Reedy

    Re: 'indent'ing Python in windows bat

    Terry Reedy, Sep 18, 2012, in forum: Python
    Replies:
    0
    Views:
    557
    Terry Reedy
    Sep 18, 2012
Loading...

Share This Page