This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: In argparse docs simplify example about argline
Type: enhancement Stage: resolved
Components: Documentation, Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: berker.peksag Nosy List: berker.peksag, docs@python, py.user, python-dev, wolma
Priority: normal Keywords: patch

Created on 2015-01-31 08:40 by py.user, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
args_ex_argline.diff py.user, 2015-01-31 08:40 python 3.4 review
Messages (6)
msg235089 - (view) Author: py.user (py.user) * Date: 2015-01-31 08:40
The example is:

def convert_arg_line_to_args(self, arg_line):
    for arg in arg_line.split():
        if not arg.strip():
            continue
        yield arg

str.split() with default delimiters never returns empty or whitespace strings in the list.

>>> '  x  x  '.split()
['x', 'x']
>>> '  '.split()
[]
>>>

Therefore, the if condition doesn't ever continue the loop.
It can be written:

def convert_arg_line_to_args(self, arg_line):
    for arg in arg_line.split():
        yield arg

It's the same as:

def convert_arg_line_to_args(self, arg_line):
    return iter(arg_line.split())

I guess, nothing uses next() for the result:

def convert_arg_line_to_args(self, arg_line):
    return arg_line.split()

Applied a patch with the last variant.
msg235090 - (view) Author: py.user (py.user) * Date: 2015-01-31 08:58
Url
https://docs.python.org/3/library/argparse.html#customizing-file-parsing
msg241932 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-04-24 10:29
LGTM
msg242051 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-04-26 09:10
New changeset bd8b99034121 by Berker Peksag in branch '3.4':
Issue #23356: Simplify convert_arg_line_to_args example.
https://hg.python.org/cpython/rev/bd8b99034121

New changeset 2d3ed019bc9f by Berker Peksag in branch 'default':
Issue #23356: Simplify convert_arg_line_to_args example.
https://hg.python.org/cpython/rev/2d3ed019bc9f
msg242052 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-04-26 09:12
New changeset 050e0c0b3d90 by Berker Peksag in branch '2.7':
Issue #23356: Simplify convert_arg_line_to_args example.
https://hg.python.org/cpython/rev/050e0c0b3d90
msg242053 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-04-26 09:14
Thanks py.user.
History
Date User Action Args
2022-04-11 14:58:12adminsetgithub: 67545
2015-04-26 09:14:04berker.peksagsetstatus: open -> closed
resolution: fixed
messages: + msg242053

stage: commit review -> resolved
2015-04-26 09:12:47python-devsetmessages: + msg242052
2015-04-26 09:10:32python-devsetnosy: + python-dev
messages: + msg242051
2015-04-24 16:22:05wolmasetnosy: + wolma
2015-04-24 10:29:04berker.peksagsetassignee: docs@python -> berker.peksag
type: performance -> enhancement
versions: + Python 3.5
nosy: + berker.peksag

messages: + msg241932
stage: commit review
2015-01-31 08:58:10py.usersetmessages: + msg235090
2015-01-31 08:40:39py.usercreate