def test_transport_matrix_3_x_3(self): input_data = {'data': [[5, 8, -4], [6, 9, -5], [4, 7, -3]]} expected_data = {'transport': [[5, 6, 4], [8, 9, 7], [-4, -5, -3]]} r = Receiver() i = Invoker() r.set_result(input_data) c = TransportCommand(r) i.execute(c) self.assertEqual(r.get_result(), expected_data)
def test_multiply_case_3(self): matrices = { 'matrix_1': [[0, 0, 0], [0, 0, 0], [0, 0, 0]], 'matrix_2': [[0, 0, 0], [0, 0, 0], [0, 0, 0]] } expected_data = {'multiply': [[0, 0, 0], [0, 0, 0], [0, 0, 0]]} r = Receiver() i = Invoker() r.set_result(matrices) c = MultiplyCommand(r) i.execute(c) self.assertEqual(r.get_result(), expected_data)
def test_delete(self): matrices = { 'matrix_1': [[3, 9, -5], [-3, 8, 8], [-8, -2, 6]], 'matrix_2': [[10, -7, 5], [-2, -8, -8], [-1, -10, -8]] } expected_data = {} r = Receiver() i = Invoker() r.set_result(matrices) c = DeleteCommand(r) i.execute(c) self.assertEqual(r.get_result(), expected_data)
def test_sum_case_1(self): matrices = { 'matrix_1': [[3, 9, -5], [-3, 8, 8], [-8, -2, 6]], 'matrix_2': [[10, -7, 5], [-2, -8, -8], [-1, -10, -8]] } expected_data = {'sum': [[13, 2, 0], [-5, 0, 0], [-9, -12, -2]]} r = Receiver() i = Invoker() r.set_result(matrices) c = SumCommand(r) i.execute(c) self.assertEqual(r.get_result(), expected_data)
def test_sum_case_2(self): matrices = { 'matrix_1': [[-68, -93, -61], [-2, 81, -65], [62, -76, -20]], 'matrix_2': [[38, -61, 57], [52, -27, 81], [32, 48, 69]] } expected_data = {'sum': [[-30, -154, -4], [50, 54, 16], [94, -28, 49]]} r = Receiver() i = Invoker() r.set_result(matrices) c = SumCommand(r) i.execute(c) self.assertEqual(r.get_result(), expected_data)
def test_undo_one_command(self): matrices = { 'matrix_1': [[3, 9, -5], [-3, 8, 8], [-8, -2, 6]], 'matrix_2': [[10, -7, 5], [-2, -8, -8], [-1, -10, -8]] } expected_data = matrices r = Receiver() i = Invoker() r.set_result(matrices) c = SumCommand(r) i.execute(c) i.undo() self.assertEqual(r.get_result(), expected_data)
def test_multiply_case_1(self): matrices = { 'matrix_1': [[3, 9, -5], [-3, 8, 8], [-8, -2, 6]], 'matrix_2': [[10, -7, 5], [-2, -8, -8], [-1, -10, -8]] } expected_data = { 'multiply': [[17, -43, -17], [-54, -123, -143], [-82, 12, -72]] } r = Receiver() i = Invoker() r.set_result(matrices) c = MultiplyCommand(r) i.execute(c) self.assertEqual(r.get_result(), expected_data)
def test_multiply_case_2(self): matrices = { 'matrix_1': [[-68, -93, -61], [-2, 81, -65], [62, -76, -20]], 'matrix_2': [[38, -61, 57], [52, -27, 81], [32, 48, 69]] } expected_data = { 'multiply': [[-9372, 3731, -15618], [2056, -5185, 1962], [-2236, -2690, -4002]] } r = Receiver() i = Invoker() r.set_result(matrices) c = MultiplyCommand(r) i.execute(c) self.assertEqual(r.get_result(), expected_data)
def main(): """ Example of using "Matrices command" started! Enter the command from list ['read', 'write', 'sum', 'multiply', 'transport', 'delete', 'undo'] Commands "read" and "write" have second arg file path, examples: $ read examples/matrices.json $ write result.json Enter the command: read examples/matrices.json result: {'matrix_1': [[5, 8, -4], [6, 9, -5], [4, 7, -3]], 'matrix_2': [[3, 2, 5], [4, -1, 3], [9, 6, 5]]} Enter the command: sum result: {'sum': [[8, 10, 1], [10, 8, -2], [13, 13, 2]]} Enter the command: transport result: {'transport': [[8, 10, 13], [10, 8, 13], [1, -2, 2]]} Enter the command: undo result: {'sum': [[8, 10, 1], [10, 8, -2], [13, 13, 2]]} Enter the command: exit End. """ print('"Matrices command" started!') receiver = Receiver() invoker = Invoker() print(f'Enter the command from list {[c.name for c in Commands]}') print('Commands "read" and "write" have second arg file path, examples:') print('$ read examples/matrices.json') print('$ write result.json') while True: inputs = input('Enter the command: ') if inputs == 'exit': print('End.') exit() elif inputs == 'undo': invoker.undo() else: args = inputs.split() command_cls = Commands[args[0]].value if len(args) == 1: command = command_cls(receiver) invoker.execute(command) elif len(args) == 2: arg = args[1] command = command_cls(receiver, arg) invoker.execute(command) print(f'result: {receiver.get_result()}')
from matrices_command.receiver import Receiver from matrices_command.invoker import Invoker from matrices_command.commands import * receiver = Receiver() invoker = Invoker() print(receiver.get_result()) # {} read_command = ReadCommand(receiver, read_from='matrices.json') invoker.execute(read_command) print( receiver.get_result() ) # {'matrix_1': [[5, 8, -4], [6, 9, -5], [4, 7, -3]], 'matrix_2': [[3, 2, 5], [4, -1, 3], [9, 6, 5]]} sum_command = SumCommand(receiver) invoker.execute(sum_command) print(receiver.get_result()) # {'sum': [[8, 10, 1], [10, 8, -2], [13, 13, 2]]} invoker.undo() print( receiver.get_result() ) # {'matrix_1': [[5, 8, -4], [6, 9, -5], [4, 7, -3]], 'matrix_2': [[3, 2, 5], [4, -1, 3], [9, 6, 5]]} multiply_command = MultiplyCommand(receiver) invoker.execute(multiply_command) print(receiver.get_result() ) # {'multiply': [[11, -22, 29], [9, -27, 32], [13, -17, 26]]} transport_command = TransportCommand(receiver) invoker.execute(transport_command)