-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,4 +7,4 @@ | |
| parser.add_argument('-n', '--name', metavar='name', | ||
| default='World', help='Name to greet') | ||
| args = parser.parse_args() | ||
| print('Hello, ' + args.name + '!') | ||
| print(f'Hello, {args.name}!') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ def main(): | |
| parser.add_argument('-n', '--name', metavar='name', | ||
| default='World', help='Name to greet') | ||
| args = parser.parse_args() | ||
| print('Hello, ' + args.name + '!') | ||
| print(f'Hello, {args.name}!') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ def get_args(): | |
|
|
||
| def main(): | ||
| args = get_args() | ||
| print('Hello, ' + args.name + '!') | ||
| print(f'Hello, {args.name}!') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ def main(): | |
| """Make a jazz noise here""" | ||
|
|
||
| args = get_args() | ||
| print('Hello, ' + args.name + '!') | ||
| print(f'Hello, {args.name}!') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| # -------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,7 @@ def main(): | |
| elif num == 2: | ||
| bringing = ' and '.join(items) | ||
| else: | ||
| items[-1] = 'and ' + items[-1] | ||
| items[-1] = f'and {items[-1]}' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| bringing = ', '.join(items) | ||
|
|
||
| print('You are bringing {}.'.format(bringing)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,9 +26,7 @@ def main(): | |
| '6': '4', '7': '3', '8': '2', '9': '1', '0': '5'} | ||
|
|
||
| # Method 2: for loop to build new string | ||
| new_text = '' | ||
| for char in args.text: | ||
| new_text += jumper.get(char, char) | ||
| new_text = ''.join(jumper.get(char, char) for char in args.text) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print(new_text) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,9 +26,7 @@ def main(): | |
| '6': '4', '7': '3', '8': '2', '9': '1', '0': '5'} | ||
|
|
||
| # Method 3: for loop to build new list | ||
| new_text = [] | ||
| for char in args.text: | ||
| new_text.append(jumper.get(char, char)) | ||
| new_text = [jumper.get(char, char) for char in args.text] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print(''.join(new_text)) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,10 +34,7 @@ def main(): | |
|
|
||
| args = get_args() | ||
|
|
||
| lookup = {} | ||
| for line in args.file: | ||
| lookup[line[0].upper()] = line.rstrip() | ||
|
|
||
| lookup = {line[0].upper(): line.rstrip() for line in args.file} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| for letter in args.letter: | ||
| if letter.upper() in lookup: | ||
| print(lookup[letter.upper()]) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,12 +43,14 @@ def verse(bottle): | |
| s1 = '' if bottle == 1 else 's' | ||
| s2 = '' if next_bottle == 1 else 's' | ||
| num_next = 'No more' if next_bottle == 0 else next_bottle | ||
| return '\n'.join([ | ||
| f'{bottle} bottle{s1} of beer on the wall,', | ||
| f'{bottle} bottle{s1} of beer,', | ||
| f'Take one down, pass it around,', | ||
| f'{num_next} bottle{s2} of beer on the wall!', | ||
| ]) | ||
| return '\n'.join( | ||
| [ | ||
| f'{bottle} bottle{s1} of beer on the wall,', | ||
| f'{bottle} bottle{s1} of beer,', | ||
| 'Take one down, pass it around,', | ||
| f'{num_next} bottle{s2} of beer on the wall!', | ||
| ] | ||
| ) | ||
|
Comment on lines
-46
to
+53
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| # -------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,10 +39,7 @@ def main(): | |
| random.seed(args.seed) | ||
|
|
||
| # Method 1: Iterate each character, add to list | ||
| ransom = [] | ||
| for char in args.text: | ||
| ransom.append(choose(char)) | ||
|
|
||
| ransom = [choose(char) for char in args.text] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print(''.join(ransom)) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,10 +39,7 @@ def main(): | |
| random.seed(args.seed) | ||
|
|
||
| # Method 3: Iterate each character, add to a str | ||
| ransom = '' | ||
| for char in args.text: | ||
| ransom += choose(char) | ||
|
|
||
| ransom = ''.join(choose(char) for char in args.text) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print(''.join(ransom)) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,7 +76,7 @@ def verse(day): | |
| lines.extend(reversed(gifts[:day])) | ||
|
|
||
| if day > 1: | ||
| lines[-1] = 'And ' + lines[-1].lower() | ||
| lines[-1] = f'And {lines[-1].lower()}' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| return '\n'.join(lines) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,7 +77,7 @@ def verse(day): | |
| lines.extend(reversed(gifts[:day])) | ||
|
|
||
| if day > 1: | ||
| lines[-1] = 'And ' + lines[-1].lower() | ||
| lines[-1] = f'And {lines[-1].lower()}' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| return '\n'.join(lines) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,7 +98,7 @@ def test_all(): | |
| os.remove(out_file) | ||
|
|
||
| try: | ||
| out = getoutput(cmd + f' -o {out_file}').rstrip() | ||
| out = getoutput(f'{cmd} -o {out_file}').rstrip() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| assert out == '' | ||
| assert os.path.isfile(out_file) | ||
| output = open(out_file).read().rstrip() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,21 +44,17 @@ def stemmer(word): | |
| vowels = 'aeiou' | ||
| consonants = ''.join( | ||
| [c for c in string.ascii_lowercase if c not in vowels]) | ||
| pattern = ( | ||
| '([' + consonants + ']+)?' # capture one or more, optional | ||
| '([' + vowels + '])' # capture at least one vowel | ||
| '(.*)' # capture zero or more of anything | ||
| ) | ||
| pattern = (((f'([{consonants}' + ']+)?' # capture one or more, optional | ||
| '([') + vowels) + '])' # capture at least one vowel | ||
| '(.*)') | ||
| pattern = f'([{consonants}]+)?([{vowels}])(.*)' | ||
|
|
||
| match = re.match(pattern, word) | ||
| if match: | ||
| p1 = match.group(1) or '' | ||
| p2 = match.group(2) or '' | ||
| p3 = match.group(3) or '' | ||
| return (p1, p2 + p3) | ||
| else: | ||
| if not (match := re.match(pattern, word)): | ||
| return (word, '') | ||
| p1 = match.group(1) or '' | ||
| p2 = match.group(2) or '' | ||
| p3 = match.group(3) or '' | ||
| return (p1, p2 + p3) | ||
|
Comment on lines
-47
to
+57
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
|
|
||
| # -------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,9 +39,9 @@ def stemmer(word): | |
| """Return leading consonants (if any), and 'stem' of word""" | ||
|
|
||
| word = word.lower() | ||
| vowel_pos = list(map(word.index, filter(lambda v: v in word, 'aeiou'))) | ||
|
|
||
| if vowel_pos: | ||
| if vowel_pos := list( | ||
| map(word.index, filter(lambda v: v in word, 'aeiou')) | ||
| ): | ||
|
Comment on lines
-42
to
+44
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| first_vowel = min(vowel_pos) | ||
| return (word[:first_vowel], word[first_vowel:]) | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,9 +41,10 @@ def fry(word): | |
| if word.lower() == 'you': | ||
| return word[0] + "'all" | ||
|
|
||
| if word.endswith('ing'): | ||
| if any(map(lambda c: c.lower() in 'aeiouy', word[:-3])): | ||
| return word[:-1] + "'" | ||
| if word.endswith('ing') and any( | ||
| map(lambda c: c.lower() in 'aeiouy', word[:-3]) | ||
| ): | ||
| return word[:-1] + "'" | ||
|
Comment on lines
-44
to
+47
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| return word | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,7 @@ def run_file(file): | |
| """run with file""" | ||
|
|
||
| assert os.path.isfile(file) | ||
| expected_file = file + '.out' | ||
| expected_file = f'{file}.out' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| assert os.path.isfile(expected_file) | ||
| expected = open(expected_file).read() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ def main(): | |
| pos = placeholder[1:-1] | ||
| article = 'an' if pos.lower()[0] in 'aeiou' else 'a' | ||
| answer = inputs.pop(0) if inputs else input(tmpl.format(article, pos)) | ||
| text = text[0:start] + answer + text[stop + 1:] | ||
| text = text[:start] + answer + text[stop + 1:] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| had_placeholders = True | ||
|
|
||
| if had_placeholders: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,8 +85,7 @@ def read_csv(fh): | |
| for row in csv.DictReader(fh, delimiter=','): | ||
| name, reps = row.get('exercise'), row.get('reps') | ||
| if name and reps: | ||
| match = re.match(r'(\d+)-(\d+)', reps) | ||
| if match: | ||
| if match := re.match(r'(\d+)-(\d+)', reps): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| low, high = map(int, match.groups()) | ||
| exercises.append((name, low, high)) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,5 @@ | |
|
|
||
| with open('inputs/exercises.csv') as fh: | ||
| reader = csv.DictReader(fh, delimiter=',') | ||
| records = [] | ||
| for rec in reader: | ||
| records.append(rec) | ||
|
|
||
| records = list(reader) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| pprint(records) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,9 +69,10 @@ def main(): | |
| def format_board(board): | ||
| """Format the board""" | ||
|
|
||
| cells = [] | ||
| for i, char in enumerate(board, start=1): | ||
| cells.append(str(i) if char == '.' else char) | ||
| cells = [ | ||
| str(i) if char == '.' else char | ||
| for i, char in enumerate(board, start=1) | ||
| ] | ||
|
Comment on lines
-72
to
+75
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| bar = '-------------' | ||
| cells_tmpl = '| {} | {} | {} |' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -220,7 +220,7 @@ def test_losing(): | |
| """test losing boards""" | ||
|
|
||
| losing_board = list('XXOO.....') | ||
| for i in range(10): | ||
| for _ in range(10): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| random.shuffle(losing_board) | ||
| out = getoutput(f'{prg} -b {"".join(losing_board)}').splitlines() | ||
| assert out[-1].strip() == 'No winner.' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,6 @@ def test_losing(): | |
|
|
||
| losing_state = list('XXOO.....') | ||
|
|
||
| for i in range(10): | ||
| for _ in range(10): | ||
| random.shuffle(losing_state) | ||
| assert find_winner(''.join(losing_state)) == None | ||
| assert find_winner(''.join(losing_state)) is None | ||
|
Comment on lines
-66
to
+68
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ def main(): | |
| """Make a jazz noise here""" | ||
|
|
||
| args = get_args() | ||
| print('Hello, ' + args.name + '!') | ||
| print(f'Hello, {args.name}!') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| # -------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -180,8 +180,7 @@ def get_defaults(): | |
| defaults = {} | ||
| if os.path.isfile(rc): | ||
| for line in open(rc): | ||
| match = re.match('([^=]+)=([^=]+)', line) | ||
| if match: | ||
| if match := re.match('([^=]+)=([^=]+)', line): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| key, val = map(str.strip, match.groups()) | ||
| if key and val: | ||
| defaults[key] = val | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,7 +74,7 @@ def test_space(): | |
| word = random.choice([' ', '\t']) | ||
| rv, out = getstatusoutput(f'{prg} "{word}"') | ||
| assert rv == 0 | ||
| assert out == f'input is space.' | ||
| assert out == 'input is space.' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| # -------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,9 +108,12 @@ def run(input_seq, codons, expected): | |
| random_file = random_filename() | ||
| try: | ||
| flip = random.randint(0, 1) | ||
| out_file, out_arg = (random_file, | ||
| '-o ' + random_file) if flip == 1 else ('out.txt', | ||
| '') | ||
| out_file, out_arg = ( | ||
| (random_file, f'-o {random_file}') | ||
| if flip == 1 | ||
| else ('out.txt', '') | ||
| ) | ||
|
|
||
|
Comment on lines
-111
to
+116
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print(f'{prg} -c {codons} {out_arg} {input_seq}') | ||
| rv, output = getstatusoutput(f'{prg} -c {codons} {out_arg} {input_seq}') | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,7 +105,7 @@ def test_defaults(): | |
| def test_options(): | ||
| """runs on good input""" | ||
|
|
||
| out_file = random_string() + '.fasta' | ||
| out_file = f'{random_string()}.fasta' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| try: | ||
| if os.path.isfile(out_file): | ||
| os.remove(out_file) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,15 +65,14 @@ def main(): | |
| out_file = os.path.join(args.outdir, basename) | ||
| print(f'{i:3}: {basename}') | ||
|
|
||
| out_fh = open(out_file, 'wt') | ||
| num_taken = 0 | ||
| with open(out_file, 'wt') as out_fh: | ||
| num_taken = 0 | ||
|
|
||
| for rec in SeqIO.parse(fh, 'fasta'): | ||
| if random.random() <= args.pct: | ||
| num_taken += 1 | ||
| SeqIO.write(rec, out_fh, 'fasta') | ||
| for rec in SeqIO.parse(fh, 'fasta'): | ||
| if random.random() <= args.pct: | ||
| num_taken += 1 | ||
| SeqIO.write(rec, out_fh, 'fasta') | ||
|
|
||
| out_fh.close() | ||
|
Comment on lines
-68
to
-76
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| total_num += num_taken | ||
|
|
||
| num_files = len(args.file) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,10 +123,10 @@ def test_options(): | |
| files = os.listdir(out_dir) | ||
| assert len(files) == 3 | ||
|
|
||
| seqs_written = 0 | ||
| for file in files: | ||
| seqs_written += len( | ||
| list(SeqIO.parse(os.path.join(out_dir, file), 'fasta'))) | ||
| seqs_written = sum( | ||
| len(list(SeqIO.parse(os.path.join(out_dir, file), 'fasta'))) | ||
| for file in files | ||
| ) | ||
|
Comment on lines
-126
to
+129
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| assert seqs_written == 27688 | ||
| finally: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines
9-9refactored with the following changes:use-fstring-for-concatenation)