def replace_transaction(args): """ Replace the amount of a transaction with a given value. replace <day> <type> <description> with <value> """ if len(args) != 5: raise clisys.InvalidArgumentCount if args[3] != 'with': raise clisys.InvalidArgument(args[3]) try: day = int(args[0]) except ValueError: raise clisys.InvalidArgument(args[0]) type = args[1] description = args[2] try: value = int(args[4]) except ValueError: raise clisys.InvalidArgument(args[4]) repo.replace_item( _transactions, lambda t: tr.get_day(t) == day and tr.get_type(t) == type and tr.get_description(t) == description, tr.create(day, value, type, description))
def list_transactions(args): """ Display the list of transactions. list list <type> list [ < | = | > ] <value> list balance <day> """ count = len(args) if count == 0: display_transactions(repo.get_items(_transactions)) elif count == 1: type = args[0] display_transactions( repo.get_items(_transactions, lambda t: tr.get_type(t) == type)) elif count == 2: option = args[0] if option == 'balance': try: day = int(args[1]) except ValueError: raise clisys.InvalidArgument(args[1]) balance = get_balance(_transactions, day) print(f"Balance on day {day}: {balance}") elif option in ['<', '=', '>']: try: amount = int(args[1]) except ValueError: raise clisys.InvalidArgument(args[1]) if option == '<': display_transactions( repo.get_items(_transactions, lambda t: tr.get_money_amount(t) < amount)) elif option == '=': display_transactions( repo.get_items(_transactions, lambda t: tr.get_money_amount(t) == amount)) elif option == '>': display_transactions( repo.get_items(_transactions, lambda t: tr.get_money_amount(t) > amount)) else: raise clisys.InvalidArgument(args[1]) else: raise clisys.InvalidArgumentCount
def remove_transaction(args): """ Remove transactions from the list. remove <day> remove <start day> to <end day> remove <type> """ count = len(args) if count == 1: try: day = int(args[0]) repo.remove_items(_transactions, lambda t: tr.get_day(t) == day) except ValueError: type = args[0] repo.remove_items(_transactions, lambda t: tr.get_type(t) == type) elif count == 3: if args[1] != 'to': raise clisys.InvalidArgument(args[1]) start_day = int(args[0]) end_day = int(args[2]) repo.remove_items(_transactions, lambda t: start_day <= tr.get_day(t) <= end_day) else: raise clisys.InvalidArgumentCount
def filter_transactions(self, args): """ Filters the transactions. filter <type> filter <type> <value> """ count = len(args) if count == 1: type = args[0] self.__repo.remove_items(lambda t: t.get_type() != type) elif count == 2: type = args[0] try: value = int(args[1]) except ValueError: raise clisys.InvalidArgument([args[1]]) self.__repo.remove_items(lambda t: t.get_type() != type or t. get_money_amount() >= value) print('Transactions filtered successfully!')
def insert_transaction(args): """ Insert a transaction in the list. insert <day> <value> <type> <description> """ try: day = int(args[0]) except ValueError: raise clisys.InvalidArgument(args[0]) try: value = int(args[1]) except ValueError: raise clisys.InvalidArgument(args[1]) type = args[2] description = args[3] transaction = tr.create(day, value, type, description) repo.add_item(_transactions, transaction)
def add_transaction(args): """ Add a transaction to the list. add <value> <type> <description> """ try: value = int(args[0]) except ValueError: raise clisys.InvalidArgument(args[0]) type = args[1] description = args[2] transaction = tr.create(int(datetime.today().strftime('%d')), value, type, description) repo.add_item(_transactions, transaction)
def max_transaction(self, args): if len(args) != 2: raise clisys.InvalidArgumentCount type = args[0] try: day = int(args[1]) except ValueError: raise clisys.InvalidArgument(args[1]) result = get_max(self.__repo, type, day) if result is None: print(f'No transaction of type "{type}" on day {day}.') else: print( f'The maximum "{type}" transaction on day {day}: "{result.get_description()}: {result.get_money_amount()}"' )
def __help(args): """ Displays help for all commands or a single command. help help <command> """ count = len(args) commands = cli.get_commands() if count == 0: for cmd_name, cmd in commands.items(): print(f'{cmd_name}: {cmd.__doc__}') elif count == 1: cmd_name = args[0] if not (cmd_name in commands.keys()): raise clisys.InvalidArgument(args[0]) cmd = commands[cmd_name] print(f'{cmd_name}: {cmd.__doc__}')