Skip to content

Sourcery refactored master branch#1

Open
sourcery-ai[bot] wants to merge 1 commit into
masterfrom
sourcery/master
Open

Sourcery refactored master branch#1
sourcery-ai[bot] wants to merge 1 commit into
masterfrom
sourcery/master

Conversation

@sourcery-ai

@sourcery-ai sourcery-ai Bot commented May 10, 2022

Copy link
Copy Markdown

Branch master refactored 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 master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai Bot requested a review from edson-github May 10, 2022 11:12
Comment on lines -41 to +43
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())

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function cleanup refactored with the following changes:

Comment thread code/ch02/ch02.py
self.cost_ = []

for i in range(self.n_iter):
for _ in range(self.n_iter):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AdalineGD.fit refactored with the following changes:

Comment thread code/ch02/ch02.py
Comment on lines -495 to +498
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)]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AdalineSGD.fit refactored with the following changes:

Comment thread code/ch02/ch02.py
Comment on lines -533 to +531
cost = 0.5 * error**2
return cost
return 0.5 * error**2

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AdalineSGD._update_weights refactored with the following changes:

Comment thread code/ch03/ch03.py
Comment on lines -337 to +343
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()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LogisticRegressionGD.fit refactored with the following changes:

+ ' '.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]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tokenizer refactored with the following changes:


results = c.fetchmany(batch_size)
while results:
while results := c.fetchmany(batch_size):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function update_model refactored with the following changes:

+ ' '.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]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tokenizer refactored with the following changes:

' '.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]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tokenizer refactored with the following changes:

' '.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]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tokenizer refactored with the following changes:

Comment thread code/ch10/ch10.py
self.cost_ = []

for i in range(self.n_iter):
for _ in range(self.n_iter):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LinearRegressionGD.fit refactored with the following changes:

Comment thread code/ch11/ch11.py
yticks.append((y_ax_lower + y_ax_upper) / 2.)
y_ax_lower += len(c_silhouette_vals)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 191-413 refactored with the following changes:

Comment thread code/ch12/ch12.py
else:
writemode = 'w'

writemode = 'wb' if (sys.version_info > (3, 0)) else 'w'

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 105-109 refactored with the following changes:

Comment thread code/ch12/ch12.py
Comment on lines -142 to +140
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')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function load_mnist refactored with the following changes:

Comment thread code/ch12/ch12.py
Comment on lines -353 to -354
cost = np.sum(term1 - term2) + L2_term

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function NeuralNetMLP._compute_cost refactored with the following changes:

Comment thread code/ch15/ch15.py
Comment on lines -103 to +107
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)
]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function conv1d refactored with the following changes:

Comment thread code/ch15/ch15.py
Comment on lines -191 to +194


Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 191-241 refactored with the following changes:

Comment thread code/ch15/ch15.py
Comment on lines -258 to +257
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')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function load_mnist refactored with the following changes:

Comment thread code/ch15/ch15.py
Comment on lines -515 to +509
print('Saving model in %s' % path)
print(f'Saving model in {path}')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function save refactored with the following changes:

Comment thread code/ch15/ch15.py
Comment on lines -521 to +515
print('Loading model from %s' % path)
print(f'Loading model from {path}')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function load refactored with the following changes:

Comment thread code/ch15/ch15.py
Comment on lines -924 to +918
print('Saving model in %s' % path)
print(f'Saving model in {path}')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ConvNN.save refactored with the following changes:

Comment thread code/ch15/ch15.py
Comment on lines -930 to +924
print('Loading model from %s' % path)
print(f'Loading model from {path}')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ConvNN.load refactored with the following changes:

Comment thread code/ch15/ch15.py
Comment on lines -949 to +943
for i, (batch_x,batch_y) in enumerate(batch_gen):
for batch_x, batch_y in batch_gen:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ConvNN.train refactored with the following changes:

Comment thread code/ch16/ch16.py
Comment on lines -294 to +302
[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)
]
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SentimentRNN.build refactored with the following changes:

Comment thread code/ch16/ch16.py
Comment on lines -501 to +509
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]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function reshape_data refactored with the following changes:

Comment thread code/ch16/ch16.py
Comment on lines -732 to +737
ch_id = np.random.choice(char_size, 1, p=p)[0]
return ch_id
return np.random.choice(char_size, 1, p=p)[0]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_top_char refactored with the following changes:

Comment on lines -19 to +32
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,
]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run_ipynb refactored with the following changes:

Comment on lines -83 to +88
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__))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestNotebooks.test_ch08 refactored with the following changes:

Comment on lines -91 to -97
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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestNotebooks.test_ch09 refactored with the following changes:

Comment on lines -130 to +132
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__))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestNotebooks.test_ch16 refactored with the following changes:

@sourcery-ai

sourcery-ai Bot commented May 10, 2022

Copy link
Copy Markdown
Author

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 0.12%.

Quality metrics Before After Change
Complexity 2.85 ⭐ 2.79 ⭐ -0.06 👍
Method Length 126.44 😞 126.21 😞 -0.23 👍
Working memory 14.39 😞 14.46 😞 0.07 👎
Quality 54.21% 🙂 54.09% 🙂 -0.12% 👎
Other metrics Before After Change
Lines 9664 9613 -51
Changed files Quality Before Quality After Quality Change
code/.convert_notebook_to_script.py 55.29% 🙂 57.71% 🙂 2.42% 👍
code/ch02/ch02.py 72.66% 🙂 72.61% 🙂 -0.05% 👎
code/ch03/ch03.py 60.74% 🙂 60.74% 🙂 0.00%
code/ch04/ch04.py 47.12% 😞 46.90% 😞 -0.22% 👎
code/ch05/ch05.py 47.51% 😞 47.42% 😞 -0.09% 👎
code/ch06/ch06.py 28.44% 😞 28.44% 😞 0.00%
code/ch07/ch07.py 41.45% 😞 41.56% 😞 0.11% 👍
code/ch08/ch08.py 56.68% 🙂 56.52% 🙂 -0.16% 👎
code/ch09/movieclassifier/vectorizer.py 85.12% ⭐ 85.75% ⭐ 0.63% 👍
code/ch09/movieclassifier_with_update/update.py 72.38% 🙂 73.70% 🙂 1.32% 👍
code/ch09/movieclassifier_with_update/vectorizer.py 85.12% ⭐ 85.75% ⭐ 0.63% 👍
code/ch09/pickle-test-scripts/pickle-dump-test.py 81.28% ⭐ 81.36% ⭐ 0.08% 👍
code/ch09/pickle-test-scripts/vectorizer.py 85.29% ⭐ 86.03% ⭐ 0.74% 👍
code/ch10/ch10.py 45.71% 😞 45.71% 😞 0.00%
code/ch11/ch11.py 29.82% 😞 29.82% 😞 0.00%
code/ch12/ch12.py 54.56% 🙂 53.99% 🙂 -0.57% 👎
code/ch12/neuralnet.py 52.17% 🙂 51.37% 🙂 -0.80% 👎
code/ch13/ch13.py 63.72% 🙂 63.64% 🙂 -0.08% 👎
code/ch14/ch14.py 45.65% 😞 45.65% 😞 0.00%
code/ch15/ch15.py 65.35% 🙂 65.48% 🙂 0.13% 👍
code/ch16/ch16.py 61.81% 🙂 61.90% 🙂 0.09% 👍
code/tests/test_notebooks.py 89.34% ⭐ 89.84% ⭐ 0.50% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
code/ch12/ch12.py NeuralNetMLP.fit 5 ⭐ 386 ⛔ 25 ⛔ 31.80% 😞 Try splitting into smaller methods. Extract out complex expressions
code/ch12/neuralnet.py NeuralNetMLP.fit 5 ⭐ 386 ⛔ 25 ⛔ 31.80% 😞 Try splitting into smaller methods. Extract out complex expressions
code/ch03/ch03.py plot_decision_regions 2 ⭐ 245 ⛔ 16 ⛔ 44.54% 😞 Try splitting into smaller methods. Extract out complex expressions
code/.convert_notebook_to_script.py cleanup 17 🙂 128 😞 12 😞 46.11% 😞 Try splitting into smaller methods. Extract out complex expressions
code/ch02/ch02.py plot_decision_regions 1 ⭐ 200 😞 15 😞 49.53% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants