Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43c004d

Browse files
authored
Merge pull request #2 from sarvex/sourcery/main
Sourcery refactored main branch
2 parents 61b800b + 89b40b6 commit 43c004d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1363
-1375
lines changed

agents.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Thing:
5050
.__name__ slot (used for output only)."""
5151

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

5555
def is_alive(self):
5656
"""Things that are 'alive' should return true."""
@@ -338,7 +338,7 @@ def step(self):
338338

339339
def run(self, steps=1000):
340340
"""Run the Environment for given number of time steps."""
341-
for step in range(steps):
341+
for _ in range(steps):
342342
if self.is_done():
343343
return
344344
self.step()
@@ -378,8 +378,8 @@ def delete_thing(self, thing):
378378
except ValueError as e:
379379
print(e)
380380
print(" in Environment delete_thing")
381-
print(" Thing to be removed: {} at {}".format(thing, thing.location))
382-
print(" from list: {}".format([(thing, thing.location) for thing in self.things]))
381+
print(f" Thing to be removed: {thing} at {thing.location}")
382+
print(f" from list: {[(thing, thing.location) for thing in self.things]}")
383383
if thing in self.agents:
384384
self.agents.remove(thing)
385385

@@ -506,8 +506,11 @@ def execute_action(self, agent, action):
506506
elif action == 'Forward':
507507
agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))
508508
elif action == 'Grab':
509-
things = [thing for thing in self.list_things_at(agent.location) if agent.can_grab(thing)]
510-
if things:
509+
if things := [
510+
thing
511+
for thing in self.list_things_at(agent.location)
512+
if agent.can_grab(thing)
513+
]:
511514
agent.holding.append(things[0])
512515
print("Grabbing ", things[0].__class__.__name__)
513516
self.delete_thing(things[0])
@@ -552,7 +555,12 @@ def add_thing(self, thing, location=None, exclude_duplicate_class_items=False):
552555
def is_inbounds(self, location):
553556
"""Checks to make sure that the location is inbounds (within walls if we have walls)"""
554557
x, y = location
555-
return not (x < self.x_start or x > self.x_end or y < self.y_start or y > self.y_end)
558+
return (
559+
x >= self.x_start
560+
and x <= self.x_end
561+
and y >= self.y_start
562+
and y <= self.y_end
563+
)
556564

557565
def random_location_inbounds(self, exclude=None):
558566
"""Returns a random location that is inbounds (within walls if we have walls)"""
@@ -634,9 +642,7 @@ def get_world(self):
634642
x_start, y_start = (0, 0)
635643
x_end, y_end = self.width, self.height
636644
for x in range(x_start, x_end):
637-
row = []
638-
for y in range(y_start, y_end):
639-
row.append(self.list_things_at((x, y)))
645+
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
640646
result.append(row)
641647
return result
642648

@@ -660,7 +666,7 @@ def run(self, steps=1000, delay=1):
660666
def run(self, steps=1000, delay=1):
661667
"""Run the Environment for given number of time steps,
662668
but update the GUI too."""
663-
for step in range(steps):
669+
for _ in range(steps):
664670
self.update(delay)
665671
if self.is_done():
666672
break
@@ -908,9 +914,7 @@ def get_world(self, show_walls=True):
908914
x_end, y_end = self.width - 1, self.height - 1
909915

910916
for x in range(x_start, x_end):
911-
row = []
912-
for y in range(y_start, y_end):
913-
row.append(self.list_things_at((x, y)))
917+
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
914918
result.append(row)
915919
return result
916920

@@ -938,8 +942,7 @@ def percept(self, agent):
938942
"""Return things in adjacent (not diagonal) cells of the agent.
939943
Result format: [Left, Right, Up, Down, Center / Current location]"""
940944
x, y = agent.location
941-
result = []
942-
result.append(self.percepts_from(agent, (x - 1, y)))
945+
result = [self.percepts_from(agent, (x - 1, y))]
943946
result.append(self.percepts_from(agent, (x + 1, y)))
944947
result.append(self.percepts_from(agent, (x, y - 1)))
945948
result.append(self.percepts_from(agent, (x, y + 1)))
@@ -999,10 +1002,11 @@ def is_done(self):
9991002
if explorer[0].alive:
10001003
return False
10011004
else:
1002-
print("Death by {} [-1000].".format(explorer[0].killed_by))
1005+
print(f"Death by {explorer[0].killed_by} [-1000].")
10031006
else:
1004-
print("Explorer climbed out {}."
1005-
.format("with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"))
1007+
print(
1008+
f'Explorer climbed out {"with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"}.'
1009+
)
10061010
return True
10071011

10081012
# TODO: Arrow needs to be implemented
@@ -1024,7 +1028,7 @@ def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000):
10241028
>>> performance_ReflexVacuumAgent <= performance_ModelBasedVacuumAgent
10251029
True
10261030
"""
1027-
envs = [EnvFactory() for i in range(n)]
1031+
envs = [EnvFactory() for _ in range(n)]
10281032
return [(A, test_agent(A, steps, copy.deepcopy(envs)))
10291033
for A in AgentFactories]
10301034

agents4e.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Thing:
5555
.__name__ slot (used for output only)."""
5656

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

6060
def is_alive(self):
6161
"""Things that are 'alive' should return true."""
@@ -343,7 +343,7 @@ def step(self):
343343

344344
def run(self, steps=1000):
345345
"""Run the Environment for given number of time steps."""
346-
for step in range(steps):
346+
for _ in range(steps):
347347
if self.is_done():
348348
return
349349
self.step()
@@ -383,8 +383,8 @@ def delete_thing(self, thing):
383383
except ValueError as e:
384384
print(e)
385385
print(" in Environment delete_thing")
386-
print(" Thing to be removed: {} at {}".format(thing, thing.location))
387-
print(" from list: {}".format([(thing, thing.location) for thing in self.things]))
386+
print(f" Thing to be removed: {thing} at {thing.location}")
387+
print(f" from list: {[(thing, thing.location) for thing in self.things]}")
388388
if thing in self.agents:
389389
self.agents.remove(thing)
390390

@@ -554,7 +554,12 @@ def add_thing(self, thing, location=None, exclude_duplicate_class_items=False):
554554
def is_inbounds(self, location):
555555
"""Checks to make sure that the location is inbounds (within walls if we have walls)"""
556556
x, y = location
557-
return not (x < self.x_start or x > self.x_end or y < self.y_start or y > self.y_end)
557+
return (
558+
x >= self.x_start
559+
and x <= self.x_end
560+
and y >= self.y_start
561+
and y <= self.y_end
562+
)
558563

559564
def random_location_inbounds(self, exclude=None):
560565
"""Returns a random location that is inbounds (within walls if we have walls)"""
@@ -639,9 +644,7 @@ def get_world(self):
639644
x_start, y_start = (0, 0)
640645
x_end, y_end = self.width, self.height
641646
for x in range(x_start, x_end):
642-
row = []
643-
for y in range(y_start, y_end):
644-
row.append(self.list_things_at((x, y)))
647+
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
645648
result.append(row)
646649
return result
647650

@@ -665,7 +668,7 @@ def run(self, steps=1000, delay=1):
665668
def run(self, steps=1000, delay=1):
666669
"""Run the Environment for given number of time steps,
667670
but update the GUI too."""
668-
for step in range(steps):
671+
for _ in range(steps):
669672
self.update(delay)
670673
if self.is_done():
671674
break
@@ -913,9 +916,7 @@ def get_world(self, show_walls=True):
913916
x_end, y_end = self.width - 1, self.height - 1
914917

915918
for x in range(x_start, x_end):
916-
row = []
917-
for y in range(y_start, y_end):
918-
row.append(self.list_things_at((x, y)))
919+
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
919920
result.append(row)
920921
return result
921922

@@ -943,8 +944,7 @@ def percept(self, agent):
943944
"""Return things in adjacent (not diagonal) cells of the agent.
944945
Result format: [Left, Right, Up, Down, Center / Current location]"""
945946
x, y = agent.location
946-
result = []
947-
result.append(self.percepts_from(agent, (x - 1, y)))
947+
result = [self.percepts_from(agent, (x - 1, y))]
948948
result.append(self.percepts_from(agent, (x + 1, y)))
949949
result.append(self.percepts_from(agent, (x, y - 1)))
950950
result.append(self.percepts_from(agent, (x, y + 1)))
@@ -980,8 +980,8 @@ def execute_action(self, agent, action):
980980
if agent.can_grab(thing)]
981981
if len(things):
982982
print("Grabbing", things[0].__class__.__name__)
983-
if len(things):
984-
agent.holding.append(things[0])
983+
if len(things):
984+
agent.holding.append(things[0])
985985
agent.performance -= 1
986986
elif action == 'Climb':
987987
if agent.location == (1, 1): # Agent can only climb out of (1,1)
@@ -1018,10 +1018,11 @@ def is_done(self):
10181018
if explorer[0].alive:
10191019
return False
10201020
else:
1021-
print("Death by {} [-1000].".format(explorer[0].killed_by))
1021+
print(f"Death by {explorer[0].killed_by} [-1000].")
10221022
else:
1023-
print("Explorer climbed out {}."
1024-
.format("with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"))
1023+
print(
1024+
f'Explorer climbed out {"with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"}.'
1025+
)
10251026
return True
10261027

10271028
# TODO: Arrow needs to be implemented
@@ -1043,7 +1044,7 @@ def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000):
10431044
>>> performance_ReflexVacuumAgent <= performance_ModelBasedVacuumAgent
10441045
True
10451046
"""
1046-
envs = [EnvFactory() for i in range(n)]
1047+
envs = [EnvFactory() for _ in range(n)]
10471048
return [(A, test_agent(A, steps, copy.deepcopy(envs)))
10481049
for A in AgentFactories]
10491050

0 commit comments

Comments
 (0)