Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29511 > unrolled thread

Re: 'indent'ing Python in windows bat

Started byAlbert Hopkins <marduk@letterboxes.org>
First post2012-09-19 13:51 -0400
Last post2012-09-19 20:23 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: 'indent'ing Python in windows bat Albert Hopkins <marduk@letterboxes.org> - 2012-09-19 13:51 -0400
    Re: 'indent'ing Python in windows bat Hans Mulder <hansmu@xs4all.nl> - 2012-09-19 20:23 +0200

#29511 — Re: 'indent'ing Python in windows bat

FromAlbert Hopkins <marduk@letterboxes.org>
Date2012-09-19 13:51 -0400
SubjectRe: 'indent'ing Python in windows bat
Message-ID<mailman.929.1348077113.27098.python-list@python.org>
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.

[toc] | [next] | [standalone]


#29513

FromHans Mulder <hansmu@xs4all.nl>
Date2012-09-19 20:23 +0200
Message-ID<505a0d8c$0$6852$e4fe514c@news2.news.xs4all.nl>
In reply to#29511
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web