style: run through black formatter in neovim

This commit is contained in:
Chris Davoren 2023-11-15 12:37:48 +10:00
parent 9abe516ec7
commit a7e0b692c0
2 changed files with 26 additions and 34 deletions

View File

@ -32,17 +32,10 @@ class Robot:
"SOUTH": 4, "SOUTH": 4,
"SOUTHWEST": 5, "SOUTHWEST": 5,
"WEST": 6, "WEST": 6,
"NORTHWEST": 7 "NORTHWEST": 7,
} }
VALID_COMMANDS = [ VALID_COMMANDS = ["BLOCK", "PLACE", "MOVE", "LEFT", "RIGHT", "REPORT"]
"BLOCK",
"PLACE",
"MOVE",
"LEFT",
"RIGHT",
"REPORT"
]
# Private internals # Private internals
# Key corresponds to numerical direction defined in Robot.directions # Key corresponds to numerical direction defined in Robot.directions
@ -159,8 +152,7 @@ class Robot:
self._position_x = new_position_x self._position_x = new_position_x
self._position_y = new_position_y self._position_y = new_position_y
def place(self, position_x: int, position_y: int, def place(self, position_x: int, position_y: int, direction_name: str = None):
direction_name: str = None):
""" """
Places the Robot instance at the specified coordinates with the Places the Robot instance at the specified coordinates with the
specified direction. specified direction.
@ -262,7 +254,7 @@ class Robot:
def interpret_command(self, command: str): def interpret_command(self, command: str):
""" """
Interprets a given string command and applies the appropriate Interprets a given string command and applies the appropriate
transformation to this Robot instance. Fails silently if the command transformation to this Robot instance. Fails silently if the command
is unrecognized or invalid. is unrecognized or invalid.
@ -274,11 +266,10 @@ class Robot:
""" """
command = command.upper() command = command.upper()
command_tokens = [x.strip() for x in command.split(' ') if len(x) > 0] command_tokens = [x.strip() for x in command.split(" ") if len(x) > 0]
# This is not strictly necessary as case # This is not strictly necessary as case
if len(command_tokens) == 0 or not command_tokens[0] in \ if len(command_tokens) == 0 or not command_tokens[0] in Robot.VALID_COMMANDS:
Robot.VALID_COMMANDS:
return return
match command_tokens[0]: match command_tokens[0]:
@ -289,7 +280,7 @@ class Robot:
# print('Insufficient tokens...') # print('Insufficient tokens...')
return return
parameter_tokens = [x.strip() for x in command_tokens[1].split(',')] parameter_tokens = [x.strip() for x in command_tokens[1].split(",")]
if len(parameter_tokens) < 2: if len(parameter_tokens) < 2:
# print('Insufficient parameters...') # print('Insufficient parameters...')
@ -299,7 +290,7 @@ class Robot:
# print("Adding block: {} {}".format(block_x, block_y)) # print("Adding block: {} {}".format(block_x, block_y))
self.add_block(block_x, block_y) self.add_block(block_x, block_y)
except ValueError as ve: except ValueError:
# print('Integer parsing error...') # print('Integer parsing error...')
return return
case "PLACE": case "PLACE":
@ -307,8 +298,7 @@ class Robot:
# Must have parameters # Must have parameters
if len(command_tokens) < 2: if len(command_tokens) < 2:
return return
parameter_tokens = [x.strip() for x in \ parameter_tokens = [x.strip() for x in command_tokens[1].split(",")]
command_tokens[1].split(',')]
# Must have at least X, Y # Must have at least X, Y
if len(parameter_tokens) < 2: if len(parameter_tokens) < 2:
@ -318,7 +308,7 @@ class Robot:
place_x = int(parameter_tokens[0]) place_x = int(parameter_tokens[0])
place_y = int(parameter_tokens[1]) place_y = int(parameter_tokens[1])
# Direction parameter is optional on second and subsequent # Direction parameter is optional on second and subsequent
# placements. The place() method accounts for an absent # placements. The place() method accounts for an absent
# direction on first call and fails silently. # direction on first call and fails silently.
# print("PLACE command parsed: {}, {}".format(place_x, place_y)) # print("PLACE command parsed: {}, {}".format(place_x, place_y))
@ -327,7 +317,7 @@ class Robot:
self.place(place_x, place_y, place_direction) self.place(place_x, place_y, place_direction)
else: else:
self.place(place_x, place_y) self.place(place_x, place_y)
except ValueError as ve: except ValueError:
# Unable to convert x or y token to int # Unable to convert x or y token to int
return return
case "MOVE": case "MOVE":
@ -355,5 +345,5 @@ class Robot:
self._position_x, self._position_x,
self._position_y, self._position_y,
self.get_direction(), self.get_direction(),
":".join([str(x) for x in self._blocks]) ":".join([str(x) for x in self._blocks]),
) )

View File

@ -12,28 +12,30 @@ def feed_file(filename: str, robot: toyrobot.Robot):
def main(): def main():
print('a)') print("a)")
feed_file('example_a.txt', toyrobot.Robot()) feed_file("example_a.txt", toyrobot.Robot())
print() print()
print('b)') print("b)")
feed_file('example_b.txt', toyrobot.Robot()) feed_file("example_b.txt", toyrobot.Robot())
print() print()
print('c)') print("c)")
feed_file('example_c.txt', toyrobot.Robot()) feed_file("example_c.txt", toyrobot.Robot())
print() print()
print('d)') print("d)")
feed_file('example_d.txt', toyrobot.Robot()) feed_file("example_d.txt", toyrobot.Robot())
print() print()
print('e)') print("e)")
feed_file('example_e.txt', toyrobot.Robot()) feed_file("example_e.txt", toyrobot.Robot())
print() print()
print('f)') print("f)")
feed_file('example_f.txt', toyrobot.Robot()) feed_file("example_f.txt", toyrobot.Robot())
print() print()
if __name__ == "__main__": if __name__ == "__main__":
main() main()