Ezio Melotti avatar Ezio Melotti committed 549aa14

#14034: adapt to Python 2 and fix indentation.

Comments (0)

Files changed (1)

Doc/howto/argparse.rst

 
 .. 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
 
    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:
    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::
 
    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 <module>
-      print(args.square**2)
+     File "prog.py", line 5, in <module>
+       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
    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'
 
    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
 
    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:
 
    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.
 
    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.
    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.
    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
    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
    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 <module>
-      if args.verbosity >= 2:
+     File "prog.py", line 11, in <module>
+       if args.verbosity >= 2:
    TypeError: unorderable types: NoneType() >= int()
 
 * First output went well, and fixes the bug we had before.
    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.
 
 .. 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,
    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
 
 
    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
 
    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
 
    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``,
 
 .. 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.