[Tutor] different behaviour in Idle shell vs Mac terminal

Adam Gold adamgold at hotmail.com
Mon Jan 9 12:40:38 CET 2012


> Date: Sun, 8 Jan 2012 23:34:15 +0000
> From: Adam Gold 
> To: 
> Subject: [Tutor] different behaviour in Idle shell vs Mac terminal
> Message-ID: 
> Content-Type: text/plain; charset="iso-8859-1"
>
>
> I have short piece of code I'm using to print a string to the terminal one letter at a time.? It works fine when I invoke the script from within Idle; each letter appears after the preceding one according to the designated time interval.? However if I run it in the Mac terminal ('python3 ./script.py'), there's a pause and then the whole string prints in one go.? Here's the relevant code:
>
> import sys
> import time
>
> text = "this text is printing one letter at a time..."
> for char in text:
> ??? sys.stdout.write(char)
> ??? time.sleep(0.03)
>
> I'm thinking this may be a tty issue (is stdout going to the right terminal?) but I'm still finding my way and would therefore appreciate any guidance.? Of course if there's a better way of printing out one letter at a time, I'm also interested to know that.? Thanks.
>
> P.S. if it's relevant, this is part of a simple financial maths program and it's used to display the results after certain inputs have been gathered.
>
>
> ------------------------------
>
> Message: 3
> Date: Mon, 09 Jan 2012 10:56:29 +1100
> From: Steven D'Aprano 
> To: tutor at python.org
> Subject: Re: [Tutor] different behaviour in Idle shell vs Mac terminal
> Message-ID: <4F0A2D2D.9000809 at pearwood.info>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Adam Gold wrote:
> > I have short piece of code I'm using to print a string to the terminal one letter at a time. It works fine when I invoke the script from within Idle; each letter appears after the preceding one according to the designated time interval. However if I run it in the Mac terminal ('python3 ./script.py'), there's a pause and then the whole string prints in one go. Here's the relevant code:
> >
> > import sys
> > import time
> >
> > text = "this text is printing one letter at a time..."
> > for char in text:
> > sys.stdout.write(char)
> > time.sleep(0.03)
> >
> > I'm thinking this may be a tty issue (is stdout going to the right terminal?)
>
> It's a buffering issue.
>
> [...]
> > P.S. if it's relevant, this is part of a simple financial maths program and it's used to display the results after certain inputs have been gathered.
>
> To annoy your users? I'm not sure why you think it's a good idea to pretend
> that the computer has to type the letters one at a time. This isn't some
> stupid imaginary program in a Hollywood movie, I assume it is meant to
> actually be useful and usable, and trust me on this, waiting while the program
> pretends to type gets old *really* fast.
>
> (What are you trying to emulate? A stock ticker or something? Do those things
> still even exist? I haven't seen one since the last time I watched the Addams
> Family TV series. The old one, in black & white.)
>
> But if you must, after writing each character, call sys.stdout.flush() to
> flush the buffer.
>
>
>
> --
> Steven
>
Thanks Steven that worked.  In terms of why I'm using this: I shouldn't overstate what I'm doing when I say financial maths.  One of the elements is a mortgage calculator for my mother who's, shall we say, not a "power user".  After taking the basic inputs, it prints out a few stats (monthly payment etc.).  Without some literal "brake" in how the info gets written on the screen, it all appears in one go and having tested it on said power user, it was too much at once.  Hence I want to slow things down.  I appreciate, I'm not pushing the boundaries here but one step at a time ('excuse the pun!).  Anyway, again, thanks for your help.
 		 	   		  


More information about the Tutor mailing list