Sourcery refactored master branch#1
Conversation
| if c in [13, 15, 16]: | ||
| if c in {13, 15, 16}: | ||
| cnn = cn | ||
| while cnn > 9: | ||
| scn = str(cnn) | ||
| i = int(scn[-2]) | ||
| i = i*2 | ||
| i = int(scn[-2]) * 2 |
There was a problem hiding this comment.
Lines 8-52 refactored with the following changes:
- Use set when checking membership of a collection of literals (
collection-into-set) - Replace assignment and augmented assignment with single assignment (
merge-assign-and-aug-assign) - Replace assignment with augmented assignment (
aug-assign) - Simplify conditional into switch-like form [×6] (
switch) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] [×2] (
remove-redundant-slice-index) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
This removes the following comments ( why? ):
# Check for AMEX
# Check for MASTERCARD and VISA
| # Opens csv and dna files | ||
| file1 = open(argv[1], 'r') | ||
| file2 = open(argv[2], 'r') | ||
| read1 = csv.DictReader(file1) | ||
| fnames = read1.fieldnames | ||
| read1 = csv.reader(file1) | ||
| read2 = file2.read() | ||
| dictr = [] | ||
| dnal = {} | ||
| # Creates a dictionary with csv data | ||
| for i in read1: | ||
| l = {} | ||
| for j in range(len(fnames)): | ||
| l[fnames[j]] = i[j] | ||
| dictr.append(l) | ||
| # Checks for matching STRs in DNA and takes count | ||
| for c in range(1, len(fnames)): | ||
| dnal[fnames[c]] = 0 | ||
| for k in range(len(read2)): | ||
| count = 0 | ||
| while fnames[c] == read2[k:(k+len(fnames[c]))]: | ||
| count += 1 | ||
| k += len(fnames[c]) | ||
| if dnal[fnames[c]] < count: | ||
| dnal[fnames[c]] = count | ||
| file1.close() | ||
| with open(argv[1], 'r') as file1: | ||
| file2 = open(argv[2], 'r') | ||
| read1 = csv.DictReader(file1) | ||
| fnames = read1.fieldnames | ||
| read1 = csv.reader(file1) | ||
| read2 = file2.read() | ||
| dictr = [] | ||
| dnal = {} | ||
| # Creates a dictionary with csv data | ||
| for i in read1: | ||
| l = {fnames[j]: i[j] for j in range(len(fnames))} | ||
| dictr.append(l) | ||
| # Checks for matching STRs in DNA and takes count | ||
| for c in range(1, len(fnames)): | ||
| dnal[fnames[c]] = 0 | ||
| for k in range(len(read2)): | ||
| count = 0 | ||
| while fnames[c] == read2[k:(k+len(fnames[c]))]: | ||
| count += 1 | ||
| k += len(fnames[c]) | ||
| dnal[fnames[c]] = max(dnal[fnames[c]], count) |
There was a problem hiding this comment.
Lines 8-33 refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed) - Convert for loop into dictionary comprehension (
dict-comprehension) - Replace comparison with min/max call (
min-max-identity)
This removes the following comments ( why? ):
# Opens csv and dna files
|
|
||
| name = input("What is your name?:") | ||
| print("Hello " + name + " ! Welcome to Guess the Number game!") | ||
| print(f"Hello {name} ! Welcome to Guess the Number game!") |
There was a problem hiding this comment.
Lines 5-22 refactored with the following changes:
- Use f-string instead of string concatenation [×7] (
use-fstring-for-concatenation) - Remove unnecessary calls to
str()from formatted values in f-strings (remove-str-from-fstring)
|
|
||
| for i in range(height): | ||
| for j in range(height - i - 1): | ||
| for _ in range(height - i - 1): | ||
| print(" ", end="") | ||
| for j in range(i + 1): | ||
| for _ in range(i + 1): | ||
| print("#", end="") | ||
| print(" ", end="") | ||
| for j in range(i + 1): | ||
| for _ in range(i + 1): |
There was a problem hiding this comment.
Lines 7-14 refactored with the following changes:
- Replace unused for index with underscore [×3] (
for-index-underscore)
| try: | ||
| c = si.readline().decode('ascii') | ||
| if c: | ||
| print(c) | ||
| sleep(1) | ||
| try: | ||
| if c := si.readline().decode('ascii'): | ||
| print(c) | ||
| sleep(1) | ||
|
|
||
| except Exception as e: | ||
| print("Error: ", e) | ||
| sleep(5) | ||
| except Exception as e: | ||
| print("Error: ", e) | ||
| sleep(5) |
There was a problem hiding this comment.
Function main refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| if not num[i].isdecimal(): | ||
| return False | ||
| return True | ||
| return all(num[i].isdecimal() for i in range(8, 12)) |
There was a problem hiding this comment.
Function isPhoneNumber refactored with the following changes:
- Use any() instead of for loop (
use-any) - Invert any/all to simplify comparisons (
invert-any-all)
| new_index = last_block.index + 1 | ||
| new_timestamp = dt.datetime.now() | ||
| new_data = "This is block " + str(new_index) | ||
| new_data = f"This is block {str(new_index)}" |
There was a problem hiding this comment.
Function next_block refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| for i in range(num): | ||
| for _ in range(num): | ||
| block_to_add = next_block(prev_block) | ||
| myblockchain.append(block_to_add) | ||
| prev_block = block_to_add | ||
| print("Block #{} has been added to your blockchain".format(prev_block.index)) | ||
| print("Hash: {}\n".format(prev_block.hash)) | ||
| print(f"Block #{prev_block.index} has been added to your blockchain") | ||
| print(f"Hash: {prev_block.hash}\n") |
There was a problem hiding this comment.
Lines 35-40 refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore) - Replace call to format with f-string [×2] (
use-fstring-for-formatting)
| # Opens csv file in read mode | ||
| file = open(argv[1], 'r') | ||
| # Reads data in csv into a list of dictionaries | ||
| read = csv.DictReader(file) | ||
| # Sets up db connection | ||
| db = SQL("sqlite:///students.db") | ||
| # Stores data in dictionary into db | ||
| for row in read: | ||
| n = row['name'].split() | ||
| if len(n) == 2: | ||
| db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?,?,?,?,?)", | ||
| n[0], None, n[1], row['house'], row['birth']) | ||
| else: | ||
| db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?,?,?,?,?)", | ||
| n[0], n[1], n[2], row['house'], row['birth']) | ||
| file.close() | ||
| with open(argv[1], 'r') as file: | ||
| # Reads data in csv into a list of dictionaries | ||
| read = csv.DictReader(file) | ||
| # Sets up db connection | ||
| db = SQL("sqlite:///students.db") | ||
| # Stores data in dictionary into db | ||
| for row in read: | ||
| n = row['name'].split() | ||
| if len(n) == 2: | ||
| db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?,?,?,?,?)", | ||
| n[0], None, n[1], row['house'], row['birth']) | ||
| else: | ||
| db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?,?,?,?,?)", | ||
| n[0], n[1], n[2], row['house'], row['birth']) |
There was a problem hiding this comment.
Lines 8-23 refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed)
This removes the following comments ( why? ):
# Opens csv file in read mode
| # Displays required data | ||
| # Displays required data | ||
| for a in s: | ||
| if a['middle'] == None: | ||
| if a['middle'] is None: |
There was a problem hiding this comment.
Lines 11-13 refactored with the following changes:
- Use x is None rather than x == None (
none-compare)
Branch
masterrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run:Help us improve this pull request!