From a0fcdc83cbc2b440a5836313b2afd1a35698f5ef Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Tue, 12 Mar 2024 23:46:02 -0600 Subject: AƱade pruebas MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/program.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/program.py (limited to 'src/program.py') diff --git a/src/program.py b/src/program.py new file mode 100644 index 0000000..1c43ea7 --- /dev/null +++ b/src/program.py @@ -0,0 +1,52 @@ +from src.arg_parser import ArgParser +from src.table import Table +from src.exceptions import IncompatibleRowLengthError +import sys + +class Program(): + def __init__(self, args=sys.argv[1:]): + self.arg_parser = ArgParser(args=args) + self.table = Table(self.arg_parser.file) + + def _command_arg_to_int(self): + try: + return int(self.arg_parser.command[1]) + except ValueError: + self.arg_parser.print_help() + exit() + + def _print_table_result(self, table): + for row in table: + print(row) + + def run(self): + match self.arg_parser.command[0]: + case 'row-at': + print(self.table.row_at(self._command_arg_to_int())) + case 'get-rows': + result = self.table.get_rows(self._command_arg_to_int()) + + self._print_table_result(result) + case 'insert': + new_row = tuple(self.arg_parser.command[1].rsplit(',')) + + try: + self.table.insert(new_row) + print('Row added!') + except IncompatibleRowLengthError as error: + print(error) + case 'search': + result = self.table.search(self.arg_parser.command[1]) + + if result is not None: + print(result) + else: + print('Not found!') + case 'delete-at': + try: + self.table.delete_at(self._command_arg_to_int()) + print(f"Row at {self._command_arg_to_int()} deleted") + except IndexError: + print(f"Couldn't find row at {self._command_arg_to_int()}") + case _: + arg_parser.print_help() \ No newline at end of file -- cgit v1.2.3