From 752c20a7cb846b4e76b2d23aa775264973a737ce Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Fri, 5 Jun 2020 14:36:33 +0000 Subject: [PATCH] Refactored by Sourcery --- misc/abandoned_roman_numerals_example/rome.py | 10 +++-- misc/get_stats.py | 20 +++++---- misc/plot.py | 5 +-- tests/book_parser.py | 6 +-- tests/book_tester.py | 45 +++++++------------ tests/slimerjs-0.9.0/slimerjs.py | 13 +++--- tests/source_updater.py | 9 ++-- tests/sourcetree.py | 17 ++++--- tests/test_book_tester.py | 4 +- ...chapter_automate_deployment_with_fabric.py | 2 +- ...pter_making_deployment_production_ready.py | 4 +- tests/test_chapter_server_side_debugging.py | 4 +- tests/test_source_updater.py | 6 +-- tests/test_sourcetree.py | 40 ++++++++--------- tests/test_write_to_file.py | 2 +- tests/write_to_file.py | 22 ++++----- 16 files changed, 95 insertions(+), 114 deletions(-) diff --git a/misc/abandoned_roman_numerals_example/rome.py b/misc/abandoned_roman_numerals_example/rome.py index a7f224a62..b7c294478 100644 --- a/misc/abandoned_roman_numerals_example/rome.py +++ b/misc/abandoned_roman_numerals_example/rome.py @@ -1,6 +1,6 @@ ROMAN_NUMERALS = ('I', 'V', 'X') def add(augend, addend): - if not isinstance(augend, basestring) or not isinstance(addend, basestring): + if not (isinstance(augend, basestring) and isinstance(addend, basestring)): raise ValueError simple_augend = augend.replace('IV', 'IIII') @@ -13,6 +13,10 @@ def add(augend, addend): ordered_sum = ''.join(reversed(sorted(simple_sum))) - canonicalised_sum = ordered_sum.replace('IIIII', 'V').replace('IIII', 'IV').replace('VV', 'X').replace('VIV', 'IX') - return canonicalised_sum + return ( + ordered_sum.replace('IIIII', 'V') + .replace('IIII', 'IV') + .replace('VV', 'X') + .replace('VIV', 'IX') + ) diff --git a/misc/get_stats.py b/misc/get_stats.py index aedcf9345..c7e52eef0 100644 --- a/misc/get_stats.py +++ b/misc/get_stats.py @@ -48,7 +48,7 @@ def main(): for commit in commits: checkout_commit(commit.hash) all_wordcounts[commit] = get_wordcounts() - filenames.update(set(wc.filename for wc in all_wordcounts[commit])) + filenames.update({wc.filename for wc in all_wordcounts[commit]}) with open(os.path.join(BOOK_ROOT, 'wordcounts.tsv'), 'w') as csvfile: fieldnames = ['date.{}'.format(thing) for thing in ['year', 'month', 'day', 'hour']] @@ -58,14 +58,16 @@ def main(): writer = csv.DictWriter(csvfile, fieldnames, dialect="excel-tab") writer.writeheader() for commit, wordcounts in all_wordcounts.items(): - row = {} - row['hash'] = commit.hash - row['subject'] = commit.subject - row['date'] = '' - row['date.year'] = commit.date.year - row['date.month'] = commit.date.month - row['date.day'] = commit.date.day - row['date.hour'] = commit.date.hour + row = { + 'hash': commit.hash, + 'subject': commit.subject, + 'date': '', + 'date.year': commit.date.year, + 'date.month': commit.date.month, + 'date.day': commit.date.day, + 'date.hour': commit.date.hour, + } + for wordcount in wordcounts: row[wordcount.filename + " (words)"] = wordcount.words row[wordcount.filename + " (lines)"] = wordcount.lines diff --git a/misc/plot.py b/misc/plot.py index 408c4810b..cb6ed9610 100644 --- a/misc/plot.py +++ b/misc/plot.py @@ -14,10 +14,7 @@ def get_data_from_csv(): for field in reader.fieldnames: if 'words' in field: val = row[field] - if val: - fixed_row[field] = val - else: - fixed_row[field] = 0 + fixed_row[field] = val if val else 0 date = datetime(int(row['date.year']), int(row['date.month']), int(row['date.day']), int(row['date.hour']),) fixed_row['date'] = date data.append(fixed_row) diff --git a/tests/book_parser.py b/tests/book_parser.py index 08cfcd584..ed7418f1d 100644 --- a/tests/book_parser.py +++ b/tests/book_parser.py @@ -121,11 +121,7 @@ def parse_output(listing): outputs = [] output_before = listing.text - if output_before: - output_before = fix_newlines(output_before.strip()) - else: - output_before = '' - + output_before = fix_newlines(output_before.strip()) if output_before else '' for command in commands: if '$' in output_before and '\n' in output_before: last_cr = output_before.rfind('\n') diff --git a/tests/book_tester.py b/tests/book_tester.py index c95355f33..fa84e50fb 100644 --- a/tests/book_tester.py +++ b/tests/book_tester.py @@ -42,8 +42,8 @@ def contains(inseq, subseq): return any( - inseq[pos:pos + len(subseq)] == subseq - for pos in range(0, len(inseq) - len(subseq) + 1) + inseq[pos : pos + len(subseq)] == subseq + for pos in range(len(inseq) - len(subseq) + 1) ) @@ -73,12 +73,11 @@ def strip_mock_ids(output): r"Mock name='\1' id='XX'>", output, ) - strip_all_mocks = re.sub( + return re.sub( r"Mock id='(\d+)'>", r"Mock id='XX'>", strip_mocks_with_names, ) - return strip_all_mocks def strip_object_ids(output): return re.sub('0x([0-9a-f]+)>', '0xXX>', output) @@ -106,13 +105,12 @@ def strip_git_hashes(output): r"index XXXXXXX\.\.XXXXXXX 100644", output, ) - fixed_commit_numbers = re.sub( + return re.sub( r"^[a-f0-9]{7} ", r"XXXXXXX ", fixed_indexes, flags=re.MULTILINE, ) - return fixed_commit_numbers def strip_callouts(output): @@ -122,13 +120,12 @@ def strip_callouts(output): output, flags=re.MULTILINE, ) - minus_new_callouts = re.sub( + return re.sub( r"^(.+) \(\d+\)$", r"\1", minus_old_callouts, flags=re.MULTILINE, ) - return minus_new_callouts def standardise_library_paths(output): @@ -527,9 +524,12 @@ def assert_console_output_correct(self, actual, expected, ls=False): else: self.assertLineIn(line, [l.strip() for l in actual_lines]) - if len(expected_lines) > 4 and '[...' not in expected_fixed: - if expected.type != 'qunit output': - self.assertMultiLineEqual(actual_fixed.strip(), expected_fixed.strip()) + if ( + len(expected_lines) > 4 + and '[...' not in expected_fixed + and expected.type != 'qunit output' + ): + self.assertMultiLineEqual(actual_fixed.strip(), expected_fixed.strip()) expected.was_checked = True @@ -614,10 +614,6 @@ def unset_PYTHONDONTWRITEBYTECODE(self): def _strip_out_any_pycs(self): return - self.sourcetree.run_command( - r"find . -name __pycache__ -exec rm -rf {} \;", - ignore_errors=True - ) def run_test_and_check_result(self, bdd=False): @@ -654,12 +650,6 @@ def run_js_tests(self, tests_path): print('fixed phantomjs output', output) return output - os.chmod(SLIMERJS_BINARY, os.stat(SLIMERJS_BINARY).st_mode | stat.S_IXUSR) - os.environ['SLIMERJSLAUNCHER'] = '/usr/bin/firefox' - return subprocess.check_output( - ['xvfb-run', '--auto-servernum', SLIMERJS_BINARY, PHANTOMJS_RUNNER, tests_path] - ).decode() - def check_qunit_output(self, expected_output): lists_tests = os.path.join( @@ -735,9 +725,10 @@ def check_diff_or_status(self, pos): 'diff' in self.listings[pos] or 'status' in self.listings[pos] ) git_output = self.run_command(self.listings[pos]) - if not any('/' + l in git_output for l in LIKELY_FILES): - if not any(f in git_output for f in ('lists/', 'functional_tests.py')): - self.fail('no likely files in diff output %s' % (git_output,)) + if all('/' + l not in git_output for l in LIKELY_FILES) and all( + f not in git_output for f in ('lists/', 'functional_tests.py') + ): + self.fail('no likely files in diff output %s' % (git_output,)) self.pos += 1 comment = self.listings[pos + 1] if comment.skip: @@ -823,11 +814,7 @@ def run_interactive_manage_py(self, listing): expected_output = output_before output_after = None next_output = None - if user_input == '2': - ignore_errors = True - else: - ignore_errors = False - + ignore_errors = True if user_input == '2' else False else: user_input = None expected_output = output_before diff --git a/tests/slimerjs-0.9.0/slimerjs.py b/tests/slimerjs-0.9.0/slimerjs.py index 14b16aed2..e2d0bf3b3 100644 --- a/tests/slimerjs-0.9.0/slimerjs.py +++ b/tests/slimerjs-0.9.0/slimerjs.py @@ -39,7 +39,7 @@ def which(program): if SLIMERJSLAUNCHER == "": POSSIBLE_PATH = [] - if sys.platform == "linux" or sys.platform == "linux2" or sys.platform == "darwin": + if sys.platform in ["linux", "linux2", "darwin"]: POSSIBLE_PATH.append(os.path.join(SLIMERJS_PATH, "xulrunner", "xulrunner")) path = which('firefox') if path != None: @@ -139,7 +139,7 @@ def showHelp(): "--reset-profile","--profile","--p","--createprofile","--profilemanager", ] for arg in SYS_ARGS: - if arg == '--help' or arg == "-h": + if arg in ['--help', "-h"]: showHelp() sys.exit(0) @@ -165,8 +165,11 @@ def showHelp(): os.environ.data['__SLIMER_ARGS'] = string.join(SYS_ARGS,' ') # launch slimerjs with firefox/xulrunner -SLCMD = [ SLIMERJSLAUNCHER ] -SLCMD.extend(["-app", os.path.join(SLIMERJS_PATH, "application.ini"), "-no-remote"]) +SLCMD = [ + SLIMERJSLAUNCHER, + *["-app", os.path.join(SLIMERJS_PATH, "application.ini"), "-no-remote"], +] + if sys.platform == "win32": SLCMD.extend(["-attach-console"]) SLCMD.extend(PROFILE) @@ -190,5 +193,5 @@ def showHelp(): if CREATE_TEMP: shutil.rmtree(PROFILE_DIR) - + sys.exit(exitCode) diff --git a/tests/source_updater.py b/tests/source_updater.py index 3a55ab8d5..afd8363ff 100644 --- a/tests/source_updater.py +++ b/tests/source_updater.py @@ -174,11 +174,10 @@ def find_first_nonimport_line(self): except StopIteration: return len(self.lines) pos = self.lines.index(first_nonimport) - if self._import_nodes: - if pos < max(n.lineno for n in self._import_nodes): - raise SourceUpdateError('first nonimport (%s) was before end of imports (%s)' % ( - first_nonimport, max(n.lineno for n in self._import_nodes)) - ) + if self._import_nodes and pos < max(n.lineno for n in self._import_nodes): + raise SourceUpdateError('first nonimport (%s) was before end of imports (%s)' % ( + first_nonimport, max(n.lineno for n in self._import_nodes)) + ) return pos diff --git a/tests/sourcetree.py b/tests/sourcetree.py index 24b302deb..a5c6c4abc 100644 --- a/tests/sourcetree.py +++ b/tests/sourcetree.py @@ -30,17 +30,17 @@ def from_diff(commit_info): commit.all_lines = commit.info.split('\n') commit.lines_to_add = [ - l[1:] for l in commit.all_lines - if l.startswith('+') and - l[1:].strip() and - not l[1] == '+' + l[1:] + for l in commit.all_lines + if l.startswith('+') and l[1:].strip() and l[1] != '+' ] + commit.lines_to_remove = [ - l[1:] for l in commit.all_lines - if l.startswith('-') and - l[1:].strip() and - not l[1] == '-' + l[1:] + for l in commit.all_lines + if l.startswith('-') and l[1:].strip() and l[1] != '-' ] + commit.moved_lines = [ l for l in commit.lines_to_add if l in commit.lines_to_remove ] @@ -130,7 +130,6 @@ def run_command(self, command, cwd=None, user_input=None, ignore_errors=False, s print(output) except io.BlockingIOError as e: print(e) - pass return output diff --git a/tests/test_book_tester.py b/tests/test_book_tester.py index 4f9093546..1bb606c60 100644 --- a/tests/test_book_tester.py +++ b/tests/test_book_tester.py @@ -641,8 +641,8 @@ def test_ignores_3_5_x_AssertionError_None_thing(self): actual = "AssertionError" expected = Output("AssertionError: None") self.assert_console_output_correct(actual, expected) - actual2 = "AssertionError: something" with self.assertRaises(AssertionError): + actual2 = "AssertionError: something" self.assert_console_output_correct(actual2, expected) @@ -662,9 +662,9 @@ def test_ignores_localhost_server_port_5_digits(self): def test_only_ignores_exactly_32_char_strings_no_whitespace(self): - actual = "qnslckvp2aga7tm6xuivyb0ob1akzzwl" expected = Output("jvhzc8kj2mkh06xooqq9iciptead20qq") with self.assertRaises(AssertionError): + actual = "qnslckvp2aga7tm6xuivyb0ob1akzzwl" self.assert_console_output_correct(actual[:-1], expected[:-1]) self.assert_console_output_correct(actual + '1', expected + 'a') self.assert_console_output_correct(' ' + actual, ' ' + expected) diff --git a/tests/test_chapter_automate_deployment_with_fabric.py b/tests/test_chapter_automate_deployment_with_fabric.py index 684079beb..f9d1648d4 100644 --- a/tests/test_chapter_automate_deployment_with_fabric.py +++ b/tests/test_chapter_automate_deployment_with_fabric.py @@ -21,7 +21,6 @@ def test_listings_and_commands_and_output(self): self.start_with_checkout() - vm_restore = 'MAKING_END' # hack fast-forward skip = False if skip: @@ -31,6 +30,7 @@ def test_listings_and_commands_and_output(self): )) if DO_SERVER_COMMANDS: + vm_restore = 'MAKING_END' subprocess.check_call(['vagrant', 'snapshot', 'restore', vm_restore]) self.current_server_cd = '~/sites/superlists-staging.ottg.eu' diff --git a/tests/test_chapter_making_deployment_production_ready.py b/tests/test_chapter_making_deployment_production_ready.py index 1605ad9d2..702a5ab6f 100644 --- a/tests/test_chapter_making_deployment_production_ready.py +++ b/tests/test_chapter_making_deployment_production_ready.py @@ -40,8 +40,6 @@ def test_listings_and_commands_and_output(self): ' && git reset --hard origin/chapter_making_deployment_production_ready', ) - vm_restore = 'MANUAL_END' - # hack fast-forward skip = False if skip: @@ -51,6 +49,8 @@ def test_listings_and_commands_and_output(self): )) if DO_SERVER_COMMANDS: + vm_restore = 'MANUAL_END' + subprocess.check_call(['vagrant', 'snapshot', 'restore', vm_restore]) self.current_server_cd = '~/sites/$SITENAME' diff --git a/tests/test_chapter_server_side_debugging.py b/tests/test_chapter_server_side_debugging.py index 0f7cf9d8e..ea1742ab5 100644 --- a/tests/test_chapter_server_side_debugging.py +++ b/tests/test_chapter_server_side_debugging.py @@ -35,8 +35,6 @@ def test_listings_and_commands_and_output(self): self.start_with_checkout() self.prep_database() - vm_restore = 'FABRIC_END' - # hack fast-forward skip = False if skip: @@ -46,6 +44,8 @@ def test_listings_and_commands_and_output(self): )) if DO_SERVER_COMMANDS: + vm_restore = 'FABRIC_END' + subprocess.check_call(['vagrant', 'snapshot', 'restore', vm_restore]) while self.pos < len(self.listings): diff --git a/tests/test_source_updater.py b/tests/test_source_updater.py index 8d6d1e9d6..63e0ceee2 100644 --- a/tests/test_source_updater.py +++ b/tests/test_source_updater.py @@ -644,7 +644,7 @@ def test_finding_start_line(self): assert source.find_start_line(['bla bla', 'whatever']) == 3 assert source.find_start_line(['indented', 'whatever']) == 4 assert source.find_start_line([' indented', 'whatever']) == 4 - assert source.find_start_line(['no such line', 'whatever']) == None + assert source.find_start_line(['no such line', 'whatever']) is None with self.assertRaises(SourceUpdateError): source.find_start_line(['']) with self.assertRaises(SourceUpdateError): @@ -666,8 +666,8 @@ def test_finding_end_line(self): assert source.find_end_line(['stuff', 'things']) == 1 assert source.find_end_line(['bla bla', 'whatever', 'more']) == 5 - assert source.find_end_line(['bla bla', 'whatever']) == None - assert source.find_end_line(['no such line', 'whatever']) == None + assert source.find_end_line(['bla bla', 'whatever']) is None + assert source.find_end_line(['no such line', 'whatever']) is None with self.assertRaises(SourceUpdateError): source.find_end_line([]) with self.assertRaises(SourceUpdateError): diff --git a/tests/test_sourcetree.py b/tests/test_sourcetree.py index 2a08047e1..143c3ec17 100644 --- a/tests/test_sourcetree.py +++ b/tests/test_sourcetree.py @@ -737,32 +737,32 @@ def test_get_offset_when_some(self): def test_over_indentation_differences_are_picked_up(self): - lines = [ - "def method1(self):", - " # amend method 1", - " return 2", - ] - future_lines = [ - " def method1(self):", - " # amend method 1", - " return 2", - ] with self.assertRaises(ApplyCommitException): + lines = [ + "def method1(self):", + " # amend method 1", + " return 2", + ] + future_lines = [ + " def method1(self):", + " # amend method 1", + " return 2", + ] check_indentation(lines, future_lines) def test_under_indentation_differences_are_picked_up(self): - lines = [ - "def method1(self):", - "# amend method 1", - " return 2", - ] - future_lines = [ - " def method1(self):", - " # amend method 1", - " return 2", - ] with self.assertRaises(ApplyCommitException): + lines = [ + "def method1(self):", + "# amend method 1", + " return 2", + ] + future_lines = [ + " def method1(self):", + " # amend method 1", + " return 2", + ] check_indentation(lines, future_lines) diff --git a/tests/test_write_to_file.py b/tests/test_write_to_file.py index a927f7d50..44f8e16f9 100644 --- a/tests/test_write_to_file.py +++ b/tests/test_write_to_file.py @@ -133,7 +133,7 @@ def test_existing_file_bears_no_relation_means_replaced(self): old = '#abc\n#def\n#ghi\n#jkl\n' new = '#mno\n#pqr\n#stu\n#vvv\n' expected = new - self.assert_write_to_file_gives(old, new, expected) + self.assert_write_to_file_gives(old, expected, expected) def test_existing_file_has_views_means_apppend(self): diff --git a/tests/write_to_file.py b/tests/write_to_file.py index 891222464..4a06ae425 100644 --- a/tests/write_to_file.py +++ b/tests/write_to_file.py @@ -15,10 +15,7 @@ def _replace_lines_from_to(old_lines, new_lines, start_pos, end_pos): print('replace lines from line', start_pos, 'to line', end_pos) old_indent = get_indent(old_lines[start_pos]) new_indent = get_indent(new_lines[0]) - if new_indent: - missing_indent = old_indent[:-len(new_indent)] - else: - missing_indent = old_indent + missing_indent = old_indent[:-len(new_indent)] if new_indent else old_indent indented_new_lines = [missing_indent + l for l in new_lines] return '\n'.join( old_lines[:start_pos] + @@ -74,8 +71,7 @@ def _replace_single_line(old_lines, new_lines): line_finder = lambda l: number_of_identical_chars(l, new_line) likely_line = sorted(old_lines, key=line_finder)[-1] new_line = get_indent(likely_line) + new_line - new_content = '\n'.join(old_lines).replace(likely_line, new_line) - return new_content + return '\n'.join(old_lines).replace(likely_line, new_line) def _replace_lines_in(old_lines, new_lines): @@ -93,12 +89,11 @@ def _replace_lines_in(old_lines, new_lines): new_contents = new_lines[0] + '\n' return new_contents + _replace_lines_in(old_lines[1:], new_lines[1:]) - if VIEW_FINDER.match(new_lines[0]): - if source.views: - view_name = VIEW_FINDER.search(new_lines[0]).group(1) - if view_name in source.views: - return source.replace_function(new_lines) - return '\n'.join(old_lines) + '\n\n' + '\n'.join(new_lines) + if VIEW_FINDER.match(new_lines[0]) and source.views: + view_name = VIEW_FINDER.search(new_lines[0]).group(1) + if view_name in source.views: + return source.replace_function(new_lines) + return '\n'.join(old_lines) + '\n\n' + '\n'.join(new_lines) class_finder = re.compile(r'^class \w+\(.+\):$', re.MULTILINE) if class_finder.match(new_lines[0]): @@ -140,10 +135,9 @@ def _find_last_line_for_class(source, classname): all_nodes = list(ast.walk(ast.parse(source))) classes = [n for n in all_nodes if isinstance(n, ast.ClassDef)] our_class = next(c for c in classes if c.name == classname) - last_line_in_our_class = max( + return max( getattr(thing, 'lineno', 0) for thing in ast.walk(our_class) ) - return last_line_in_our_class def add_to_class(new_lines, old_lines):