Skip to content

Conversation

@sourcery-ai
Copy link

@sourcery-ai sourcery-ai bot commented Nov 4, 2022

Pull Request #2 refactored by Sourcery.

Since the original Pull Request was opened as a fork in a contributor's
repository, we are unable to create a Pull Request branching from it.

To incorporate these changes, you can either:

  1. Merge this Pull Request instead of the original, or

  2. Ask your contributor to locally incorporate these commits and push them to
    the original Pull Request

    Incorporate changes via command line
    git fetch https://github.com/Hammer2900/aima-python pull/2/head
    git merge --ff-only FETCH_HEAD
    git push

NOTE: As code is pushed to the original Pull Request, Sourcery will
re-run and update (force-push) this Pull Request with new refactorings as
necessary. If Sourcery finds no refactorings at any point, this Pull Request
will be closed automatically.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from Hammer2900 November 4, 2022 16:20
Copy link
Author

@sourcery-ai sourcery-ai bot left a comment

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.


def __repr__(self):
return '<{}>'.format(getattr(self, '__name__', self.__class__.__name__))
return f"<{getattr(self, '__name__', self.__class__.__name__)}>"
Copy link
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:

def run(self, steps=1000):
"""Run the Environment for given number of time steps."""
for step in range(steps):
for _ in range(steps):
Copy link
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 on lines -381 to +382
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
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 on lines -509 to +513
things = [thing for thing in self.list_things_at(agent.location) if agent.can_grab(thing)]
if things:
if things := [
thing
for thing in self.list_things_at(agent.location)
if agent.can_grab(thing)
]:
Copy link
Author

Choose a reason for hiding this comment

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

Function XYEnvironment.execute_action refactored with the following changes:

Comment on lines -555 to +563
return not (x < self.x_start or x > self.x_end or y < self.y_start or y > self.y_end)
return (
x >= self.x_start
and x <= self.x_end
and y >= self.y_start
and y <= self.y_end
)
Copy link
Author

Choose a reason for hiding this comment

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

Function XYEnvironment.is_inbounds refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

Comment on lines -557 to +562
return not (x < self.x_start or x > self.x_end or y < self.y_start or y > self.y_end)
return (
x >= self.x_start
and x <= self.x_end
and y >= self.y_start
and y <= self.y_end
)
Copy link
Author

Choose a reason for hiding this comment

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

Function XYEnvironment.is_inbounds refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

Comment on lines -642 to +647
row = []
for y in range(y_start, y_end):
row.append(self.list_things_at((x, y)))
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
Copy link
Author

Choose a reason for hiding this comment

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

Function GraphicEnvironment.get_world refactored with the following changes:

Comment on lines -668 to +671
for step in range(steps):
for _ in range(steps):
Copy link
Author

Choose a reason for hiding this comment

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

Function GraphicEnvironment.run refactored with the following changes:

Comment on lines -693 to +697
for x in range(0, len(world)):
for y in range(0, len(world[x])):
for x in range(len(world)):
for y in range(len(world[x])):
Copy link
Author

Choose a reason for hiding this comment

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

Function GraphicEnvironment.draw_world refactored with the following changes:

Comment on lines -916 to +919
row = []
for y in range(y_start, y_end):
row.append(self.list_things_at((x, y)))
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
Copy link
Author

Choose a reason for hiding this comment

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

Function WumpusEnvironment.get_world refactored with the following changes:

Comment on lines -946 to +947
result = []
result.append(self.percepts_from(agent, (x - 1, y)))
result = [self.percepts_from(agent, (x - 1, y))]
Copy link
Author

Choose a reason for hiding this comment

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

Function WumpusEnvironment.percept refactored with the following changes:

Comment on lines -983 to +984
if len(things):
agent.holding.append(things[0])
if len(things):
agent.holding.append(things[0])
Copy link
Author

Choose a reason for hiding this comment

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

Function WumpusEnvironment.execute_action refactored with the following changes:

Comment on lines -1021 to +1026
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
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 on lines -1046 to +1048
envs = [EnvFactory() for i in range(n)]
envs = [EnvFactory() for _ in range(n)]
Copy link
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 on lines -98 to +101
else:
assignment = dict(state)
var = first([v for v in self.variables if v not in assignment])
return [(var, val) for val in self.domains[var]
if self.nconflicts(var, val, assignment) == 0]
assignment = dict(state)
var = first([v for v in self.variables if v not in assignment])
return [(var, val) for val in self.domains[var]
if self.nconflicts(var, val, assignment) == 0]
Copy link
Author

Choose a reason for hiding this comment

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

Function CSP.actions refactored with the following changes:

Comment on lines -997 to +989
sumv.__name__ = str(n) + "==sum"
sumv.__name__ = f"{str(n)}==sum"
Copy link
Author

Choose a reason for hiding this comment

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

Function sum_constraint refactored with the following changes:

Comment on lines -1007 to +999
isv.__name__ = str(val) + "=="
isv.__name__ = f"{str(val)}=="
Copy link
Author

Choose a reason for hiding this comment

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

Function is_constraint refactored with the following changes:

Comment on lines -1017 to +1009
nev.__name__ = str(val) + "!="
nev.__name__ = f"{str(val)}!="
Copy link
Author

Choose a reason for hiding this comment

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

Function ne_constraint refactored with the following changes:

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
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 on lines -1058 to +1050
if len(other_vars) == 0:
if not other_vars:
Copy link
Author

Choose a reason for hiding this comment

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

Function ACSolver.GAC refactored with the following changes:

Comment on lines -1110 to +1109
else:
var = other_vars[ind]
for val in domains[var]:
# env = dict_union(env, {var:val}) # no side effects
env[var] = val
holds, checks = self.any_holds(domains, const, env, other_vars, ind + 1, checks)
if holds:
return True, checks
return False, checks
var = other_vars[ind]
for val in domains[var]:
# env = dict_union(env, {var:val}) # no side effects
env[var] = val
holds, checks = self.any_holds(domains, const, env, other_vars, ind + 1, checks)
if holds:
return True, checks
return False, checks
Copy link
Author

Choose a reason for hiding this comment

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

Function ACSolver.any_holds refactored with the following changes:

Comment on lines -1133 to +1132
var = first(x for x in self.csp.variables if len(new_domains[x]) > 1)
if var:
if var := first(
x for x in self.csp.variables if len(new_domains[x]) > 1
):
dom1, dom2 = partition_domain(new_domains[var])
new_doms1 = extend(new_domains, var, dom1)
new_doms2 = extend(new_domains, var, dom2)
to_do = self.new_to_do(var, None)
return self.domain_splitting(new_doms1, to_do, arc_heuristic) or \
self.domain_splitting(new_doms2, to_do, arc_heuristic)
self.domain_splitting(new_doms2, to_do, arc_heuristic)
Copy link
Author

Choose a reason for hiding this comment

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

Function ACSolver.domain_splitting refactored with the following changes:

Comment on lines -1236 to +1228
var = "p" + str(j) + str(i)
var = f"p{str(j)}{str(i)}"
Copy link
Author

Choose a reason for hiding this comment

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

Function Crossword.__init__ refactored with the following changes:

Comment on lines +1258 to +1265
elif assignment is None:
puzzle += "[_] "
else:
var = "p" + str(j) + str(i)
if assignment is not None:
if isinstance(assignment[var], set) and len(assignment[var]) == 1:
puzzle += "[" + str(first(assignment[var])).upper() + "] "
elif isinstance(assignment[var], str):
puzzle += "[" + str(assignment[var]).upper() + "] "
else:
puzzle += "[_] "
var = f"p{str(j)}{str(i)}"
if isinstance(assignment[var], set) and len(assignment[var]) == 1:
puzzle += f"[{str(first(assignment[var])).upper()}] "
elif isinstance(assignment[var], str):
puzzle += f"[{str(assignment[var]).upper()}] "
Copy link
Author

Choose a reason for hiding this comment

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

Function Crossword.display refactored with the following changes:

for i, line in enumerate(puzzle):
for j, element in enumerate(line):
if element != '_' and element != '*':
if element not in ['_', '*']:
Copy link
Author

Choose a reason for hiding this comment

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

Function Kakuro.__init__ refactored with the following changes:

Comment on lines -519 to +517
for idx in range(0, 24):
if board[idx][player] > 0:
return 0
return util[player]
return next((0 for idx in range(24) if board[idx][player] > 0), util[player])
Copy link
Author

Choose a reason for hiding this comment

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

Function Backgammon.compute_utility refactored with the following changes:

Comment on lines -526 to +521
sum_range = range(0, 7) if player == 'W' else range(17, 24)
sum_range = range(7) if player == 'W' else range(17, 24)
Copy link
Author

Choose a reason for hiding this comment

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

Function Backgammon.checkers_at_home refactored with the following changes:

Comment on lines -538 to +548
dest_range = range(0, 24)
dest_range = range(24)
move1_legal = move2_legal = False
if dest1 in dest_range:
if self.is_point_open(player, board[dest1]):
self.move_checker(board, start[0], steps[0], player)
move1_legal = True
else:
if self.allow_bear_off[player]:
self.move_checker(board, start[0], steps[0], player)
move1_legal = True
elif self.allow_bear_off[player]:
self.move_checker(board, start[0], steps[0], player)
move1_legal = True
if not move1_legal:
return False
if dest2 in dest_range:
if self.is_point_open(player, board[dest2]):
move2_legal = True
else:
if self.allow_bear_off[player]:
move2_legal = True
elif self.allow_bear_off[player]:
move2_legal = True
Copy link
Author

Choose a reason for hiding this comment

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

Function Backgammon.is_legal_move refactored with the following changes:

Comment on lines -561 to +554
dest_range = range(0, 24)
dest_range = range(24)
Copy link
Author

Choose a reason for hiding this comment

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

Function Backgammon.move_checker refactored with the following changes:

Comment on lines -577 to +570
dice_rolls = list(itertools.combinations_with_replacement([1, 2, 3, 4, 5, 6], 2))
return dice_rolls
return list(itertools.combinations_with_replacement([1, 2, 3, 4, 5, 6], 2))
Copy link
Author

Choose a reason for hiding this comment

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

Function Backgammon.chances refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Nov 4, 2022

Sourcery Code Quality Report

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

Quality metrics Before After Change
Complexity 7.68 ⭐ 7.15 ⭐ -0.53 👍
Method Length 65.38 🙂 65.05 🙂 -0.33 👍
Working memory 6.39 🙂 6.38 🙂 -0.01 👍
Quality 67.66% 🙂 67.93% 🙂 0.27% 👍
Other metrics Before After Change
Lines 27534 27648 114
Changed files Quality Before Quality After Quality Change
agents.py 79.03% ⭐ 79.03% ⭐ 0.00%
agents4e.py 77.64% ⭐ 77.70% ⭐ 0.06% 👍
csp.py 54.60% 🙂 55.17% 🙂 0.57% 👍
deep_learning4e.py 69.05% 🙂 69.04% 🙂 -0.01% 👎
games.py 68.78% 🙂 68.61% 🙂 -0.17% 👎
games4e.py 66.16% 🙂 66.19% 🙂 0.03% 👍
ipyviews.py 85.19% ⭐ 85.74% ⭐ 0.55% 👍
knowledge.py 72.05% 🙂 72.06% 🙂 0.01% 👍
learning.py 63.55% 🙂 63.45% 🙂 -0.10% 👎
learning4e.py 72.27% 🙂 72.19% 🙂 -0.08% 👎
logic.py 55.18% 🙂 55.77% 🙂 0.59% 👍
logic4e.py 54.90% 🙂 55.73% 🙂 0.83% 👍
making_simple_decision4e.py 79.83% ⭐ 79.55% ⭐ -0.28% 👎
mdp.py 66.42% 🙂 66.17% 🙂 -0.25% 👎
mdp4e.py 66.87% 🙂 66.41% 🙂 -0.46% 👎
nlp.py 76.62% ⭐ 76.87% ⭐ 0.25% 👍
nlp4e.py 72.25% 🙂 72.65% 🙂 0.40% 👍
notebook.py 48.87% 😞 48.91% 😞 0.04% 👍
notebook4e.py 48.78% 😞 48.82% 😞 0.04% 👍
perception4e.py 67.75% 🙂 68.26% 🙂 0.51% 👍
planning.py 58.74% 🙂 59.48% 🙂 0.74% 👍
probabilistic_learning.py 82.73% ⭐ 82.88% ⭐ 0.15% 👍
probability.py 77.51% ⭐ 77.43% ⭐ -0.08% 👎
probability4e.py 81.25% ⭐ 81.17% ⭐ -0.08% 👎
reinforcement_learning.py 69.07% 🙂 68.77% 🙂 -0.30% 👎
reinforcement_learning4e.py 69.07% 🙂 68.77% 🙂 -0.30% 👎
search.py 69.75% 🙂 70.28% 🙂 0.53% 👍
text.py 84.16% ⭐ 84.08% ⭐ -0.08% 👎
utils.py 91.28% ⭐ 91.30% ⭐ 0.02% 👍
utils4e.py 90.14% ⭐ 90.16% ⭐ 0.02% 👍
gui/eight_puzzle.py 69.30% 🙂 68.65% 🙂 -0.65% 👎
gui/genetic_algorithm_example.py 67.94% 🙂 67.88% 🙂 -0.06% 👎
gui/grid_mdp.py 59.39% 🙂 59.43% 🙂 0.04% 👍
gui/romania_problem.py 56.17% 🙂 56.17% 🙂 0.00%
gui/tic-tac-toe.py 63.14% 🙂 64.43% 🙂 1.29% 👍
gui/tsp.py 55.97% 🙂 55.61% 🙂 -0.36% 👎
gui/vacuum_agent.py 76.59% ⭐ 77.35% ⭐ 0.76% 👍
gui/xy_vacuum_environment.py 65.93% 🙂 67.46% 🙂 1.53% 👍
tests/test_agents.py 73.77% 🙂 73.99% 🙂 0.22% 👍
tests/test_agents4e.py 73.66% 🙂 73.88% 🙂 0.22% 👍
tests/test_csp.py 68.91% 🙂 69.39% 🙂 0.48% 👍
tests/test_games.py 83.09% ⭐ 84.32% ⭐ 1.23% 👍
tests/test_games4e.py 82.96% ⭐ 83.89% ⭐ 0.93% 👍
tests/test_knowledge.py 77.26% ⭐ 77.64% ⭐ 0.38% 👍
tests/test_logic.py 84.09% ⭐ 84.16% ⭐ 0.07% 👍
tests/test_logic4e.py 85.46% ⭐ 85.53% ⭐ 0.07% 👍
tests/test_mdp.py 71.44% 🙂 72.74% 🙂 1.30% 👍
tests/test_mdp4e.py 70.37% 🙂 71.51% 🙂 1.14% 👍
tests/test_nlp.py 84.86% ⭐ 85.35% ⭐ 0.49% 👍
tests/test_nlp4e.py 88.98% ⭐ 89.99% ⭐ 1.01% 👍
tests/test_planning.py 78.71% ⭐ 78.72% ⭐ 0.01% 👍
tests/test_probability.py 74.67% 🙂 74.67% 🙂 0.00%
tests/test_probability4e.py 83.34% ⭐ 83.34% ⭐ 0.00%
tests/test_reinforcement_learning.py 88.05% ⭐ 88.05% ⭐ 0.00%
tests/test_reinforcement_learning4e.py 88.05% ⭐ 88.05% ⭐ 0.00%
tests/test_search.py 74.71% 🙂 75.56% ⭐ 0.85% 👍
tests/test_text.py 67.48% 🙂 67.47% 🙂 -0.01% 👎
tests/test_utils.py 87.29% ⭐ 87.38% ⭐ 0.09% 👍

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

File Function Complexity Length Working Memory Quality Recommendation
csp.py Zebra 68 ⛔ 342 ⛔ 5.15% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
learning.py BackPropagationLearner 43 ⛔ 593 ⛔ 24 ⛔ 7.13% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
logic.py unit_propagation 48 ⛔ 331 ⛔ 9.19% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
planning.py PlanningProblem.expand_actions 74 ⛔ 325 ⛔ 16 ⛔ 10.92% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
logic.py SAT_plan 37 ⛔ 405 ⛔ 11.69% ⛔ Refactor to reduce nesting. Try splitting into smaller methods

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.

1 participant