[issue34764] Improve documentation example for using iter() with sentinel value

2018-12-24 Thread Raymond Hettinger


Raymond Hettinger  added the comment:


New changeset 00a48d57dfd52a968b357d68f63c51db3a451566 by Raymond Hettinger 
(Miss Islington (bot)) in branch '3.7':
bpo-34764: improve docs example of iter() with sentinel value (GH-11222) 
(#11301)
https://github.com/python/cpython/commit/00a48d57dfd52a968b357d68f63c51db3a451566


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34764] Improve documentation example for using iter() with sentinel value

2018-12-24 Thread miss-islington


Change by miss-islington :


--
pull_requests: +10536

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34764] Improve documentation example for using iter() with sentinel value

2018-12-24 Thread Raymond Hettinger


Raymond Hettinger  added the comment:


New changeset d378b1f8ed7919f65a89f026bc899204be3773d4 by Raymond Hettinger 
(Chris Rands) in branch 'master':
bpo-34764: improve docs example of iter() with sentinel value (GH-11222)
https://github.com/python/cpython/commit/d378b1f8ed7919f65a89f026bc899204be3773d4


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34764] Improve documentation example for using iter() with sentinel value

2018-12-23 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34764] Improve documentation example for using iter() with sentinel value

2018-12-18 Thread ChrisRands


Change by ChrisRands :


--
keywords: +patch
pull_requests: +10457
stage:  -> patch review

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34764] Improve documentation example for using iter() with sentinel value

2018-09-21 Thread ChrisRands


ChrisRands  added the comment:

Thank you Raymond, I like both your examples, although I think I prefer 1) for 
the simplicity

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34764] Improve documentation example for using iter() with sentinel value

2018-09-21 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

I concur that the readline() example is problematic.  While it succeeds in 
showing how iter() works, the example itself is not the best way to solve that 
particular problem.

Here are two possible substitute examples.

1) Simple example that focuses primarily on the behavior of iter() and required 
little extra knowledge:


>>> from random import randint
>>> def roll_dice():
return randint(1, 6) + randint(1, 6)

>>> # roll until a seven is seen
>>> list(iter(roll_dice, 7))
>>> list(iter(roll_dice, 7))
[10, 6, 5, 6, 8]

2) Practical application reading binary files in fixed-width blocks for 
processing with the structure module.  This also teaches how to partial() to 
produce an arity-zero callable suitable for use with iter().

>>> Read fixed-width blocks from a database binary file
>>> from functools import partial
>>> with open('mydata.db', 'rb') as f:
for block in iter(partial(f.read, 64)):
print(parse_struct(block))

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34764] Improve documentation example for using iter() with sentinel value

2018-09-21 Thread ChrisRands


New submission from ChrisRands :

This arose from this SO question: 
https://stackoverflow.com/questions/52446415/line-in-iterfp-readline-rather-than-line-in-fp
 

The example given in the docs:

with open('mydata.txt') as fp:
for line in iter(fp.readline, ''):
process_line(line)

Is exactly equivalent to the following because an empty string is only returned 
by readline at the EOF:

with open('mydata.txt') as fp:
for line in fp:
process_line(line)

The empty string '' sentinel value could be changed to another value to provide 
a possibly more meaningful example where the 2nd code snippet would not always 
produce the same result?

--
assignee: docs@python
components: Documentation
messages: 325995
nosy: ChrisRands, docs@python
priority: normal
severity: normal
status: open
title: Improve documentation example for using iter() with sentinel value

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com