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 12, 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 WildGenie May 12, 2022 17:39

@sourcery-ai sourcery-ai Bot left a comment

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.

Due to GitHub API limits, only the first 60 comments can be shown.

Comment thread agents.py

def __repr__(self):
return '<{}>'.format(getattr(self, '__name__', self.__class__.__name__))
return f"<{getattr(self, '__name__', self.__class__.__name__)}>"

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 Thing.__repr__ refactored with the following changes:

Comment thread agents.py

def program(percept):
return eval(input('Percept={}; action? '.format(percept)))
return eval(input(f'Percept={percept}; action? '))

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 Agent.__init__.program refactored with the following changes:

Comment thread agents.py
def new_program(percept):
action = old_program(percept)
print('{} perceives {} and does {}'.format(agent, percept, action))
print(f'{agent} perceives {percept} and does {action}')

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 TraceAgent.new_program refactored with the following changes:

Comment thread agents.py
percepts.append(percept)
action = table.get(tuple(percepts))
return action
return table.get(tuple(percepts))

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 TableDrivenAgentProgram.program refactored with the following changes:

Comment thread agents.py
Comment on lines -162 to +161
action = rule.action
return action
return rule.action

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 SimpleReflexAgentProgram.program refactored with the following changes:

Comment thread agents.py
Comment on lines -1002 to +1007
print("Death by {} [-1000].".format(explorer[0].killed_by))
print(f"Death by {explorer[0].killed_by} [-1000].")
else:
print("Explorer climbed out {}."
.format("with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"))
print(
f'Explorer climbed out {"with Gold [+1000]!" if Gold() not in self.things else "without Gold [+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 WumpusEnvironment.is_done refactored with the following changes:

Comment thread agents.py
Comment on lines -1027 to +1029
envs = [EnvFactory() for i in range(n)]
envs = [EnvFactory() for _ in range(n)]

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 compare_agents refactored with the following changes:

Comment thread agents4e.py

def __repr__(self):
return '<{}>'.format(getattr(self, '__name__', self.__class__.__name__))
return f"<{getattr(self, '__name__', self.__class__.__name__)}>"

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 Thing.__repr__ refactored with the following changes:

Comment thread agents4e.py

def program(percept):
return eval(input('Percept={}; action? '.format(percept)))
return eval(input(f'Percept={percept}; action? '))

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 Agent.__init__.program refactored with the following changes:

Comment thread agents4e.py
def new_program(percept):
action = old_program(percept)
print('{} perceives {} and does {}'.format(agent, percept, action))
print(f'{agent} perceives {percept} and does {action}')

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 TraceAgent.new_program refactored with the following changes:

Comment thread agents4e.py
percepts.append(percept)
action = table.get(tuple(percepts))
return action
return table.get(tuple(percepts))

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 TableDrivenAgentProgram.program refactored with the following changes:

Comment thread agents4e.py
Comment on lines -167 to +166
action = rule.action
return action
return rule.action

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 SimpleReflexAgentProgram.program refactored with the following changes:

Comment thread agents4e.py
Comment on lines -182 to +180
action = rule.action
return action
return rule.action

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 ModelBasedReflexAgentProgram.program refactored with the following changes:

Comment thread agents4e.py
Comment on lines -346 to +343
for step in range(steps):
for _ in range(steps):

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 Environment.run refactored with the following changes:

Comment thread agents4e.py
Comment on lines -386 to +384
print(" Thing to be removed: {} at {}".format(thing, thing.location))
print(" from list: {}".format([(thing, thing.location) for thing in self.things]))
print(f" Thing to be removed: {thing} at {thing.location}")
print(f" from list: {[(thing, thing.location) for thing in self.things]}")

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 Environment.delete_thing refactored with the following changes:

Comment thread csp.py
Comment on lines -144 to +147
return {v: self.curr_domains[v][0]
for v in self.variables if 1 == len(self.curr_domains[v])}
return {
v: self.curr_domains[v][0]
for v in self.variables
if len(self.curr_domains[v]) == 1
}

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 CSP.infer_assignment refactored with the following changes:

Comment thread csp.py
Comment on lines -320 to +323
if revised:
if not csp.curr_domains[Xi]:
return False, checks # CSP is inconsistent
if revised and not csp.curr_domains[Xi]:
return False, checks # CSP is inconsistent

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 AC4 refactored with the following changes:

Comment thread csp.py
var = select_unassigned_variable(assignment, csp)
for value in order_domain_values(var, assignment, csp):
if 0 == csp.nconflicts(var, value, assignment):
if csp.nconflicts(var, value, assignment) == 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 backtracking_search.backtrack refactored with the following changes:

Comment thread csp.py
csp.assign(var, val, current)
# Now repeatedly choose a random conflicted variable and change it
for i in range(max_steps):
for _ in range(max_steps):

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 min_conflicts refactored with the following changes:

Comment thread csp.py

def tree_csp_solver(csp):
"""[Figure 6.11]"""
assignment = {}

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 tree_csp_solver refactored with the following changes:

Comment thread csp.py
Comment on lines -524 to +523
keep = False # Keep or remove val1
for val2 in csp.domains[Xk]:
if csp.constraints(Xj, val1, Xk, val2):
# Found a consistent assignment for val1, keep it
keep = True
break

keep = any(csp.constraints(Xj, val1, Xk, val2) for val2 in csp.domains[Xk])

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 make_arc_consistent refactored with the following changes:

  • Use any() instead of for loop (use-any)

This removes the following comments ( why? ):

# Keep or remove val1
# Found a consistent assignment for val1, keep it

Comment thread csp.py
if csp.constraints(Xj, parent_assignment, Xk, val)
),
None,
)

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 assign_value refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)

This removes the following comments ( why? ):

# No consistent assignment available

Comment thread csp.py
Comment on lines -710 to +705
if assignment.get(var, '') == val:
ch = '*'
else:
ch = ' '
ch = '*' if assignment.get(var, '') == val else ' '

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 NQueensCSP.display refactored with the following changes:

Comment thread csp.py
Comment on lines -731 to +723
_BGRID = [[[[_CELL() for x in _R3] for y in _R3] for bx in _R3] for by in _R3]
_BGRID = [[[[_CELL() for _ in _R3] for _ in _R3] for _ in _R3] for _ in _R3]

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 731-731 refactored with the following changes:

Comment thread csp.py
Comment on lines -1026 to +1018
return SortedSet(to_do, key=lambda t: 1 / len([var for var in t[1].scope]))
return SortedSet(to_do, key=lambda t: 1 / len(list(t[1].scope)))

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 sat_up refactored with the following changes:

Comment thread games.py
print("current state:")
game.display(state)
print("available moves: {}".format(game.actions(state)))
print(f"available moves: {game.actions(state)}")

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 query_player refactored with the following changes:

Comment thread games.py

def __repr__(self):
return '<{}>'.format(self.__class__.__name__)
return f'<{self.__class__.__name__}>'

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 Game.__repr__ refactored with the following changes:

Comment thread games.py
return self.utils[state]
else:
return -self.utils[state]
return self.utils[state] if player == 'MAX' else -self.utils[state]

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 Fig52Game.utility refactored with the following changes:

Comment thread games.py
Comment on lines -339 to +336
if player == 'MAX':
return self.utils[state]
else:
return -self.utils[state]
return self.utils[state] if player == 'MAX' else -self.utils[state]

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 Fig52Extended.utility refactored with the following changes:

Comment thread games.py
Comment on lines -448 to +442
board = [point.copy() for index in range(24)]
board = [point.copy() for _ in range(24)]

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 Backgammon.__init__ refactored with the following changes:

@sourcery-ai

sourcery-ai Bot commented May 12, 2022

Copy link
Copy Markdown
Author

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.33%.

Quality metrics Before After Change
Complexity 7.51 ⭐ 6.93 ⭐ -0.58 👍
Method Length 54.60 ⭐ 54.30 ⭐ -0.30 👍
Working memory 7.69 🙂 7.70 🙂 0.01 👎
Quality 69.66% 🙂 69.99% 🙂 0.33% 👍
Other metrics Before After Change
Lines 28989 29030 41
Changed files Quality Before Quality After Quality Change
agents.py 81.50% ⭐ 81.35% ⭐ -0.15% 👎
agents4e.py 80.25% ⭐ 80.09% ⭐ -0.16% 👎
csp.py 56.96% 🙂 57.51% 🙂 0.55% 👍
deep_learning4e.py 70.21% 🙂 70.19% 🙂 -0.02% 👎
games.py 74.10% 🙂 74.29% 🙂 0.19% 👍
games4e.py 73.01% 🙂 73.23% 🙂 0.22% 👍
ipyviews.py 86.95% ⭐ 87.54% ⭐ 0.59% 👍
knowledge.py 73.33% 🙂 73.27% 🙂 -0.06% 👎
learning.py 67.96% 🙂 67.90% 🙂 -0.06% 👎
learning4e.py 73.66% 🙂 73.59% 🙂 -0.07% 👎
logic.py 57.87% 🙂 59.02% 🙂 1.15% 👍
logic4e.py 57.11% 🙂 58.73% 🙂 1.62% 👍
making_simple_decision4e.py 79.36% ⭐ 78.87% ⭐ -0.49% 👎
mdp.py 67.81% 🙂 67.59% 🙂 -0.22% 👎
mdp4e.py 68.28% 🙂 67.83% 🙂 -0.45% 👎
nlp.py 77.12% ⭐ 77.33% ⭐ 0.21% 👍
nlp4e.py 73.49% 🙂 74.49% 🙂 1.00% 👍
notebook.py 54.89% 🙂 55.10% 🙂 0.21% 👍
notebook4e.py 54.77% 🙂 54.98% 🙂 0.21% 👍
perception4e.py 69.10% 🙂 69.60% 🙂 0.50% 👍
planning.py 60.62% 🙂 61.12% 🙂 0.50% 👍
probability.py 78.55% ⭐ 78.42% ⭐ -0.13% 👎
probability4e.py 82.68% ⭐ 82.63% ⭐ -0.05% 👎
reinforcement_learning.py 71.93% 🙂 71.67% 🙂 -0.26% 👎
reinforcement_learning4e.py 71.93% 🙂 71.67% 🙂 -0.26% 👎
search.py 71.34% 🙂 71.65% 🙂 0.31% 👍
text.py 85.46% ⭐ 85.38% ⭐ -0.08% 👎
utils.py 92.20% ⭐ 92.33% ⭐ 0.13% 👍
utils4e.py 91.15% ⭐ 91.27% ⭐ 0.12% 👍
gui/eight_puzzle.py 69.50% 🙂 69.38% 🙂 -0.12% 👎
gui/genetic_algorithm_example.py 66.01% 🙂 65.31% 🙂 -0.70% 👎
gui/grid_mdp.py 59.67% 🙂 59.70% 🙂 0.03% 👍
gui/romania_problem.py 58.04% 🙂 58.04% 🙂 0.00%
gui/tic-tac-toe.py 64.60% 🙂 65.82% 🙂 1.22% 👍
gui/tsp.py 59.64% 🙂 59.52% 🙂 -0.12% 👎
gui/vacuum_agent.py 76.50% ⭐ 77.15% ⭐ 0.65% 👍
gui/xy_vacuum_environment.py 66.09% 🙂 67.41% 🙂 1.32% 👍
tests/test_agents.py 78.64% ⭐ 78.62% ⭐ -0.02% 👎
tests/test_agents4e.py 78.34% ⭐ 78.32% ⭐ -0.02% 👎
tests/test_csp.py 69.97% 🙂 70.46% 🙂 0.49% 👍
tests/test_games.py 82.30% ⭐ 83.40% ⭐ 1.10% 👍
tests/test_games4e.py 81.60% ⭐ 82.40% ⭐ 0.80% 👍
tests/test_knowledge.py 77.76% ⭐ 78.14% ⭐ 0.38% 👍
tests/test_logic.py 84.94% ⭐ 84.85% ⭐ -0.09% 👎
tests/test_logic4e.py 86.34% ⭐ 86.28% ⭐ -0.06% 👎
tests/test_mdp.py 71.39% 🙂 72.65% 🙂 1.26% 👍
tests/test_mdp4e.py 70.36% 🙂 71.47% 🙂 1.11% 👍
tests/test_nlp.py 85.06% ⭐ 85.52% ⭐ 0.46% 👍
tests/test_nlp4e.py 88.34% ⭐ 89.27% ⭐ 0.93% 👍
tests/test_planning.py 79.57% ⭐ 79.58% ⭐ 0.01% 👍
tests/test_probability.py 75.14% ⭐ 75.14% ⭐ 0.00%
tests/test_probability4e.py 82.52% ⭐ 82.52% ⭐ 0.00%
tests/test_reinforcement_learning.py 87.25% ⭐ 87.25% ⭐ 0.00%
tests/test_reinforcement_learning4e.py 87.25% ⭐ 87.25% ⭐ 0.00%
tests/test_search.py 77.65% ⭐ 78.14% ⭐ 0.49% 👍
tests/test_text.py 69.85% 🙂 69.85% 🙂 0.00%
tests/test_utils.py 87.64% ⭐ 87.73% ⭐ 0.09% 👍

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

File Function Complexity Length Working Memory Quality Recommendation
learning.py BackPropagationLearner 43 ⛔ 552 ⛔ 24 ⛔ 7.23% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
planning.py PlanningProblem.expand_actions 74 ⛔ 283 ⛔ 16 ⛔ 12.26% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
csp.py Kakuro.__init__ 74 ⛔ 297 ⛔ 15 😞 12.88% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
notebook.py Canvas_alpha_beta.draw_graph 30 😞 417 ⛔ 16 ⛔ 17.98% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
notebook4e.py Canvas_alpha_beta.draw_graph 30 😞 417 ⛔ 16 ⛔ 17.98% ⛔ Refactor to reduce nesting. 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