-
Notifications
You must be signed in to change notification settings - Fork 0
last update (Sourcery refactored) #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,7 +50,7 @@ class Thing: | |
| .__name__ slot (used for output only).""" | ||
|
|
||
| def __repr__(self): | ||
| return '<{}>'.format(getattr(self, '__name__', self.__class__.__name__)) | ||
| return f"<{getattr(self, '__name__', self.__class__.__name__)}>" | ||
|
|
||
| def is_alive(self): | ||
| """Things that are 'alive' should return true.""" | ||
|
|
@@ -338,7 +338,7 @@ def step(self): | |
|
|
||
| def run(self, steps=1000): | ||
| """Run the Environment for given number of time steps.""" | ||
| for step in range(steps): | ||
| for _ in range(steps): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if self.is_done(): | ||
| return | ||
| self.step() | ||
|
|
@@ -378,8 +378,8 @@ def delete_thing(self, thing): | |
| except ValueError as e: | ||
| print(e) | ||
| print(" in Environment delete_thing") | ||
| 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]}") | ||
|
Comment on lines
-381
to
+382
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if thing in self.agents: | ||
| self.agents.remove(thing) | ||
|
|
||
|
|
@@ -506,8 +506,11 @@ def execute_action(self, agent, action): | |
| elif action == 'Forward': | ||
| agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location)) | ||
| elif action == 'Grab': | ||
| 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) | ||
| ]: | ||
|
Comment on lines
-509
to
+513
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| agent.holding.append(things[0]) | ||
| print("Grabbing ", things[0].__class__.__name__) | ||
| self.delete_thing(things[0]) | ||
|
|
@@ -552,7 +555,12 @@ def add_thing(self, thing, location=None, exclude_duplicate_class_items=False): | |
| def is_inbounds(self, location): | ||
| """Checks to make sure that the location is inbounds (within walls if we have walls)""" | ||
| x, y = location | ||
| 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 | ||
| ) | ||
|
Comment on lines
-555
to
+563
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def random_location_inbounds(self, exclude=None): | ||
| """Returns a random location that is inbounds (within walls if we have walls)""" | ||
|
|
@@ -634,9 +642,7 @@ def get_world(self): | |
| x_start, y_start = (0, 0) | ||
| x_end, y_end = self.width, self.height | ||
| for x in range(x_start, x_end): | ||
| 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)] | ||
|
Comment on lines
-637
to
+645
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| result.append(row) | ||
| return result | ||
|
|
||
|
|
@@ -660,7 +666,7 @@ def run(self, steps=1000, delay=1): | |
| def run(self, steps=1000, delay=1): | ||
| """Run the Environment for given number of time steps, | ||
| but update the GUI too.""" | ||
| for step in range(steps): | ||
| for _ in range(steps): | ||
|
Comment on lines
-663
to
+669
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| self.update(delay) | ||
| if self.is_done(): | ||
| break | ||
|
|
@@ -685,8 +691,8 @@ def reveal(self): | |
| def draw_world(self): | ||
| self.grid[:] = (200, 200, 200) | ||
| world = self.get_world() | ||
| 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])): | ||
|
Comment on lines
-688
to
+695
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if len(world[x][y]): | ||
| self.grid[y, x] = self.colors[world[x][y][-1].__class__.__name__] | ||
|
|
||
|
|
@@ -908,9 +914,7 @@ def get_world(self, show_walls=True): | |
| x_end, y_end = self.width - 1, self.height - 1 | ||
|
|
||
| for x in range(x_start, x_end): | ||
| 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)] | ||
|
Comment on lines
-911
to
+917
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| result.append(row) | ||
| return result | ||
|
|
||
|
|
@@ -938,8 +942,7 @@ def percept(self, agent): | |
| """Return things in adjacent (not diagonal) cells of the agent. | ||
| Result format: [Left, Right, Up, Down, Center / Current location]""" | ||
| x, y = agent.location | ||
| result = [] | ||
| result.append(self.percepts_from(agent, (x - 1, y))) | ||
| result = [self.percepts_from(agent, (x - 1, y))] | ||
|
Comment on lines
-941
to
+945
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| result.append(self.percepts_from(agent, (x + 1, y))) | ||
| result.append(self.percepts_from(agent, (x, y - 1))) | ||
| result.append(self.percepts_from(agent, (x, y + 1))) | ||
|
|
@@ -999,10 +1002,12 @@ def is_done(self): | |
| if explorer[0].alive: | ||
| return False | ||
| else: | ||
| 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]"}.' | ||
| ) | ||
|
|
||
|
Comment on lines
-1002
to
+1010
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return True | ||
|
|
||
| # TODO: Arrow needs to be implemented | ||
|
|
@@ -1024,7 +1029,7 @@ def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000): | |
| >>> performance_ReflexVacuumAgent <= performance_ModelBasedVacuumAgent | ||
| True | ||
| """ | ||
| envs = [EnvFactory() for i in range(n)] | ||
| envs = [EnvFactory() for _ in range(n)] | ||
|
Comment on lines
-1027
to
+1032
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return [(A, test_agent(A, steps, copy.deepcopy(envs))) | ||
| for A in AgentFactories] | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,7 @@ class Thing: | |
| .__name__ slot (used for output only).""" | ||
|
|
||
| def __repr__(self): | ||
| return '<{}>'.format(getattr(self, '__name__', self.__class__.__name__)) | ||
| return f"<{getattr(self, '__name__', self.__class__.__name__)}>" | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def is_alive(self): | ||
| """Things that are 'alive' should return true.""" | ||
|
|
@@ -343,7 +343,7 @@ def step(self): | |
|
|
||
| def run(self, steps=1000): | ||
| """Run the Environment for given number of time steps.""" | ||
| for step in range(steps): | ||
| for _ in range(steps): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if self.is_done(): | ||
| return | ||
| self.step() | ||
|
|
@@ -383,8 +383,8 @@ def delete_thing(self, thing): | |
| except ValueError as e: | ||
| print(e) | ||
| print(" in Environment delete_thing") | ||
| 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]}") | ||
|
Comment on lines
-386
to
+387
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if thing in self.agents: | ||
| self.agents.remove(thing) | ||
|
|
||
|
|
@@ -554,7 +554,12 @@ def add_thing(self, thing, location=None, exclude_duplicate_class_items=False): | |
| def is_inbounds(self, location): | ||
| """Checks to make sure that the location is inbounds (within walls if we have walls)""" | ||
| x, y = location | ||
| 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 | ||
| ) | ||
|
Comment on lines
-557
to
+562
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def random_location_inbounds(self, exclude=None): | ||
| """Returns a random location that is inbounds (within walls if we have walls)""" | ||
|
|
@@ -639,9 +644,7 @@ def get_world(self): | |
| x_start, y_start = (0, 0) | ||
| x_end, y_end = self.width, self.height | ||
| for x in range(x_start, x_end): | ||
| 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)] | ||
|
Comment on lines
-642
to
+647
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| result.append(row) | ||
| return result | ||
|
|
||
|
|
@@ -665,7 +668,7 @@ def run(self, steps=1000, delay=1): | |
| def run(self, steps=1000, delay=1): | ||
| """Run the Environment for given number of time steps, | ||
| but update the GUI too.""" | ||
| for step in range(steps): | ||
| for _ in range(steps): | ||
|
Comment on lines
-668
to
+671
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| self.update(delay) | ||
| if self.is_done(): | ||
| break | ||
|
|
@@ -690,8 +693,8 @@ def reveal(self): | |
| def draw_world(self): | ||
| self.grid[:] = (200, 200, 200) | ||
| world = self.get_world() | ||
| 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])): | ||
|
Comment on lines
-693
to
+697
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if len(world[x][y]): | ||
| self.grid[y, x] = self.colors[world[x][y][-1].__class__.__name__] | ||
|
|
||
|
|
@@ -913,9 +916,7 @@ def get_world(self, show_walls=True): | |
| x_end, y_end = self.width - 1, self.height - 1 | ||
|
|
||
| for x in range(x_start, x_end): | ||
| 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)] | ||
|
Comment on lines
-916
to
+919
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| result.append(row) | ||
| return result | ||
|
|
||
|
|
@@ -943,8 +944,7 @@ def percept(self, agent): | |
| """Return things in adjacent (not diagonal) cells of the agent. | ||
| Result format: [Left, Right, Up, Down, Center / Current location]""" | ||
| x, y = agent.location | ||
| result = [] | ||
| result.append(self.percepts_from(agent, (x - 1, y))) | ||
| result = [self.percepts_from(agent, (x - 1, y))] | ||
|
Comment on lines
-946
to
+947
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| result.append(self.percepts_from(agent, (x + 1, y))) | ||
| result.append(self.percepts_from(agent, (x, y - 1))) | ||
| result.append(self.percepts_from(agent, (x, y + 1))) | ||
|
|
@@ -980,8 +980,8 @@ def execute_action(self, agent, action): | |
| if agent.can_grab(thing)] | ||
| if len(things): | ||
| print("Grabbing", things[0].__class__.__name__) | ||
| if len(things): | ||
| agent.holding.append(things[0]) | ||
| if len(things): | ||
| agent.holding.append(things[0]) | ||
|
Comment on lines
-983
to
+984
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| agent.performance -= 1 | ||
| elif action == 'Climb': | ||
| if agent.location == (1, 1): # Agent can only climb out of (1,1) | ||
|
|
@@ -1018,10 +1018,12 @@ def is_done(self): | |
| if explorer[0].alive: | ||
| return False | ||
| else: | ||
| 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]"}.' | ||
| ) | ||
|
|
||
|
Comment on lines
-1021
to
+1026
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return True | ||
|
|
||
| # TODO: Arrow needs to be implemented | ||
|
|
@@ -1043,7 +1045,7 @@ def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000): | |
| >>> performance_ReflexVacuumAgent <= performance_ModelBasedVacuumAgent | ||
| True | ||
| """ | ||
| envs = [EnvFactory() for i in range(n)] | ||
| envs = [EnvFactory() for _ in range(n)] | ||
|
Comment on lines
-1046
to
+1048
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return [(A, test_agent(A, steps, copy.deepcopy(envs))) | ||
| for A in AgentFactories] | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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:use-fstring-for-formatting)