mirror of
https://github.com/python/cpython.git
synced 2025-11-15 00:00:00 +00:00
#14034: fix indentation.
This commit is contained in:
parent
6cc7a41c2f
commit
9ab3fdd8cb
1 changed files with 78 additions and 78 deletions
|
|
@ -84,7 +84,7 @@ Following is a result of running the code:
|
||||||
usage: prog.py [-h]
|
usage: prog.py [-h]
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
$ python3 prog.py --verbose
|
$ python3 prog.py --verbose
|
||||||
usage: prog.py [-h]
|
usage: prog.py [-h]
|
||||||
prog.py: error: unrecognized arguments: --verbose
|
prog.py: error: unrecognized arguments: --verbose
|
||||||
|
|
@ -128,10 +128,10 @@ And running the code:
|
||||||
usage: prog.py [-h] echo
|
usage: prog.py [-h] echo
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
echo
|
echo
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
$ python3 prog.py foo
|
$ python3 prog.py foo
|
||||||
foo
|
foo
|
||||||
|
|
||||||
|
|
@ -170,10 +170,10 @@ And we get:
|
||||||
usage: prog.py [-h] echo
|
usage: prog.py [-h] echo
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
echo echo the string you use here
|
echo echo the string you use here
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
|
|
||||||
Now, how about doing something even more useful::
|
Now, how about doing something even more useful::
|
||||||
|
|
||||||
|
|
@ -181,7 +181,7 @@ Now, how about doing something even more useful::
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("square", help="display a square of a given number")
|
parser.add_argument("square", help="display a square of a given number")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
print(args.square**2))
|
print(args.square**2)
|
||||||
|
|
||||||
Following is a result of running the code:
|
Following is a result of running the code:
|
||||||
|
|
||||||
|
|
@ -189,8 +189,8 @@ Following is a result of running the code:
|
||||||
|
|
||||||
$ python3 prog.py 4
|
$ python3 prog.py 4
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
File "prog.py", line 5, in <module>
|
File "prog.py", line 5, in <module>
|
||||||
print(args.square**2)
|
print(args.square**2)
|
||||||
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
|
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
|
||||||
|
|
||||||
That didn't go so well. That's because :mod:`argparse` treats the options we
|
That didn't go so well. That's because :mod:`argparse` treats the options we
|
||||||
|
|
@ -200,7 +200,7 @@ give it as strings, unless we tell it otherwise. So, let's tell
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("square", help="display a square of a given number",
|
parser.add_argument("square", help="display a square of a given number",
|
||||||
type=int)
|
type=int)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
print(args.square**2)
|
print(args.square**2)
|
||||||
|
|
||||||
|
|
@ -229,7 +229,7 @@ have a look on how to add optional ones::
|
||||||
parser.add_argument("--verbosity", help="increase output verbosity")
|
parser.add_argument("--verbosity", help="increase output verbosity")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.verbosity:
|
if args.verbosity:
|
||||||
print("verbosity turned on")
|
print("verbosity turned on")
|
||||||
|
|
||||||
And the output:
|
And the output:
|
||||||
|
|
||||||
|
|
@ -242,8 +242,8 @@ And the output:
|
||||||
usage: prog.py [-h] [--verbosity VERBOSITY]
|
usage: prog.py [-h] [--verbosity VERBOSITY]
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
--verbosity VERBOSITY
|
--verbosity VERBOSITY
|
||||||
increase output verbosity
|
increase output verbosity
|
||||||
$ python3 prog.py --verbosity
|
$ python3 prog.py --verbosity
|
||||||
usage: prog.py [-h] [--verbosity VERBOSITY]
|
usage: prog.py [-h] [--verbosity VERBOSITY]
|
||||||
|
|
@ -272,10 +272,10 @@ Let's modify the code accordingly::
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--verbose", help="increase output verbosity",
|
parser.add_argument("--verbose", help="increase output verbosity",
|
||||||
action="store_true")
|
action="store_true")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.verbose:
|
if args.verbose:
|
||||||
print("verbosity turned on")
|
print("verbosity turned on")
|
||||||
|
|
||||||
And the output:
|
And the output:
|
||||||
|
|
||||||
|
|
@ -283,15 +283,15 @@ And the output:
|
||||||
|
|
||||||
$ python3 prog.py --verbose
|
$ python3 prog.py --verbose
|
||||||
verbosity turned on
|
verbosity turned on
|
||||||
$ python3 prog.py --verbose 1
|
$ python3 prog.py --verbose 1
|
||||||
usage: prog.py [-h] [--verbose]
|
usage: prog.py [-h] [--verbose]
|
||||||
prog.py: error: unrecognized arguments: 1
|
prog.py: error: unrecognized arguments: 1
|
||||||
$ python3 prog.py --help
|
$ python3 prog.py --help
|
||||||
usage: prog.py [-h] [--verbose]
|
usage: prog.py [-h] [--verbose]
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
--verbose increase output verbosity
|
--verbose increase output verbosity
|
||||||
|
|
||||||
Here is what is happening:
|
Here is what is happening:
|
||||||
|
|
||||||
|
|
@ -318,10 +318,10 @@ versions of the options. It's quite simple::
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-v", "--verbose", help="increase output verbosity",
|
parser.add_argument("-v", "--verbose", help="increase output verbosity",
|
||||||
action="store_true")
|
action="store_true")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.verbose:
|
if args.verbose:
|
||||||
print("verbosity turned on")
|
print("verbosity turned on")
|
||||||
|
|
||||||
And here goes:
|
And here goes:
|
||||||
|
|
||||||
|
|
@ -333,8 +333,8 @@ And here goes:
|
||||||
usage: prog.py [-h] [-v]
|
usage: prog.py [-h] [-v]
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-v, --verbose increase output verbosity
|
-v, --verbose increase output verbosity
|
||||||
|
|
||||||
Note that the new ability is also reflected in the help text.
|
Note that the new ability is also reflected in the help text.
|
||||||
|
|
||||||
|
|
@ -347,15 +347,15 @@ Our program keeps growing in complexity::
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("square", type=int,
|
parser.add_argument("square", type=int,
|
||||||
help="display a square of a given number")
|
help="display a square of a given number")
|
||||||
parser.add_argument("-v", "--verbose", action="store_true",
|
parser.add_argument("-v", "--verbose", action="store_true",
|
||||||
help="increase output verbosity")
|
help="increase output verbosity")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
answer = args.square**2
|
answer = args.square**2
|
||||||
if args.verbose:
|
if args.verbose:
|
||||||
print("the square of {} equals {}".format(args.square, answer))
|
print("the square of {} equals {}".format(args.square, answer))
|
||||||
else:
|
else:
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
And now the output:
|
And now the output:
|
||||||
|
|
||||||
|
|
@ -381,17 +381,17 @@ multiple verbosity values, and actually get to use them::
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("square", type=int,
|
parser.add_argument("square", type=int,
|
||||||
help="display a square of a given number")
|
help="display a square of a given number")
|
||||||
parser.add_argument("-v", "--verbosity", type=int,
|
parser.add_argument("-v", "--verbosity", type=int,
|
||||||
help="increase output verbosity")
|
help="increase output verbosity")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
answer = args.square**2
|
answer = args.square**2
|
||||||
if args.verbosity == 2:
|
if args.verbosity == 2:
|
||||||
print("the square of {} equals {}".format(args.square, answer))
|
print("the square of {} equals {}".format(args.square, answer))
|
||||||
elif args.verbosity == 1:
|
elif args.verbosity == 1:
|
||||||
print("{}^2 == {}".format(args.square, answer))
|
print("{}^2 == {}".format(args.square, answer))
|
||||||
else:
|
else:
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
And the output:
|
And the output:
|
||||||
|
|
||||||
|
|
@ -415,17 +415,17 @@ Let's fix it by restricting the values the ``--verbosity`` option can accept::
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("square", type=int,
|
parser.add_argument("square", type=int,
|
||||||
help="display a square of a given number")
|
help="display a square of a given number")
|
||||||
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],
|
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],
|
||||||
help="increase output verbosity")
|
help="increase output verbosity")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
answer = args.square**2
|
answer = args.square**2
|
||||||
if args.verbosity == 2:
|
if args.verbosity == 2:
|
||||||
print("the square of {} equals {}".format(args.square, answer))
|
print("the square of {} equals {}".format(args.square, answer))
|
||||||
elif args.verbosity == 1:
|
elif args.verbosity == 1:
|
||||||
print("{}^2 == {}".format(args.square, answer))
|
print("{}^2 == {}".format(args.square, answer))
|
||||||
else:
|
else:
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
And the output:
|
And the output:
|
||||||
|
|
||||||
|
|
@ -438,11 +438,11 @@ And the output:
|
||||||
usage: prog.py [-h] [-v {0,1,2}] square
|
usage: prog.py [-h] [-v {0,1,2}] square
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
square display a square of a given number
|
square display a square of a given number
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-v {0,1,2}, --verbosity {0,1,2}
|
-v {0,1,2}, --verbosity {0,1,2}
|
||||||
increase output verbosity
|
increase output verbosity
|
||||||
|
|
||||||
Note that the change also reflects both in the error message as well as the
|
Note that the change also reflects both in the error message as well as the
|
||||||
|
|
@ -455,17 +455,17 @@ verbosity argument (check the output of ``python --help``)::
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("square", type=int,
|
parser.add_argument("square", type=int,
|
||||||
help="display the square of a given number")
|
help="display the square of a given number")
|
||||||
parser.add_argument("-v", "--verbosity", action="count",
|
parser.add_argument("-v", "--verbosity", action="count",
|
||||||
help="increase output verbosity")
|
help="increase output verbosity")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
answer = args.square**2
|
answer = args.square**2
|
||||||
if args.verbosity == 2:
|
if args.verbosity == 2:
|
||||||
print("the square of {} equals {}".format(args.square, answer))
|
print("the square of {} equals {}".format(args.square, answer))
|
||||||
elif args.verbosity == 1:
|
elif args.verbosity == 1:
|
||||||
print("{}^2 == {}".format(args.square, answer))
|
print("{}^2 == {}".format(args.square, answer))
|
||||||
else:
|
else:
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
We have introduced another action, "count",
|
We have introduced another action, "count",
|
||||||
to count the number of occurences of a specific optional arguments:
|
to count the number of occurences of a specific optional arguments:
|
||||||
|
|
@ -487,11 +487,11 @@ to count the number of occurences of a specific optional arguments:
|
||||||
usage: prog.py [-h] [-v] square
|
usage: prog.py [-h] [-v] square
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
square display a square of a given number
|
square display a square of a given number
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-v, --verbosity increase output verbosity
|
-v, --verbosity increase output verbosity
|
||||||
$ python3 prog.py 4 -vvv
|
$ python3 prog.py 4 -vvv
|
||||||
16
|
16
|
||||||
|
|
||||||
|
|
@ -521,19 +521,19 @@ Let's fix::
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("square", type=int,
|
parser.add_argument("square", type=int,
|
||||||
help="display a square of a given number")
|
help="display a square of a given number")
|
||||||
parser.add_argument("-v", "--verbosity", action="count",
|
parser.add_argument("-v", "--verbosity", action="count",
|
||||||
help="increase output verbosity")
|
help="increase output verbosity")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
answer = args.square**2
|
answer = args.square**2
|
||||||
|
|
||||||
# bugfix: replace == with >=
|
# bugfix: replace == with >=
|
||||||
if args.verbosity >= 2:
|
if args.verbosity >= 2:
|
||||||
print("the square of {} equals {}".format(args.square, answer))
|
print("the square of {} equals {}".format(args.square, answer))
|
||||||
elif args.verbosity >= 1:
|
elif args.verbosity >= 1:
|
||||||
print("{}^2 == {}".format(args.square, answer))
|
print("{}^2 == {}".format(args.square, answer))
|
||||||
else:
|
else:
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
And this is what it gives:
|
And this is what it gives:
|
||||||
|
|
||||||
|
|
@ -545,8 +545,8 @@ And this is what it gives:
|
||||||
the square of 4 equals 16
|
the square of 4 equals 16
|
||||||
$ python3 prog.py 4
|
$ python3 prog.py 4
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
File "prog.py", line 11, in <module>
|
File "prog.py", line 11, in <module>
|
||||||
if args.verbosity >= 2:
|
if args.verbosity >= 2:
|
||||||
TypeError: unorderable types: NoneType() >= int()
|
TypeError: unorderable types: NoneType() >= int()
|
||||||
|
|
||||||
* First output went well, and fixes the bug we had before.
|
* First output went well, and fixes the bug we had before.
|
||||||
|
|
@ -559,17 +559,17 @@ Let's fix that bug::
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("square", type=int,
|
parser.add_argument("square", type=int,
|
||||||
help="display a square of a given number")
|
help="display a square of a given number")
|
||||||
parser.add_argument("-v", "--verbosity", action="count", default=0,
|
parser.add_argument("-v", "--verbosity", action="count", default=0,
|
||||||
help="increase output verbosity")
|
help="increase output verbosity")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
answer = args.square**2
|
answer = args.square**2
|
||||||
if args.verbosity >= 2:
|
if args.verbosity >= 2:
|
||||||
print("the square of {} equals {}".format(args.square, answer))
|
print("the square of {} equals {}".format(args.square, answer))
|
||||||
elif args.verbosity >= 1:
|
elif args.verbosity >= 1:
|
||||||
print("{}^2 == {}".format(args.square, answer))
|
print("{}^2 == {}".format(args.square, answer))
|
||||||
else:
|
else:
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
We've just introduced yet another keyword, ``default``.
|
We've just introduced yet another keyword, ``default``.
|
||||||
We've set it to ``0`` in order to make it comparable to the other int values.
|
We've set it to ``0`` in order to make it comparable to the other int values.
|
||||||
|
|
@ -605,11 +605,11 @@ not just squares::
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
answer = args.x**args.y
|
answer = args.x**args.y
|
||||||
if args.verbosity >= 2:
|
if args.verbosity >= 2:
|
||||||
print("{} to the power {} equals {}".format(args.x, args.y, answer))
|
print("{} to the power {} equals {}".format(args.x, args.y, answer))
|
||||||
elif args.verbosity >= 1:
|
elif args.verbosity >= 1:
|
||||||
print("{}^{} == {}".format(args.x, args.y, answer))
|
print("{}^{} == {}".format(args.x, args.y, answer))
|
||||||
else:
|
else:
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
|
|
||||||
|
|
@ -622,12 +622,12 @@ Output:
|
||||||
usage: prog.py [-h] [-v] x y
|
usage: prog.py [-h] [-v] x y
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
x the base
|
x the base
|
||||||
y the exponent
|
y the exponent
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-v, --verbosity
|
-v, --verbosity
|
||||||
$ python3 prog.py 4 2 -v
|
$ python3 prog.py 4 2 -v
|
||||||
4^2 == 16
|
4^2 == 16
|
||||||
|
|
||||||
|
|
@ -644,9 +644,9 @@ to display *more* text instead::
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
answer = args.x**args.y
|
answer = args.x**args.y
|
||||||
if args.verbosity >= 2:
|
if args.verbosity >= 2:
|
||||||
print("Running '{}'".format(__file__))
|
print("Running '{}'".format(__file__))
|
||||||
if args.verbosity >= 1:
|
if args.verbosity >= 1:
|
||||||
print("{}^{} == ".format(args.x, args.y), end="")
|
print("{}^{} == ".format(args.x, args.y), end="")
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
|
|
@ -685,11 +685,11 @@ which will be the opposite of the ``--verbose`` one::
|
||||||
answer = args.x**args.y
|
answer = args.x**args.y
|
||||||
|
|
||||||
if args.quiet:
|
if args.quiet:
|
||||||
print(answer)
|
print(answer)
|
||||||
elif args.verbose:
|
elif args.verbose:
|
||||||
print("{} to the power {} equals {}".format(args.x, args.y, answer))
|
print("{} to the power {} equals {}".format(args.x, args.y, answer))
|
||||||
else:
|
else:
|
||||||
print("{}^{} == {}".format(args.x, args.y, answer))
|
print("{}^{} == {}".format(args.x, args.y, answer))
|
||||||
|
|
||||||
Our program is now simpler, and we've lost some functionality for the sake of
|
Our program is now simpler, and we've lost some functionality for the sake of
|
||||||
demonstration. Anyways, here's the output:
|
demonstration. Anyways, here's the output:
|
||||||
|
|
@ -728,11 +728,11 @@ your program, just in case they don't know::
|
||||||
answer = args.x**args.y
|
answer = args.x**args.y
|
||||||
|
|
||||||
if args.quiet:
|
if args.quiet:
|
||||||
print(answer)
|
print(answer)
|
||||||
elif args.verbose:
|
elif args.verbose:
|
||||||
print("{} to the power {} equals {}".format(args.x, args.y, answer))
|
print("{} to the power {} equals {}".format(args.x, args.y, answer))
|
||||||
else:
|
else:
|
||||||
print("{}^{} == {}".format(args.x, args.y, answer))
|
print("{}^{} == {}".format(args.x, args.y, answer))
|
||||||
|
|
||||||
Note that slight difference in the usage text. Note the ``[-v | -q]``,
|
Note that slight difference in the usage text. Note the ``[-v | -q]``,
|
||||||
which tells us that we can either use ``-v`` or ``-q``,
|
which tells us that we can either use ``-v`` or ``-q``,
|
||||||
|
|
@ -746,13 +746,13 @@ but not both at the same time:
|
||||||
calculate X to the power of Y
|
calculate X to the power of Y
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
x the base
|
x the base
|
||||||
y the exponent
|
y the exponent
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-v, --verbose
|
-v, --verbose
|
||||||
-q, --quiet
|
-q, --quiet
|
||||||
|
|
||||||
|
|
||||||
Conclusion
|
Conclusion
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue