Looping [was Re: Python and the need for speed]

Christian Gollwitzer auriocus at gmx.de
Tue Apr 18 03:20:43 EDT 2017


Am 18.04.17 um 08:21 schrieb Chris Angelico:
> On Tue, Apr 18, 2017 at 4:06 PM, Christian Gollwitzer <auriocus at gmx.de> wrote:
>> Am 18.04.17 um 02:18 schrieb Ben Bacarisse:
>>
>>> Thanks (and to Grant).  IO seems to be the canonical example.  Where
>>> some languages would force one to write
>>>
>>>   c = sys.stdin.read(1)
>>>   while c == ' ':
>>>       c = sys.stdin.read(1)
>>
>> repeat
>>         c  = sys.stdin.read(1)
>> until c != ' '
>
> Except that there's processing code after it.
>

Sorry, I misread it then - Ben's code did NOT have it, it looks like a 
"skip the whitespace" loop.

> while True:
>     c = sys.stdin.read(1)
>     if not c: break
>     if c.isprintable(): text += c
>     elif c == "\x08": text = text[:-1]
>     # etc
>
> Can you write _that_ as a do-while?

No. This case OTOH looks like an iteration to me and it would be most 
logical to write

for c in sys.stdin:
      if c.isprintable(): text += c
      elif c == "\x08": text = text[:-1]
      # etc

except that this iterates over lines. Is there an analogous iterator for 
chars? For "lines" terminated by something else than "\n"?
"for c in get_chars(sys.stdin)" and
"for c in get_string(sys.stdin, terminate=':')" would be nicely readable 
IMHO. Or AWK-like processing:

for fields in get_fields(open('/etc/passwd'), RS='\n', FS=':'):
	if fields[2]=='0':
		print 'Super-User found:', fields[0]


	Christian



More information about the Python-list mailing list