def getProperties(command): ''' Function to return a list containing the properties of the filter query :param command: a list containing the user command, split by whitespaces :return: another list containing the arguments of the filter command which can be of two types: greater/less than X - all transactions greater/less than X (given amount of money) greater/less than X before Y - all transactions greater/less than X (given amount of money) before given day(Y) :raises exception whenever there is a syntax error, a value error or an invalid parameter error. ''' if len(command) < 3: raise CommandError("Filter command - Syntax error!") if len(command) == 4: raise CommandError("Filter command - Syntax error!") if command[1] != 'than': raise CommandError("Filter command - Syntax error!") if len(command) > 3 and command[3] != 'before': raise CommandError("Filter command - Syntax error!") if not representsInt(command[2]): raise InvalidParameters("Filter command - amount is not an integer!") if int(command[2]) <= 0: raise InvalidParameters( "Filter command - amount should be strictly positive!") if len(command) > 3 and not representsInt(command[4]): raise InvalidParameters("Filter command - day is not an integer!") if len(command) > 3 and int(command[4]) <= 0: raise InvalidParameters( "Filter command - day should be strictly positive!") arguments = [command[0], int(command[2])] if len(command) > 3: arguments.append(int(command[4])) return arguments
def getReplaceTransaction(command): ''' A function to handle the Replace Transaction Feature :param command: the command the user has inputted the transaction the user wants to insert ''' if len(command) < 4: raise CommandError("Replace command - Syntax error!") if command[-2] != 'with': raise CommandError("Replace command - Syntax error!") if not representsInt(command[-1]): raise InvalidParameters("Replace command - Amount not an integer!") if int(command[-1]) <= 0: raise InvalidParameters( 'Replace command - Amount cannot be negative or null!') argsList = command[1].split(',') if len(argsList) < 3: raise CommandError("Replace command - Not enough parameters!") if not representsInt(argsList[0]): raise InvalidParameters("Replace command - Date not an integer!") if int(argsList[0]) <= 0: raise InvalidParameters( "Replace command - Date should be strictly positive!") if argsList[1] not in ['in', 'out']: raise InvalidParameters( 'Replace command - Transaction type unknown, should be only in or out' ) newAmount = int(command[-1]) day = int(argsList[0]) description = ','.join(argsList[2:]) if len(command) > 4: description = description + ' ' + ' '.join(command[2:-2]) return (day, argsList[1], newAmount, description)
def getAddTransaction(command): ''' A function to handle the Add Transaction Feature :param command: the command the user has inputted :return a tuple (day, amount, in/out, description) which describes the transaction the user wants to add or None if there is a command syntax error ''' if len(command) < 2: raise CommandError("Add command - Syntax Error!") argsList = command[1].split(',') if len(argsList) < 3: raise InvalidParameters("Add command - Not enough parameters!") if not representsInt(argsList[0]): raise InvalidParameters( "Add command - The amount of money not an integer!") if int(argsList[0]) <= 0: raise InvalidParameters( "Add command - The amount of money should be strictly positive!") if argsList[1].lower() not in ["in", "out"]: raise InvalidParameters( "Add command - The only known types are in and out!") description = ','.join(argsList[2:]) if len(command) > 2: description = description + ' ' + ' '.join(command[2:]) transaction = (datetime.datetime.now().day, int(argsList[0]), argsList[1], description) return transaction
def getRemoveTransactionDay(command): ''' Function to parse the remove command and to return the transaction day that needs to be removed :param command: :return: an integer - the date parsed from the command ''' if not representsInt(command[1]): raise InvalidParameters("Remove Command - Day not an integer.") if int(command[1]) <= 0: raise InvalidParameters( "Remove Command - Date should be strictly positive.") return int(command[1])
def getBalanceArguments(command): ''' Function to get the arguments of Balance command :param command: a list containing the whitespace-split command from the user :return: an integer representing the day when the use wants to know it's balance :raises exception whenever there is a syntax error, a value error or an invalid parameter error. ''' if len(command) < 2: raise CommandError("Balance - syntax error!") if not representsInt(command[1]): raise InvalidParameters("Balance - day should be integer!") if int(command[1]) <= 0: raise InvalidParameters("Balance - day should be positive!") return int(command[1])
def getRemoveTypeTransaction(command): ''' Function to get the parameter of the Remove Type(in/out) transaction :param command: :return: ''' if command[1] != 'in' and command[1] != 'out': raise InvalidParameters( "Remove command - The only known types of transactions are in and out!" ) return command[1]
def getFilterArguments(command): ''' Function to return a list of arguments which represents the Filter command arguments :param command: a list of strings which is the input string split by whitespaces :return: ''' if len(command) < 2: raise CommandError("Filter command - Syntax Error.") if command[1] != 'in' and command[1] != 'out': raise InvalidParameters( "Filter command - the first parameter should be either in or out.") if len(command) > 2 and not representsInt(command[2]): raise InvalidParameters( "Filter command - the second arguments should be an integer.") if len(command) > 2 and not int(command[2]) < 0: raise InvalidParameters( "Filter command - the second arguments should be positive.") args = [command[1]] if len(command) > 2: args.append(command[2]) return args
def getSumArgument(command): ''' Function to get the arguments of Sum command :param command: a list containing the whitespace-split command from the user :return: an integer representing the sum of all the 'in' transaction or 'out' transaction. :raises exception whenever there is a syntax error, a value error or an invalid parameter error. ''' if len(command) < 2: raise CommandError("Sum command - Syntax error!") if command[1] != 'in' and command[1] != 'out': raise InvalidParameters("Sum command - Unknown transaction type!") return command[1]
def getAllArguments(command): ''' Function to get the arguments of All command :param command: a list containing the whitespace-split command from the user :return: either 'in' or 'out' parsed from what the user inserted :raises exception whenever there is a syntax error, a value error or an invalid parameter error. ''' if len(command) < 2: raise CommandError("All filter - syntax error!") if command[1] != "in" and command[1] != 'out': raise InvalidParameters("All filter - the only known types are in/out") return command[1]
def getInsertTransaction(command): ''' A function to handle the Insert Transaction Feature :param command: the command the user has inputted :return a tuple (day, amount, in/out, description) which describes the transaction the user wants to insert or None if there is a command syntax error ''' if len(command) < 2: raise CommandError("Insert Command - Syntax Error!") argsList = command[1].split(',') if len(argsList) < 4: raise InvalidParameters("Insert Command - Not enough parameters!") if not representsInt(argsList[0]): raise InvalidParameters("Insert Command - Day not an integer!") if int(argsList[0]) <= 0: raise InvalidParameters( "Insert Command - Day should be strictly positive!") if not representsInt(argsList[1]): raise InvalidParameters("Insert Command - Amount not an integer!") if int(argsList[1]) <= 0: raise InvalidParameters( "Insert Command - Amount cannot be negative or nul!") if argsList[2].lower() not in ["in", "out"]: raise InvalidParameters( "Insert Command - The only known transaction types are in and out") description = ','.join(argsList[3:]) if len(command) > 2: description = description + ' ' + ' '.join(command[2:]) transaction = (int(argsList[0]), int(argsList[1]), argsList[2], description) return transaction
def __init__(self, message=None, command_type=None, command_data=None): """ Class for in/out facing command data messages, set either message or both command_type and command_data :param message: Full message, unparsed, with command type and command data inside it :param command_type: Only set if message is not set, Command type in num :param command_data: Only set if message is not set, Data of command """ if message is None and (command_type is None or command_data is None): raise InvalidParameters() if command_type is not None and command_data is not None: if isinstance(command_data, str): command_data = command_data.encode() message = struct.pack("B", command_type) + command_data super().__init__(MESSAGE_TYPE, message)
def getMaxArguments(command): ''' Function to get the arguments of Max command :param command: a list containing the whitespace-split command from the user :return: an string from the set {"in", "out"} which is the argument of the max command :raises exception whenever there is a syntax error, a value error or an invalid parameter error. ''' if len(command) < 3: raise CommandError("Max command - Syntax error!") if command[1] != 'in' and command[1] != 'out': raise InvalidParameters("Max command - Unknown type of transaction") if command[2] != 'day': raise CommandError("Max command - Syntax error!") return command[1]
def getRemoveTransactionInterval(command): ''' A function to handle the Remove Interval Transaction :param command: the command the user has inputted :return a tuple (startDay, endDay) which describes the date interval that the user wants to remoev or None if there is a command syntax error :raise CommandError, ValueError, SyntaxError ''' if command[1] != 'from' or command[3] != 'to': raise CommandError("Remove command - Syntax Error!") if representsInt(command[2]) == False or representsInt( command[4]) == False: raise InvalidParameters("Remove command - Dates should be integers!") if int(command[2]) <= 0: raise InvalidParameters("Remove command - Days should be strictly!") startDate = int(command[2]) endDate = int(command[4]) if startDate > endDate: raise InvalidParameters( "Remove command - Dates do not form an interval!") return (startDate, endDate)