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


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

Re: Embedding Python in a shell script

Started byChris Angelico <rosuav@gmail.com>
First post2011-06-17 11:05 +1000
Last post2011-06-17 08:58 +0000
Articles 3 — 3 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: Embedding Python in a shell script Chris Angelico <rosuav@gmail.com> - 2011-06-17 11:05 +1000
    Re: Embedding Python in a shell script rusi <rustompmody@gmail.com> - 2011-06-16 19:25 -0700
      Re: Embedding Python in a shell script mg <marco.giusti@gmail.com> - 2011-06-17 08:58 +0000

#7787 — Re: Embedding Python in a shell script

FromChris Angelico <rosuav@gmail.com>
Date2011-06-17 11:05 +1000
SubjectRe: Embedding Python in a shell script
Message-ID<mailman.54.1308272749.1164.python-list@python.org>
On Fri, Jun 17, 2011 at 10:57 AM, Jason Friedman <jason@powerpull.net> wrote:
> The code behaves as I expect and want, but the de-denting of the
> Python call is unattractive, especially unattractive the longer the
> Python call becomes.  I'd prefer something like:
>

#!/bin/bash
for i in 1 2 3 4; do
  python -c "if True:
      for j in range($i):
          print j
  "
done

Untested, but it's a hack I've used in a few places. The if tells
Python to expect an indent, and nobody cares if your first indent is
two miles and the one after that is only another two spaces.

ChrisA

[toc] | [next] | [standalone]


#7793

Fromrusi <rustompmody@gmail.com>
Date2011-06-16 19:25 -0700
Message-ID<3206787a-4feb-4098-8ed3-9e0b86ee7b57@o10g2000prn.googlegroups.com>
In reply to#7787
On Jun 17, 6:05 am, Chris Angelico <ros...@gmail.com> wrote:

> > Python call becomes.  I'd prefer something like:
>
> #!/bin/bash
> for i in 1 2 3 4; do
>   python -c "if True:
# comfortably indented python code

Thanks. Nice!

[toc] | [prev] | [next] | [standalone]


#7814

Frommg <marco.giusti@gmail.com>
Date2011-06-17 08:58 +0000
Message-ID<itf4vt$ki1$1@speranza.aioe.org>
In reply to#7793
rusi <rustompmody@gmail.com> wrote:
> On Jun 17, 6:05 am, Chris Angelico <ros...@gmail.com> wrote:
> 
>> > Python call becomes.  I'd prefer something like:
>>
>> #!/bin/bash
>> for i in 1 2 3 4; do
>>   python -c "if True:
> # comfortably indented python code
> 
> Thanks. Nice!

You can use bash here document feature, <<-, that strips heading tab
characters but not spaces.


	#!/bin/bash

	for i in 1 2 3 4; do
		python /dev/stdin <<-EOF
			for i in range($i):
			    print i # two tabs and four spaces
			EOF
	done

Or alternatively you can use a temporary file:


	#!/bin/bash

	TEMPFILE=$(mktemp)
	trap 'rm -f $TEMPFILE' TERM INT

	cat > $TEMPFILE <<EOF
	import sys

	for i in range(int(sys.argv[1])):
	    print i
	EOF

	for i in 1 2 3 4; do
		python $TEMPFILE $i
	done

[toc] | [prev] | [standalone]


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


csiph-web