From: Ezio Melotti Date: Sun, 6 May 2012 14:03:19 +0000 (+0300) Subject: #14034: adapt to Python 2 and fix indentation. X-Git-Tag: v2.7.4rc1~847 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1c96f946cfb0b1e99c88031e5bda44bb1cb574aa;p=python #14034: adapt to Python 2 and fix indentation. --- diff --git a/Doc/howto/argparse.rst b/Doc/howto/argparse.rst index 9cbf90abf2..5dbd82eb1c 100644 --- a/Doc/howto/argparse.rst +++ b/Doc/howto/argparse.rst @@ -79,16 +79,16 @@ Following is a result of running the code: .. code-block:: sh - $ python3 prog.py - $ python3 prog.py --help + $ python prog.py + $ python prog.py --help usage: prog.py [-h] optional arguments: - -h, --help show this help message and exit - $ python3 prog.py --verbose + -h, --help show this help message and exit + $ python prog.py --verbose usage: prog.py [-h] prog.py: error: unrecognized arguments: --verbose - $ python3 prog.py foo + $ python prog.py foo usage: prog.py [-h] prog.py: error: unrecognized arguments: foo @@ -115,24 +115,24 @@ An example:: parser = argparse.ArgumentParser() parser.add_argument("echo") args = parser.parse_args() - print(args.echo) + print args.echo And running the code: .. code-block:: sh - $ python3 prog.py + $ python prog.py usage: prog.py [-h] echo prog.py: error: the following arguments are required: echo - $ python3 prog.py --help + $ python prog.py --help usage: prog.py [-h] echo positional arguments: - echo + echo optional arguments: - -h, --help show this help message and exit - $ python3 prog.py foo + -h, --help show this help message and exit + $ python prog.py foo foo Here is what's happening: @@ -160,20 +160,20 @@ by reading the source code. So, let's make it a bit more useful:: parser = argparse.ArgumentParser() parser.add_argument("echo", help="echo the string you use here") args = parser.parse_args() - print(args.echo) + print args.echo And we get: .. code-block:: sh - $ python3 prog.py -h + $ python prog.py -h usage: prog.py [-h] echo positional arguments: - echo echo the string you use here + echo echo the string you use here 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:: @@ -181,16 +181,16 @@ Now, how about doing something even more useful:: parser = argparse.ArgumentParser() parser.add_argument("square", help="display a square of a given number") args = parser.parse_args() - print(args.square**2)) + print args.square**2 Following is a result of running the code: .. code-block:: sh - $ python3 prog.py 4 + $ python prog.py 4 Traceback (most recent call last): - File "prog.py", line 5, in - print(args.square**2) + File "prog.py", line 5, in + print args.square**2 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 @@ -200,17 +200,17 @@ give it as strings, unless we tell it otherwise. So, let's tell import argparse parser = argparse.ArgumentParser() parser.add_argument("square", help="display a square of a given number", - type=int) + type=int) args = parser.parse_args() - print(args.square**2) + print args.square**2 Following is a result of running the code: .. code-block:: sh - $ python3 prog.py 4 + $ python prog.py 4 16 - $ python3 prog.py four + $ python prog.py four usage: prog.py [-h] square prog.py: error: argument square: invalid int value: 'four' @@ -229,23 +229,23 @@ have a look on how to add optional ones:: parser.add_argument("--verbosity", help="increase output verbosity") args = parser.parse_args() if args.verbosity: - print("verbosity turned on") + print "verbosity turned on" And the output: .. code-block:: sh - $ python3 prog.py --verbosity 1 + $ python prog.py --verbosity 1 verbosity turned on - $ python3 prog.py - $ python3 prog.py --help + $ python prog.py + $ python prog.py --help usage: prog.py [-h] [--verbosity VERBOSITY] optional arguments: - -h, --help show this help message and exit - --verbosity VERBOSITY + -h, --help show this help message and exit + --verbosity VERBOSITY increase output verbosity - $ python3 prog.py --verbosity + $ python prog.py --verbosity usage: prog.py [-h] [--verbosity VERBOSITY] prog.py: error: argument --verbosity: expected one argument @@ -272,26 +272,26 @@ Let's modify the code accordingly:: import argparse parser = argparse.ArgumentParser() parser.add_argument("--verbose", help="increase output verbosity", - action="store_true") + action="store_true") args = parser.parse_args() if args.verbose: - print("verbosity turned on") + print "verbosity turned on" And the output: .. code-block:: sh - $ python3 prog.py --verbose + $ python prog.py --verbose verbosity turned on - $ python3 prog.py --verbose 1 + $ python prog.py --verbose 1 usage: prog.py [-h] [--verbose] prog.py: error: unrecognized arguments: 1 - $ python3 prog.py --help + $ python prog.py --help usage: prog.py [-h] [--verbose] optional arguments: - -h, --help show this help message and exit - --verbose increase output verbosity + -h, --help show this help message and exit + --verbose increase output verbosity Here is what is happening: @@ -318,23 +318,23 @@ versions of the options. It's quite simple:: import argparse parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", help="increase output verbosity", - action="store_true") + action="store_true") args = parser.parse_args() if args.verbose: - print("verbosity turned on") + print "verbosity turned on" And here goes: .. code-block:: sh - $ python3 prog.py -v + $ python prog.py -v verbosity turned on - $ python3 prog.py --help + $ python prog.py --help usage: prog.py [-h] [-v] optional arguments: - -h, --help show this help message and exit - -v, --verbose increase output verbosity + -h, --help show this help message and exit + -v, --verbose increase output verbosity Note that the new ability is also reflected in the help text. @@ -347,28 +347,28 @@ Our program keeps growing in complexity:: import argparse parser = argparse.ArgumentParser() 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", - help="increase output verbosity") + help="increase output verbosity") args = parser.parse_args() answer = args.square**2 if args.verbose: - print("the square of {} equals {}".format(args.square, answer)) + print "the square of {} equals {}".format(args.square, answer) else: - print(answer) + print answer And now the output: .. code-block:: sh - $ python3 prog.py + $ python prog.py usage: prog.py [-h] [-v] square prog.py: error: the following arguments are required: square - $ python3 prog.py 4 + $ python prog.py 4 16 - $ python3 prog.py 4 --verbose + $ python prog.py 4 --verbose the square of 4 equals 16 - $ python3 prog.py --verbose 4 + $ python prog.py --verbose 4 the square of 4 equals 16 * We've brought back a positional argument, hence the complaint. @@ -381,32 +381,32 @@ multiple verbosity values, and actually get to use them:: import argparse parser = argparse.ArgumentParser() 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, - help="increase output verbosity") + help="increase output verbosity") args = parser.parse_args() answer = args.square**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: - print("{}^2 == {}".format(args.square, answer)) + print "{}^2 == {}".format(args.square, answer) else: - print(answer) + print answer And the output: .. code-block:: sh - $ python3 prog.py 4 + $ python prog.py 4 16 - $ python3 prog.py 4 -v + $ python prog.py 4 -v usage: prog.py [-h] [-v VERBOSITY] square prog.py: error: argument -v/--verbosity: expected one argument - $ python3 prog.py 4 -v 1 + $ python prog.py 4 -v 1 4^2 == 16 - $ python3 prog.py 4 -v 2 + $ python prog.py 4 -v 2 the square of 4 equals 16 - $ python3 prog.py 4 -v 3 + $ python prog.py 4 -v 3 16 These all look good except the last one, which exposes a bug in our program. @@ -415,34 +415,34 @@ Let's fix it by restricting the values the ``--verbosity`` option can accept:: import argparse parser = argparse.ArgumentParser() 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], - help="increase output verbosity") + help="increase output verbosity") args = parser.parse_args() answer = args.square**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: - print("{}^2 == {}".format(args.square, answer)) + print "{}^2 == {}".format(args.square, answer) else: - print(answer) + print answer And the output: .. code-block:: sh - $ python3 prog.py 4 -v 3 + $ python prog.py 4 -v 3 usage: prog.py [-h] [-v {0,1,2}] square prog.py: error: argument -v/--verbosity: invalid choice: 3 (choose from 0, 1, 2) - $ python3 prog.py 4 -h + $ python prog.py 4 -h usage: prog.py [-h] [-v {0,1,2}] square positional arguments: - square display a square of a given number + square display a square of a given number optional arguments: - -h, --help show this help message and exit - -v {0,1,2}, --verbosity {0,1,2} + -h, --help show this help message and exit + -v {0,1,2}, --verbosity {0,1,2} increase output verbosity Note that the change also reflects both in the error message as well as the @@ -455,44 +455,44 @@ verbosity argument (check the output of ``python --help``):: import argparse parser = argparse.ArgumentParser() 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", - help="increase output verbosity") + help="increase output verbosity") args = parser.parse_args() answer = args.square**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: - print("{}^2 == {}".format(args.square, answer)) + print "{}^2 == {}".format(args.square, answer) else: - print(answer) + print answer We have introduced another action, "count", to count the number of occurences of a specific optional arguments: .. code-block:: sh - $ python3 prog.py 4 + $ python prog.py 4 16 - $ python3 prog.py 4 -v + $ python prog.py 4 -v 4^2 == 16 - $ python3 prog.py 4 -vv + $ python prog.py 4 -vv the square of 4 equals 16 - $ python3 prog.py 4 --verbosity --verbosity + $ python prog.py 4 --verbosity --verbosity the square of 4 equals 16 - $ python3 prog.py 4 -v 1 + $ python prog.py 4 -v 1 usage: prog.py [-h] [-v] square prog.py: error: unrecognized arguments: 1 - $ python3 prog.py 4 -h + $ python prog.py 4 -h usage: prog.py [-h] [-v] square positional arguments: - square display a square of a given number + square display a square of a given number optional arguments: - -h, --help show this help message and exit - -v, --verbosity increase output verbosity - $ python3 prog.py 4 -vvv + -h, --help show this help message and exit + -v, --verbosity increase output verbosity + $ python prog.py 4 -vvv 16 * Yes, it's now more of a flag (similar to ``action="store_true"``) in the @@ -521,32 +521,32 @@ Let's fix:: import argparse parser = argparse.ArgumentParser() 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", - help="increase output verbosity") + help="increase output verbosity") args = parser.parse_args() answer = args.square**2 # bugfix: replace == with >= 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: - print("{}^2 == {}".format(args.square, answer)) + print "{}^2 == {}".format(args.square, answer) else: - print(answer) + print answer And this is what it gives: .. code-block:: sh - $ python3 prog.py 4 -vvv + $ python prog.py 4 -vvv the square of 4 equals 16 - $ python3 prog.py 4 -vvvv + $ python prog.py 4 -vvvv the square of 4 equals 16 - $ python3 prog.py 4 + $ python prog.py 4 Traceback (most recent call last): - File "prog.py", line 11, in - if args.verbosity >= 2: + File "prog.py", line 11, in + if args.verbosity >= 2: TypeError: unorderable types: NoneType() >= int() * First output went well, and fixes the bug we had before. @@ -559,17 +559,17 @@ Let's fix that bug:: import argparse parser = argparse.ArgumentParser() 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, - help="increase output verbosity") + help="increase output verbosity") args = parser.parse_args() answer = args.square**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: - print("{}^2 == {}".format(args.square, answer)) + print "{}^2 == {}".format(args.square, answer) else: - print(answer) + print answer 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. @@ -582,7 +582,7 @@ And: .. code-block:: sh - $ python3 prog.py 4 + $ python prog.py 4 16 You can go quite far just with what we've learned so far, @@ -605,30 +605,30 @@ not just squares:: args = parser.parse_args() answer = args.x**args.y 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: - print("{}^{} == {}".format(args.x, args.y, answer)) + print "{}^{} == {}".format(args.x, args.y, answer) else: - print(answer) + print answer Output: .. code-block:: sh - $ python3 prog.py + $ python prog.py usage: prog.py [-h] [-v] x y prog.py: error: the following arguments are required: x, y - $ python3 prog.py -h + $ python prog.py -h usage: prog.py [-h] [-v] x y positional arguments: - x the base - y the exponent + x the base + y the exponent optional arguments: - -h, --help show this help message and exit - -v, --verbosity - $ python3 prog.py 4 2 -v + -h, --help show this help message and exit + -v, --verbosity + $ python prog.py 4 2 -v 4^2 == 16 @@ -644,20 +644,20 @@ to display *more* text instead:: args = parser.parse_args() answer = args.x**args.y if args.verbosity >= 2: - print("Running '{}'".format(__file__)) + print "Running '{}'".format(__file__) if args.verbosity >= 1: - print("{}^{} == ".format(args.x, args.y), end="") - print(answer) + print "{}^{} == ".format(args.x, args.y), end="" + print answer Output: .. code-block:: sh - $ python3 prog.py 4 2 + $ python prog.py 4 2 16 - $ python3 prog.py 4 2 -v + $ python prog.py 4 2 -v 4^2 == 16 - $ python3 prog.py 4 2 -vv + $ python prog.py 4 2 -vv Running 'prog.py' 4^2 == 16 @@ -685,27 +685,27 @@ which will be the opposite of the ``--verbose`` one:: answer = args.x**args.y if args.quiet: - print(answer) + print answer 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: - 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 demonstration. Anyways, here's the output: .. code-block:: sh - $ python3 prog.py 4 2 + $ python prog.py 4 2 4^2 == 16 - $ python3 prog.py 4 2 -q + $ python prog.py 4 2 -q 16 - $ python3 prog.py 4 2 -v + $ python prog.py 4 2 -v 4 to the power 2 equals 16 - $ python3 prog.py 4 2 -vq + $ python prog.py 4 2 -vq usage: prog.py [-h] [-v | -q] x y prog.py: error: argument -q/--quiet: not allowed with argument -v/--verbose - $ python3 prog.py 4 2 -v --quiet + $ python prog.py 4 2 -v --quiet usage: prog.py [-h] [-v | -q] x y prog.py: error: argument -q/--quiet: not allowed with argument -v/--verbose @@ -728,11 +728,11 @@ your program, just in case they don't know:: answer = args.x**args.y if args.quiet: - print(answer) + print answer 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: - 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]``, which tells us that we can either use ``-v`` or ``-q``, @@ -740,19 +740,19 @@ but not both at the same time: .. code-block:: sh - $ python3 prog.py --help + $ python prog.py --help usage: prog.py [-h] [-v | -q] x y calculate X to the power of Y positional arguments: - x the base - y the exponent + x the base + y the exponent optional arguments: - -h, --help show this help message and exit - -v, --verbose - -q, --quiet + -h, --help show this help message and exit + -v, --verbose + -q, --quiet Conclusion