From c689e74fed45bf1a7b2908defc343d35f31dff71 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Thu, 6 Dec 2018 01:24:19 -0500 Subject: Fix flake8 issues This commit contains a very large number of trivial changes in order to fix flake8 errors and warnings. Predominantly these are whitespace changes. Additionally, the build for Python 3.7 on TravisCI has been tweaked to fail if there are any flake8 errors using the following commandline: * flake8 . --count --ignore=E252 --max-complexity=31 --max-line-length=127 --show-source --statistics NOTE: In the future the max cyclomatic complexity should be lowered, but some improvements need to be made first. One flake8 error is being ignored entirely: * E252 missing whitespace around parameter equals * ignored because it doesn't correctly deal with default argument values after a type hint A few flake8 errors are being selectively ignored in certain files: * C901 fuction is too complex * ignored in argparse_completer.py because the complex code is an override of argparse complexity * E302 expected 2 blank lines after ... * ignored in all unit test files for convenience * F401 module imported but unused * ignored in cmd2/__init__.py because imports are for convenience of cmd2 developers and backwards compatibility * F821 undefined name * ignored in cmd2 script files which are intended to run only within cmd2 applications via pyscript where "app" and "cmd" are defined --- examples/alias_startup.py | 1 + examples/arg_print.py | 2 ++ examples/bash_completion.py | 2 +- examples/cmd_as_argument.py | 2 +- examples/decorator_example.py | 1 - examples/help_categories.py | 17 ++++++++------- examples/paged_output.py | 2 +- examples/pirate.py | 3 +-- examples/scripts/arg_printer.py | 2 +- examples/scripts/conditional.py | 2 +- examples/scripts/script.py | 1 - examples/tab_autocomp_dynamic.py | 47 +++++++++++++++++----------------------- examples/tab_autocompletion.py | 42 +++++++++++++++++------------------ examples/table_display.py | 8 +++---- 14 files changed, 63 insertions(+), 69 deletions(-) (limited to 'examples') diff --git a/examples/alias_startup.py b/examples/alias_startup.py index 8a289e79..7c70bcd9 100755 --- a/examples/alias_startup.py +++ b/examples/alias_startup.py @@ -7,6 +7,7 @@ import os import cmd2 + class AliasAndStartup(cmd2.Cmd): """ Example cmd2 application where we create commands that just print the arguments they are called with.""" diff --git a/examples/arg_print.py b/examples/arg_print.py index f2168126..1a103858 100755 --- a/examples/arg_print.py +++ b/examples/arg_print.py @@ -13,6 +13,7 @@ import argparse import cmd2 + class ArgumentAndOptionPrinter(cmd2.Cmd): """ Example cmd2 application where we create commands that just print the arguments they are called with.""" @@ -52,6 +53,7 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): pprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') pprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') pprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') + @cmd2.with_argparser_and_unknown_args(pprint_parser) def do_pprint(self, args, unknown): """Print the options and argument list this options command was called with.""" diff --git a/examples/bash_completion.py b/examples/bash_completion.py index 6a5a2a89..b70761e2 100755 --- a/examples/bash_completion.py +++ b/examples/bash_completion.py @@ -60,7 +60,7 @@ except ImportError: pass # Intentionally below the bash completion code to reduce tab completion lag -import cmd2 +import cmd2 # noqa: E402 class DummyApp(cmd2.Cmd): diff --git a/examples/cmd_as_argument.py b/examples/cmd_as_argument.py index eb46eedf..070a34a0 100755 --- a/examples/cmd_as_argument.py +++ b/examples/cmd_as_argument.py @@ -111,4 +111,4 @@ def main(argv=None): if __name__ == '__main__': - sys.exit(main()) \ No newline at end of file + sys.exit(main()) diff --git a/examples/decorator_example.py b/examples/decorator_example.py index 5b8b303b..bd9228db 100755 --- a/examples/decorator_example.py +++ b/examples/decorator_example.py @@ -71,7 +71,6 @@ class CmdLineApp(cmd2.Cmd): """create a html tag""" self.poutput('<{0}>{1}'.format(args.tag, ' '.join(args.content))) - @cmd2.with_argument_list def do_tagg(self, arglist): """verion of creating an html tag using arglist instead of argparser""" diff --git a/examples/help_categories.py b/examples/help_categories.py index b4a2c977..50b2c17d 100755 --- a/examples/help_categories.py +++ b/examples/help_categories.py @@ -8,6 +8,7 @@ import argparse import cmd2 + class HelpCategories(cmd2.Cmd): """ Example cmd2 application. """ @@ -81,14 +82,14 @@ class HelpCategories(cmd2.Cmd): # Tag the above command functions under the category Application Management cmd2.categorize((do_list, - do_deploy, - do_start, - do_sessions, - do_redeploy, - do_expire, - do_undeploy, - do_stop, - do_findleakers), CMD_CAT_APP_MGMT) + do_deploy, + do_start, + do_sessions, + do_redeploy, + do_expire, + do_undeploy, + do_stop, + do_findleakers), CMD_CAT_APP_MGMT) def do_resources(self, _): """Resources command""" diff --git a/examples/paged_output.py b/examples/paged_output.py index d1b1b2c2..a0674a62 100755 --- a/examples/paged_output.py +++ b/examples/paged_output.py @@ -21,7 +21,7 @@ class PagedOutput(cmd2.Cmd): with open(filename, 'r') as f: text = f.read() self.ppaged(text, chop=chop) - except FileNotFoundError as ex: + except FileNotFoundError: self.perror('ERROR: file {!r} not found'.format(filename), traceback_war=False) @cmd2.with_argument_list diff --git a/examples/pirate.py b/examples/pirate.py index 096133dc..2da3fcaa 100755 --- a/examples/pirate.py +++ b/examples/pirate.py @@ -51,8 +51,7 @@ class Pirate(cmd2.Cmd): def postcmd(self, stop, line): """Runs right before a command is about to return.""" if self.gold != self.initial_gold: - self.poutput('Now we gots {0} doubloons' - .format(self.gold)) + self.poutput('Now we gots {0} doubloons'.format(self.gold)) if self.gold < 0: self.poutput("Off to debtorrr's prison.") stop = True diff --git a/examples/scripts/arg_printer.py b/examples/scripts/arg_printer.py index 848dcf99..19f6dd3f 100755 --- a/examples/scripts/arg_printer.py +++ b/examples/scripts/arg_printer.py @@ -5,4 +5,4 @@ import sys print("Running Python script {!r} which was called with {} arguments".format(os.path.basename(sys.argv[0]), len(sys.argv) - 1)) for i, arg in enumerate(sys.argv[1:]): - print("arg {}: {!r}".format(i+1, arg)) + print("arg {}: {!r}".format(i + 1, arg)) diff --git a/examples/scripts/conditional.py b/examples/scripts/conditional.py index d7ee5ea2..faac3947 100644 --- a/examples/scripts/conditional.py +++ b/examples/scripts/conditional.py @@ -1,4 +1,5 @@ # coding=utf-8 +# flake8: noqa F821 """ This is a Python script intended to be used with the "python_scripting.py" cmd2 example application. @@ -12,7 +13,6 @@ application instance. import os import sys - if len(sys.argv) > 1: directory = sys.argv[1] print('Using specified directory: {!r}'.format(directory)) diff --git a/examples/scripts/script.py b/examples/scripts/script.py index 3f60c8fb..5195b8cc 100644 --- a/examples/scripts/script.py +++ b/examples/scripts/script.py @@ -4,4 +4,3 @@ Trivial example of a Python script which can be run inside a cmd2 application. """ print("This is a python script running ...") - diff --git a/examples/tab_autocomp_dynamic.py b/examples/tab_autocomp_dynamic.py index af77b204..0c64d020 100755 --- a/examples/tab_autocomp_dynamic.py +++ b/examples/tab_autocomp_dynamic.py @@ -6,8 +6,6 @@ A example usage of AutoCompleter with delayed initialization of the argparse obj Copyright 2018 Eric Lin Released under MIT license, see LICENSE file """ -import argparse -import itertools from typing import List import cmd2 @@ -87,18 +85,15 @@ class TabCompleteExample(cmd2.Cmd): # Add the 'movies' parser as a parent of sub-parser video_types_subparsers.add_parser('movies', parents=[vid_movies_parser], add_help=False) - - vid_shows_parser = argparse_completer.ACArgumentParser(prog='shows') vid_shows_parser.set_defaults(func=TabCompleteExample._do_vid_media_shows) vid_shows_commands_subparsers = vid_shows_parser.add_subparsers(title='Commands', dest='command') - vid_shows_list_parser = vid_shows_commands_subparsers.add_parser('list') + vid_shows_commands_subparsers.add_parser('list') video_types_subparsers.add_parser('shows', parents=[vid_shows_parser], add_help=False) - # For mocking a data source for the example commands ratings_types = ['G', 'PG', 'PG-13', 'R', 'NC-17'] show_ratings = ['TV-Y', 'TV-Y7', 'TV-G', 'TV-PG', 'TV-14', 'TV-MA'] @@ -126,10 +121,10 @@ class TabCompleteExample(cmd2.Cmd): 'Alec Guinness', 'Peter Mayhew', 'Anthony Daniels'] }, 'SW_EP1': {'title': 'Star Wars: Episode I - The Phantom Menace', - 'rating': 'PG', - 'director': ['George Lucas'], - 'actor': ['Liam Neeson', 'Ewan McGregor', 'Natalie Portman', 'Jake Lloyd'] - }, + 'rating': 'PG', + 'director': ['George Lucas'], + 'actor': ['Liam Neeson', 'Ewan McGregor', 'Natalie Portman', 'Jake Lloyd'] + }, 'SW_EP02': {'title': 'Star Wars: Episode II - Attack of the Clones', 'rating': 'PG', 'director': ['George Lucas'], @@ -146,21 +141,21 @@ class TabCompleteExample(cmd2.Cmd): } USER_SHOW_LIBRARY = {'SW_REB': ['S01E01', 'S02E02']} SHOW_DATABASE_IDS = ['SW_CW', 'SW_TCW', 'SW_REB'] - SHOW_DATABASE = {'SW_CW': {'title': 'Star Wars: Clone Wars', - 'rating': 'TV-Y7', - 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], - 2: ['S02E01', 'S02E02', 'S02E03']} - }, - 'SW_TCW': {'title': 'Star Wars: The Clone Wars', - 'rating': 'TV-PG', - 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], - 2: ['S02E01', 'S02E02', 'S02E03']} - }, - 'SW_REB': {'title': 'Star Wars: Rebels', - 'rating': 'TV-Y7', - 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], - 2: ['S02E01', 'S02E02', 'S02E03']} - }, + SHOW_DATABASE = {'SW_CW': {'title': 'Star Wars: Clone Wars', + 'rating': 'TV-Y7', + 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], + 2: ['S02E01', 'S02E02', 'S02E03']} + }, + 'SW_TCW': {'title': 'Star Wars: The Clone Wars', + 'rating': 'TV-PG', + 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], + 2: ['S02E01', 'S02E02', 'S02E03']} + }, + 'SW_REB': {'title': 'Star Wars: Rebels', + 'rating': 'TV-Y7', + 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], + 2: ['S02E01', 'S02E02', 'S02E03']} + }, } file_list = \ @@ -187,12 +182,10 @@ class TabCompleteExample(cmd2.Cmd): return completions_with_desc - ################################################################################### # The media command demonstrates a completer with multiple layers of subcommands # - This example demonstrates how to tag a completion attribute on each action, enabling argument # completion without implementing a complete_COMMAND function - def _do_vid_media_movies(self, args) -> None: if not args.command: self.do_help('video movies') diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py index dad9e90d..1c578c72 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -59,10 +59,10 @@ class TabCompleteExample(cmd2.Cmd): 'Alec Guinness', 'Peter Mayhew', 'Anthony Daniels'] }, 'SW_EP1': {'title': 'Star Wars: Episode I - The Phantom Menace', - 'rating': 'PG', - 'director': ['George Lucas'], - 'actor': ['Liam Neeson', 'Ewan McGregor', 'Natalie Portman', 'Jake Lloyd'] - }, + 'rating': 'PG', + 'director': ['George Lucas'], + 'actor': ['Liam Neeson', 'Ewan McGregor', 'Natalie Portman', 'Jake Lloyd'] + }, 'SW_EP02': {'title': 'Star Wars: Episode II - Attack of the Clones', 'rating': 'PG', 'director': ['George Lucas'], @@ -79,21 +79,21 @@ class TabCompleteExample(cmd2.Cmd): } USER_SHOW_LIBRARY = {'SW_REB': ['S01E01', 'S02E02']} SHOW_DATABASE_IDS = ['SW_CW', 'SW_TCW', 'SW_REB'] - SHOW_DATABASE = {'SW_CW': {'title': 'Star Wars: Clone Wars', - 'rating': 'TV-Y7', - 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], - 2: ['S02E01', 'S02E02', 'S02E03']} - }, - 'SW_TCW': {'title': 'Star Wars: The Clone Wars', - 'rating': 'TV-PG', - 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], - 2: ['S02E01', 'S02E02', 'S02E03']} - }, - 'SW_REB': {'title': 'Star Wars: Rebels', - 'rating': 'TV-Y7', - 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], - 2: ['S02E01', 'S02E02', 'S02E03']} - }, + SHOW_DATABASE = {'SW_CW': {'title': 'Star Wars: Clone Wars', + 'rating': 'TV-Y7', + 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], + 2: ['S02E01', 'S02E02', 'S02E03']} + }, + 'SW_TCW': {'title': 'Star Wars: The Clone Wars', + 'rating': 'TV-PG', + 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], + 2: ['S02E01', 'S02E02', 'S02E03']} + }, + 'SW_REB': {'title': 'Star Wars: Rebels', + 'rating': 'TV-Y7', + 'seasons': {1: ['S01E01', 'S01E02', 'S01E03'], + 2: ['S02E01', 'S02E02', 'S02E03']} + }, } file_list = \ @@ -299,7 +299,8 @@ class TabCompleteExample(cmd2.Cmd): ', '.join(movie['director']), '\n '.join(movie['actor']))) elif args.command == 'add': - print('Adding Movie\n----------------\nTitle: {}\nRating: {}\nDirectors: {}\nActors: {}\n\n'.format(args.title, args.rating, ', '.join(args.director), ', '.join(args.actor))) + print('Adding Movie\n----------------\nTitle: {}\nRating: {}\nDirectors: {}\nActors: {}\n\n' + .format(args.title, args.rating, ', '.join(args.director), ', '.join(args.actor))) def _do_media_shows(self, args) -> None: if not args.command: @@ -367,7 +368,6 @@ class TabCompleteExample(cmd2.Cmd): # No subcommand was provided, so call help self.do_help('media') - # This completer is implemented using a single dictionary to look up completion lists for all layers of # subcommands. For each argument, AutoCompleter will search for completion values from the provided # arg_choices dict. This requires careful naming of argparse arguments so that there are no unintentional diff --git a/examples/table_display.py b/examples/table_display.py index 7541e548..04415afd 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -52,11 +52,11 @@ EXAMPLE_ITERABLE_DATA = [['Shanghai (上海)', 'Shanghai', 'China', 'Asia', 2418 ['Guangzho (广州市)', 'Guangdong', 'China', 'Asia', 13081000, 1347.81], ['Mumbai (मुंबई)', 'Maharashtra', 'India', 'Asia', 12442373, 465.78], ['Istanbul (İstanbuld)', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29], - ] + ] # Calculate population density for row in EXAMPLE_ITERABLE_DATA: - row.append(row[-2]/row[-1]) + row.append(row[-2] / row[-1]) # Column headers plus optional formatting info for each column @@ -69,7 +69,7 @@ COLUMNS = [tf.Column('City', width=11, header_halign=tf.ColumnAlignment.AlignCen cell_halign=tf.ColumnAlignment.AlignRight, formatter=two_dec), tf.Column('Pop. Density (/km²)', width=12, header_halign=tf.ColumnAlignment.AlignCenter, cell_halign=tf.ColumnAlignment.AlignRight, formatter=no_dec), - ] + ] # ######## Table data formatted as an iterable of python objects ######### @@ -119,7 +119,7 @@ OBJ_COLS = [tf.Column('City', attrib='city', header_halign=tf.ColumnAlignment.Al cell_halign=tf.ColumnAlignment.AlignRight, formatter=two_dec), tf.Column('Pop. Density (/km²)', width=12, header_halign=tf.ColumnAlignment.AlignCenter, cell_halign=tf.ColumnAlignment.AlignRight, obj_formatter=pop_density), - ] + ] EXTREMELY_HIGH_POULATION_DENSITY = 25000 -- cgit v1.2.1