Re: How to write a simple shell loop in python?
[Sorry for top posting - I had a HD problem and lost the original mails] Hi Saul, Steve, Ben, James, Scott David and James! Thank you all very much for your help! I finally got rid of the extra space and also understood why the space was printed :) After using Steve's 'input = raw_input($ )' solution for a while - it does exactly what I want and is the fastest fix also - I wanted command line editing and switched to the cmd.Cmd module. Special thanks to James Mills for writing his own solution! I would like to use your component - but cmd.Cmd is just perfect for my purpose and I want to run my program without the need to install any new module (I am writing the program for somebody else). I got interested in circuits though - but the homepage http://trac.softcircuit.com.au/circuits/ (currently?) seems not to be available. Thanks again for your answers :) Dietrich PS: There is a little tutorial for cmd.Cmd here: - http://www.doughellmann.com/PyMOTW/cmd/index.html The documentation is here: - http://docs.python.org/library/cmd.html (got it from Ben Finney's post) On Wed, 2009-01-21 at 08:37 -0500, Steve Holden wrote: Dietrich Bollmann wrote: Hi, I am trying to write a simple shell loop in Python. My simple approach works fine - but the first output line after entering something is always indented by one blank. Is there any logic explanation for this? How can I get rid of the blank? Is there a smarter way to write a simple shell loop which would work better? Thanks, Dietrich Here my approach: $ python Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. import sys while (1): ... print $ , ... input = sys.stdin.readline() Just replace the lines above with input = raw_input($ ) and you'll be fine. The , in the print statement causes the interpreter to set a flag to emit a space before the next output unless it has just printed a newline. The newline, of course, is provided by the input, so the next print emits a space since it *hasn't* just emitted a newline. regards Steve ... input = input.strip() ... print input ... print input ... print input ... $ one one one one $ two two two two $ three three three three $ Traceback (most recent call last): File stdin, line 3, in module KeyboardInterrupt -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write a simple shell loop in python?
Dietrich Bollmann wrote: Hi, I am trying to write a simple shell loop in Python. My simple approach works fine - but the first output line after entering something is always indented by one blank. Is there any logic explanation for this? How can I get rid of the blank? Is there a smarter way to write a simple shell loop which would work better? Thanks, Dietrich Here my approach: $ python Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. import sys while (1): ... print $ , ... input = sys.stdin.readline() Just replace the lines above with input = raw_input($ ) and you'll be fine. The , in the print statement causes the interpreter to set a flag to emit a space before the next output unless it has just printed a newline. The newline, of course, is provided by the input, so the next print emits a space since it *hasn't* just emitted a newline. regards Steve ... input = input.strip() ... print input ... print input ... print input ... $ one one one one $ two two two two $ three three three three $ Traceback (most recent call last): File stdin, line 3, in module KeyboardInterrupt -- http://mail.python.org/mailman/listinfo/python-list -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write a simple shell loop in python?
Dietrich Bollmann wrote: I am trying to write a simple shell loop in Python. My simple approach works fine - but the first output line after entering something is always indented by one blank. Is there any logic explanation for this? Yes How can I get rid of the blank? By not asking for it with the comma. while (1): ... print $ , ... input = sys.stdin.readline() ... input = input.strip() ... print input ... print input ... print input ... $ one The one and \n above are from stdin, not stdout. So stdout thinks it is still on the same line as the $. one The space separates the 'one' from the '$ ' that it output to stdout above. one one --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
How to write a simple shell loop in python?
Hi, I am trying to write a simple shell loop in Python. My simple approach works fine - but the first output line after entering something is always indented by one blank. Is there any logic explanation for this? How can I get rid of the blank? Is there a smarter way to write a simple shell loop which would work better? Thanks, Dietrich Here my approach: $ python Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. import sys while (1): ... print $ , ... input = sys.stdin.readline() ... input = input.strip() ... print input ... print input ... print input ... $ one one one one $ two two two two $ three three three three $ Traceback (most recent call last): File stdin, line 3, in module KeyboardInterrupt -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write a simple shell loop in python?
Dietrich Bollmann wrote: Hi, I am trying to write a simple shell loop in Python. My simple approach works fine - but the first output line after entering something is always indented by one blank. Is there any logic explanation for this? How can I get rid of the blank? Is there a smarter way to write a simple shell loop which would work better? Thanks, Dietrich Here my approach: $ python Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. import sys while (1): ... print $ , ... input = sys.stdin.readline() ... input = input.strip() ... print input ... print input ... print input ... $ one one one one $ two two two two $ three three three three $ Traceback (most recent call last): File stdin, line 3, in module KeyboardInterrupt Strange. I don't have an explanation, but experiment shows that if you change print $ , to print $ (that is, leave out the comma) then the leading blank is not printed. This behavior doesn't depend on the print input statement's being in a loop. By the way, you don't need parens around the loop guard in python: while 1: (or as I prefer, while True:) work just fine. Saul -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write a simple shell loop in python?
On Wed, Jan 21, 2009 at 3:12 PM, Saul Spatz ssp...@kcnet.com wrote: Strange. I don't have an explanation, but experiment shows that if you change print $ , to print $ (that is, leave out the comma) then the leading blank is not printed. This behavior doesn't depend on the print input statement's being in a loop. By the way, you don't need parens around the loop guard in python: while 1: (or as I prefer, while True:) work just fine. http://codepad.org/f2XSwsPo This component I wrote - just for the hell of it - (just for this thread) works nicely :) cheers James -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write a simple shell loop in python?
Dietrich Bollmann dir...@web.de writes: I am trying to write a simple shell loop in Python. You should investigate the ‘cmd’ module in the standard library URL:http://docs.python.org/library/cmd.html. My simple approach works fine - but the first output line after entering something is always indented by one blank. You should investigate the meaning of a comma ‘,’ in the ‘print’ statement (hint: it can affect subsequent ‘print’ statements) URL:http://docs.python.org/reference/simple_stmts.html#the-print-statement. -- \ “Anyone who puts a small gloss on [a] fundamental technology, | `\ calls it proprietary, and then tries to keep others from | _o__) building on it, is a thief.” —Tim O'Reilly, 2000-01-25 | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list