def insert(userInput, categoryList, undo_steps, undo, step_count): day, categ, sum, remainder = getDayCategAndValue(userInput) try: day = int(day) # Verifing that the user input is correct if day > 31 or day < 1 or len( remainder ) > 0: # Imposing that the day must be a number between 1 and 31 ( [1, 31] ) return " Invalid syntax" except ValueError: return " Invalid day value or syntax" # day try: sum = int(sum) # sum if undo == 1 and sum < 0 or categ not in categoryList: return " Invalid syntax" except ValueError: return " Invalid sum value" categoryDictionary = files.initializeDictionary( day, categoryList) # Today's date (day) categoryDictionary[ categ] += sum # updating the sum of the selected category files.fileUpdate( day, categoryDictionary ) # updating the file for this day with the new information if undo: stepCountUpdate(undo_steps, sum, categ, day, step_count) return " Inserted successfully!"
def deleteAllExcept(category, symbol, value, undo_steps, step_count, categoryList): ''' This function formats all the categories except the one that was passed through the parameter input: the category that we would like to filter This function will filter all of the categories leaving the category from the parameters intact if and only if the condition regarding the symbol is fulfilled input: the category, symbol and value ''' for i in range(1, 32): dictForDayI = files.initializeDictionary(i, categoryList) for key in dictForDayI: if key != category or ( symbol == ">" and dictForDayI[key] <= value) or ( symbol == "<" and dictForDayI[key] >= value) or ( symbol == "=" and dictForDayI[key] != value): if dictForDayI[key] != 0: undo_steps.append([ step_count, "insert " + str(i) + " " + str(dictForDayI[key]) + " " + str(key) ]) # updating the undo list dictForDayI[key] = "0" files.fileUpdate(i, dictForDayI)
def removeExpenses(a, b, undo_steps, step_count, categoryList): ''' This function removes all the expenses for days in the interval [a, b] passed in by parameters (updates the files with the default dictionary) input: a, b - start day and end day ''' default_dict = { "housekeeping": 0, "food": 0, "transport": 0, "clothing": 0, "internet": 0, "others": 0 } for i in range(a, b + 1): aux_dict = files.initializeDictionary(i, categoryList) for key in aux_dict: if aux_dict[key] != 0: undo_steps.append([ step_count, "insert " + str(i) + " " + str(aux_dict[key]) + " " + str(key) ]) # updating the undo list files.fileUpdate(i, default_dict)
def add(userInput, categoryList, undo_steps, step_count): categ, sum, remainder = getCategAndValue( userInput) # getting the category and the sum from the user input day = int(time.strftime("%d")) try: # In case that the user has entered an invalid number that may contain sum = int( sum ) # strings and other characters other than digits and or '-', '+', etc. if len( remainder ) > 0 or sum < 0 or categ not in categoryList: # In case the user has entered additional characters after the valid syntax return " Invalid syntax, check sum or category" except ValueError: # the program doesn't crash return " Invalid sum or syntax" categoryDictionary = files.initializeDictionary( day, categoryList) # Today's date (day) categoryDictionary[ categ] += sum # updating the sum of the selected category files.fileUpdate( int(time.strftime("%d")), categoryDictionary ) # updating the file for this day with the new information stepCountUpdate(undo_steps, sum, categ, day, step_count) #updating the undo list return " Added successfully!"
def removeExpensesByCategory(categ, undo_steps, step_count, categoryList): ''' The function removes all the expenses for a certain category from day 1 to day 31 (updates all the files with the default dictionary) input: the category ''' for i in range(1, 32): aux_dict = files.initializeDictionary(i, categoryList) if aux_dict[categ] != 0: undo_steps.append([ step_count, "insert " + str(i) + " " + str(aux_dict[categ]) + " " + str(categ) ]) # updating the undo list aux_dict[categ] = 0 files.fileUpdate(i, aux_dict)