Skip to content

Commit

Permalink
Simplify isinstance checks with multiple options
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-bass committed Jan 5, 2025
1 parent 885f69a commit 551bd82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
4 changes: 1 addition & 3 deletions pyrobosim/pyrobosim/core/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ def at_openable_location(self):
:return: True if the robot is at an openable location, else False.
:type: bool
"""
return isinstance(self.location, Hallway) or isinstance(
self.location, ObjectSpawn
)
return isinstance(self.location, (Hallway, ObjectSpawn))

def _attach_object(self, obj):
"""
Expand Down
14 changes: 7 additions & 7 deletions pyrobosim/pyrobosim/core/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def open_location(self, location):
# Validate the input
if isinstance(location, str):
location = self.get_entity_by_name(location)
if not (isinstance(location, Location) or isinstance(location, Hallway)):
if not isinstance(location, (Location, Hallway)):
message = message = (
f"Cannot open {location} since it is of type {type(location).__name__}."
)
Expand Down Expand Up @@ -582,7 +582,7 @@ def close_location(self, location, ignore_robots=[]):
# Validate the input
if isinstance(location, str):
location = self.get_entity_by_name(location)
if not (isinstance(location, Location) or isinstance(location, Hallway)):
if not isinstance(location, (Location, Hallway)):
message = message = (
f"Cannot close {location} since it is of type {type(location).__name__}."
)
Expand Down Expand Up @@ -642,7 +642,7 @@ def lock_location(self, location):
# Validate the input
if isinstance(location, str):
location = self.get_entity_by_name(location)
if not (isinstance(location, Location) or isinstance(location, Hallway)):
if not isinstance(location, (Location, Hallway)):
message = message = (
f"Cannot lock {location} since it is of type {type(location).__name__}."
)
Expand Down Expand Up @@ -678,7 +678,7 @@ def unlock_location(self, location):
# Validate the input
if isinstance(location, str):
location = self.get_entity_by_name(location)
if not (isinstance(location, Location) or isinstance(location, Hallway)):
if not isinstance(location, (Location, Hallway)):
message = message = (
f"Cannot unlock {location} since it is of type {type(location).__name__}."
)
Expand Down Expand Up @@ -984,7 +984,7 @@ def add_robot(self, robot, loc=None, pose=None):
if isinstance(loc, str):
loc = self.get_entity_by_name(loc)

if isinstance(loc, Room) or isinstance(loc, Hallway):
if isinstance(loc, (Room, Hallway)):
if pose is None:
# Sample a pose in the location
x_sample, y_sample = sample_from_polygon(
Expand All @@ -1002,7 +1002,7 @@ def add_robot(self, robot, loc=None, pose=None):
self.logger.warning(f"{pose} is occupied")
valid_pose = False
robot_pose = pose
elif isinstance(loc, Location) or isinstance(loc, ObjectSpawn):
elif isinstance(loc, (Location, ObjectSpawn)):
if isinstance(loc, Location):
# NOTE: If you don't want a random object spawn, use the object spawn as the input location.
loc = np.random.choice(loc.children)
Expand Down Expand Up @@ -1694,7 +1694,7 @@ def graph_node_from_entity(
else:
entity = entity_query

if isinstance(entity, ObjectSpawn) or isinstance(entity, Room):
if isinstance(entity, (ObjectSpawn, Room)):
graph_nodes = entity.graph_nodes
elif isinstance(entity, Hallway):
graph_nodes = [entity.graph_nodes[0], entity.graph_nodes[-1]]
Expand Down

0 comments on commit 551bd82

Please sign in to comment.