Sourcery refactored master branch#1
Conversation
| else: | ||
| if line.strip() not in existing_imports: | ||
| imports.append(line) | ||
| existing_imports.add(line.strip()) | ||
| elif line.strip() not in existing_imports: | ||
| imports.append(line) | ||
| existing_imports.add(line.strip()) |
There was a problem hiding this comment.
Function cleanup refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
| self.cost_ = [] | ||
|
|
||
| for i in range(self.n_iter): | ||
| for _ in range(self.n_iter): |
There was a problem hiding this comment.
Function AdalineGD.fit refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| for i in range(self.n_iter): | ||
| for _ in range(self.n_iter): | ||
| if self.shuffle: | ||
| X, y = self._shuffle(X, y) | ||
| cost = [] | ||
| for xi, target in zip(X, y): | ||
| cost.append(self._update_weights(xi, target)) | ||
| cost = [self._update_weights(xi, target) for xi, target in zip(X, y)] |
There was a problem hiding this comment.
Function AdalineSGD.fit refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore) - Convert for loop into list comprehension (
list-comprehension)
| cost = 0.5 * error**2 | ||
| return cost | ||
| return 0.5 * error**2 |
There was a problem hiding this comment.
Function AdalineSGD._update_weights refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| for i in range(self.n_iter): | ||
| for _ in range(self.n_iter): | ||
| net_input = self.net_input(X) | ||
| output = self.activation(net_input) | ||
| errors = (y - output) | ||
| self.w_[1:] += self.eta * X.T.dot(errors) | ||
| self.w_[0] += self.eta * errors.sum() | ||
|
|
There was a problem hiding this comment.
Function LogisticRegressionGD.fit refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| + ' '.join(emoticons).replace('-', '') | ||
| tokenized = [w for w in text.split() if w not in stop] | ||
| return tokenized | ||
| return [w for w in text.split() if w not in stop] |
There was a problem hiding this comment.
Function tokenizer refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
|
|
||
| results = c.fetchmany(batch_size) | ||
| while results: | ||
| while results := c.fetchmany(batch_size): |
There was a problem hiding this comment.
Function update_model refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| + ' '.join(emoticons).replace('-', '') | ||
| tokenized = [w for w in text.split() if w not in stop] | ||
| return tokenized | ||
| return [w for w in text.split() if w not in stop] |
There was a problem hiding this comment.
Function tokenizer refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| ' '.join(emoticons).replace('-', '') | ||
| tokenized = [w for w in text.split() if w not in stop] | ||
| return tokenized | ||
| return [w for w in text.split() if w not in stop] |
There was a problem hiding this comment.
Function tokenizer refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| ' '.join(emoticons).replace('-', '') | ||
| tokenized = [w for w in text.split() if w not in stop] | ||
| return tokenized | ||
| return [w for w in text.split() if w not in stop] |
There was a problem hiding this comment.
Function tokenizer refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| self.cost_ = [] | ||
|
|
||
| for i in range(self.n_iter): | ||
| for _ in range(self.n_iter): |
There was a problem hiding this comment.
Function LinearRegressionGD.fit refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| yticks.append((y_ax_lower + y_ax_upper) / 2.) | ||
| y_ax_lower += len(c_silhouette_vals) | ||
|
|
There was a problem hiding this comment.
Lines 191-413 refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| else: | ||
| writemode = 'w' | ||
|
|
||
| writemode = 'wb' if (sys.version_info > (3, 0)) else 'w' |
There was a problem hiding this comment.
Lines 105-109 refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| labels_path = os.path.join(path, | ||
| '%s-labels-idx1-ubyte' % kind) | ||
| images_path = os.path.join(path, | ||
| '%s-images-idx3-ubyte' % kind) | ||
|
|
||
| labels_path = os.path.join(path, f'{kind}-labels-idx1-ubyte') | ||
| images_path = os.path.join(path, f'{kind}-images-idx3-ubyte') | ||
|
|
There was a problem hiding this comment.
Function load_mnist refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| cost = np.sum(term1 - term2) + L2_term | ||
|
|
There was a problem hiding this comment.
Function NeuralNetMLP._compute_cost refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| res = [] | ||
| for i in range(0, int(len(x)/s),s): | ||
| res.append(np.sum(x_padded[i:i+w_rot.shape[0]] * w_rot)) | ||
| res = [ | ||
| np.sum(x_padded[i : i + w_rot.shape[0]] * w_rot) | ||
| for i in range(0, int(len(x) / s), s) | ||
| ] | ||
|
|
There was a problem hiding this comment.
Function conv1d refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
|
|
||
|
|
There was a problem hiding this comment.
Lines 191-241 refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| labels_path = os.path.join(path, | ||
| '%s-labels-idx1-ubyte' | ||
| % kind) | ||
| images_path = os.path.join(path, | ||
| '%s-images-idx3-ubyte' | ||
| % kind) | ||
| labels_path = os.path.join(path, f'{kind}-labels-idx1-ubyte') | ||
| images_path = os.path.join(path, f'{kind}-images-idx3-ubyte') |
There was a problem hiding this comment.
Function load_mnist refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| print('Saving model in %s' % path) | ||
| print(f'Saving model in {path}') |
There was a problem hiding this comment.
Function save refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| print('Loading model from %s' % path) | ||
| print(f'Loading model from {path}') |
There was a problem hiding this comment.
Function load refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| print('Saving model in %s' % path) | ||
| print(f'Saving model in {path}') |
There was a problem hiding this comment.
Function ConvNN.save refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| print('Loading model from %s' % path) | ||
| print(f'Loading model from {path}') |
There was a problem hiding this comment.
Function ConvNN.load refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| for i, (batch_x,batch_y) in enumerate(batch_gen): | ||
| for batch_x, batch_y in batch_gen: |
There was a problem hiding this comment.
Function ConvNN.train refactored with the following changes:
- Remove unnecessary calls to
enumeratewhen the index is not used (remove-unused-enumerate)
| [tf.contrib.rnn.DropoutWrapper( | ||
| tf.contrib.rnn.BasicLSTMCell(self.lstm_size), | ||
| output_keep_prob=tf_keepprob) | ||
| for i in range(self.num_layers)]) | ||
| [ | ||
| tf.contrib.rnn.DropoutWrapper( | ||
| tf.contrib.rnn.BasicLSTMCell(self.lstm_size), | ||
| output_keep_prob=tf_keepprob, | ||
| ) | ||
| for _ in range(self.num_layers) | ||
| ] | ||
| ) | ||
|
|
There was a problem hiding this comment.
Function SentimentRNN.build refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| num_batches = num_batches - 1 | ||
| num_batches -= 1 | ||
| ## Truncate the sequence at the end to get rid of | ||
| ## remaining charcaters that do not make a full batch | ||
| x = sequence[0 : num_batches*mini_batch_length] | ||
| x = sequence[:num_batches*mini_batch_length] |
There was a problem hiding this comment.
Function reshape_data refactored with the following changes:
- Replace assignment with augmented assignment (
aug-assign) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index)
| ch_id = np.random.choice(char_size, 1, p=p)[0] | ||
| return ch_id | ||
| return np.random.choice(char_size, 1, p=p)[0] |
There was a problem hiding this comment.
Function get_top_char refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if (sys.version_info >= (3, 0)): | ||
| kernel_name = 'python3' | ||
| else: | ||
| kernel_name = 'python2' | ||
| kernel_name = 'python3' if (sys.version_info >= (3, 0)) else 'python2' | ||
| # error_cells = [] | ||
|
|
||
| args = ["jupyter", "nbconvert", | ||
| "--execute", "--inplace", | ||
| "--debug", | ||
| "--ExecutePreprocessor.timeout=5000", | ||
| "--ExecutePreprocessor.kernel_name=%s" % kernel_name, | ||
| nb_path] | ||
| args = [ | ||
| "jupyter", | ||
| "nbconvert", | ||
| "--execute", | ||
| "--inplace", | ||
| "--debug", | ||
| "--ExecutePreprocessor.timeout=5000", | ||
| f"--ExecutePreprocessor.kernel_name={kernel_name}", | ||
| nb_path, | ||
| ] | ||
|
|
There was a problem hiding this comment.
Function run_ipynb refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| this_dir = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
| # run only on Py3, because of Py2 unicode handling | ||
| if (sys.version_info >= (3, 0)): | ||
| this_dir = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
There was a problem hiding this comment.
Function TestNotebooks.test_ch08 refactored with the following changes:
- Move assignments closer to their usage (
move-assign)
| this_dir = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
| # run only on Py3, because of the Py3 specific pickle files | ||
| if (sys.version_info >= (3, 0)): | ||
| this_dir = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
| run_ipynb(os.path.join(this_dir, '../ch09/ch09.ipynb')) | ||
| else: | ||
| pass |
There was a problem hiding this comment.
Function TestNotebooks.test_ch09 refactored with the following changes:
- Move assignments closer to their usage (
move-assign) - Remove redundant pass statement (
remove-redundant-pass)
| this_dir = os.path.dirname(os.path.abspath(__file__)) | ||
| # run only on Py3, because of Py2 unicode handling | ||
| if (sys.version_info >= (3, 0)): | ||
| this_dir = os.path.dirname(os.path.abspath(__file__)) |
There was a problem hiding this comment.
Function TestNotebooks.test_ch16 refactored with the following changes:
- Move assignments closer to their usage (
move-assign)
Sourcery Code Quality Report❌ Merging this PR will decrease code quality in the affected files by 0.12%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
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!