Fixed line lengths according to Python PEP8 E501.

This commit is contained in:
Chris Davoren 2023-09-29 10:35:42 +10:00
parent e41e360ed8
commit c19a01b720
1 changed files with 57 additions and 53 deletions

View File

@ -1,15 +1,16 @@
class Robot: class Robot:
""" """
Class representing a single "Toy Robot". Robots instances have knowledge of their Class representing a single "Toy Robot". Robots instances have knowledge
position, direction, and movement limits. of their position, direction, and movement limits.
Attributes: Attributes:
DEFAULT_MAX_X (int): The default maximum allowable horizontal coordinate DEFAULT_MAX_X (int): The default maximum allowable horizontal
(inclusive). coordinate (inclusive).
DEFAULT_MAX_Y (int): The default maximum allowable vertical coordinate DEFAULT_MAX_Y (int): The default maximum allowable vertical coordinate
(inclusive). (inclusive).
DIRECTIONS (dict): A dictionary (str: int) of direction names (e.g. NORTH, EAST DIRECTIONS (dict): A dictionary (str: int) of direction names (e.g.
etc) and their numerical encoding. Only the keys are intended for use externally as a list of valid directions. NORTH, EAST etc) and their numerical encoding. Only the keys are
intended for use externally as a list of valid directions.
""" """
DEFAULT_MAX_X = 5 DEFAULT_MAX_X = 5
@ -25,8 +26,8 @@ class Robot:
# Private internals # Private internals
# Key corresponds to numerical direction defined in Robot.directions # Key corresponds to numerical direction defined in Robot.directions
# Value is an offset vector in with x and y keys (positive x is NORTH, positive y is # Value is an offset vector in with x and y keys (positive x is NORTH,
# EAST) # positive y is EAST)
_MOVEMENT_VECTORS = { _MOVEMENT_VECTORS = {
0: {"x": 0, "y": 1}, 0: {"x": 0, "y": 1},
1: {"x": 1, "y": 0}, 1: {"x": 1, "y": 0},
@ -36,18 +37,18 @@ class Robot:
def __init__(self, max_x: int = DEFAULT_MAX_X, max_y: int = DEFAULT_MAX_Y): def __init__(self, max_x: int = DEFAULT_MAX_X, max_y: int = DEFAULT_MAX_Y):
""" """
Creates a new Robot instance with the specified position limits (optional). Creates a new Robot instance with the specified position limits
Implicitly, the minimum limit on coordinates is 0 both horizontally and (optional). Implicitly, the minimum limit on coordinates is 0 both
vertically. horizontally and vertically.
See the class attributes `DEFAULT_MAX_X` and `DEFAULT_MAX_Y` for the respective See the class attributes `DEFAULT_MAX_X` and `DEFAULT_MAX_Y` for the
default numerical values. respective default numerical values.
Args: Args:
max_x (int): The maximum allowable horizontal coordinate (inclusive, max_x (int): The maximum allowable horizontal coordinate
positive is EAST). (inclusive, positive is EAST).
max_y (int): The maximum allowable vertical coordinate (inclusive, max_y (int): The maximum allowable vertical coordinate
positive is NORTH). (inclusive, positive is NORTH).
Raises: Raises:
ValueError: If max_x or max_y are less than 0. ValueError: If max_x or max_y are less than 0.
@ -86,18 +87,18 @@ class Robot:
Returns the current maximum coordinates valid for this instance. Returns the current maximum coordinates valid for this instance.
Returns: Returns:
(int, int): A tuple of the maximum coordinates in the order (maximum_x, (int, int): A tuple of the maximum coordinates in the order
maximum y). (maximum_x, maximum y).
""" """
return (self._max_x, self._max_y) return (self._max_x, self._max_y)
def valid_position(self, x: int, y: int) -> bool: def valid_position(self, x: int, y: int) -> bool:
""" """
Calculates whether the given coordinates are valid for the limits set on this Calculates whether the given coordinates are valid for the limits set
Robot instance. on this Robot instance.
This function is used by the `move()` function to verify that a move action This function is used by the `move()` function to verify that a move
would be successful. action would be successful.
Args: Args:
x: Proposed horizontal coordinate. x: Proposed horizontal coordinate.
@ -111,11 +112,11 @@ class Robot:
def is_initialized(self): def is_initialized(self):
""" """
Returns whether this Robot instance has been initialized (i.e. whether a valid Returns whether this Robot instance has been initialized (i.e. whether
`place()` command has been executed). a valid `place()` command has been executed).
This function is used by the `move()` function to verify that the instance has This function is used by the `move()` function to verify that the
been correctly placed before movement. instance has been correctly placed before movement.
Returns: Returns:
bool: True if correctly initialized, otherwise false. bool: True if correctly initialized, otherwise false.
@ -128,11 +129,12 @@ class Robot:
def move(self): def move(self):
""" """
Moves the robot one space in the direction it is currently facing, provided that Moves the robot one space in the direction it is currently facing,
said movement would be within limits. provided that said movement would be within limits.
Will do nothing if this Robot instance has not been initialized successfully Will do nothing (fail silently) if this Robot instance has not been
with `place()` or the specified movement would be out of bounds. initialized successfully with `place()` or the specified movement would
be out of bounds.
""" """
if not self.is_initialized(): if not self.is_initialized():
@ -150,24 +152,23 @@ class Robot:
def place(self, position_x: int, position_y: int, direction_name: str): def place(self, position_x: int, position_y: int, direction_name: str):
""" """
Places the Robot instance at the specified coordinates with the specified Places the Robot instance at the specified coordinates with the
direction. specified direction.
Will do nothing if the given coordinates are out of bounds, or the direction is Will do nothing if the given coordinates are out of bounds, or the
not one of the keys in `Robot.DIRECTIONS`. direction is not one of the keys in `Robot.DIRECTIONS`.
See `__init__()` for detailed coordinate information. See `__init__()` for detailed coordinate information.
Args: Args:
position_x (int): Horizontal coordinate for placement. position_x (int): Horizontal coordinate for placement.
position_y (int): Vertical coordinate for placement. position_y (int): Vertical coordinate for placement.
direction_name (str): Direction of placement; must be one of the string keys direction_name (str): Direction of placement; must be one of the
in `Robot.DIRECTIONS`. string keys in `Robot.DIRECTIONS`.
""" """
direction_name = direction_name.upper() direction_name = direction_name.upper()
if direction_name not in Robot.DIRECTIONS.keys() or not self.valid_position( if direction_name not in Robot.DIRECTIONS.keys() or \
position_x, position_y not self.valid_position(position_x, position_y):
):
return return
self._position_x = position_x self._position_x = position_x
@ -179,9 +180,9 @@ class Robot:
Returns the current position of this Robot instance. Returns the current position of this Robot instance.
Returns: Returns:
(int, int): A tuple with this instance's current coordinates in the order (int, int): A tuple with this instance's current coordinates in the
(position_x, position_y), or (None, None) if this instance has not yet order (position_x, position_y), or (None, None) if this
been initialized. instance has not yet been initialized.
""" """
return ( return (
(self._position_x, self._position_y) (self._position_x, self._position_y)
@ -191,12 +192,13 @@ class Robot:
def get_direction(self) -> str: def get_direction(self) -> str:
""" """
Returns the direction in which this instance is currently facing in string form. Returns the direction in which this instance is currently facing in
string form.
Returns: Returns:
str: The current direction of this instance; will be one of the keys of str: The current direction of this instance; will be one of the
`Robot.DIRECTION`. Will return None if this instance has not yet been keys of `Robot.DIRECTION`. Will return None if this instance
initialized. has not yet been initialized.
""" """
return ( return (
{v: k for k, v in Robot.DIRECTIONS.items()}[self._direction] {v: k for k, v in Robot.DIRECTIONS.items()}[self._direction]
@ -206,10 +208,11 @@ class Robot:
def rotate_left(self): def rotate_left(self):
""" """
Rotates this Robot instance's direction to the LEFT. For example, if currently Rotates this Robot instance's direction to the LEFT. For example, if
facing NORTH, the new direction will be WEST. currently facing NORTH, the new direction will be WEST.
Does nothing if this instance has not yet been correctly placed (initialized). Does nothing if this instance has not yet been correctly placed
(initialized).
""" """
if not self.is_initialized(): if not self.is_initialized():
return return
@ -217,10 +220,11 @@ class Robot:
def rotate_right(self): def rotate_right(self):
""" """
Rotates this Robot instance's direction to the RIGHT. For example, if currently Rotates this Robot instance's direction to the RIGHT. For example, if
facing NORTH, the new direction will be EAST. currently facing NORTH, the new direction will be EAST.
Does nothing if this instance has not yet been correctly placed (initialized). Does nothing if this instance has not yet been correctly placed
(initialized).
""" """
if self.is_initialized(): if self.is_initialized():
return return