SHARE
TWEET

Untitled

a guest Nov 19th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from domain import *
  2. import json
  3.  
  4.  
  5. def save_to_file(list_of_expenses):
  6.     '''
  7.     Saves a list of expenses to file as json.
  8.     :param list_of_expenses: the list of expenses
  9.     :return: -
  10.     '''
  11.     with open('expenses.txt', 'w') as f_out:
  12.         f_out.write(json.dumps(list_of_expenses))
  13.  
  14.  
  15. def read_from_file():
  16.     '''
  17.     Reads a list of expenses from a file containing json
  18.     :return: a list of expenses
  19.     '''
  20.  
  21.     with open('expenses.txt', 'r') as f_in:
  22.         listExpense = json.load(f_in)
  23.  
  24.     return listExpense
  25.  
  26.  
  27. def addExpense(expense, listExpense):
  28.     '''
  29.     Se adauga o cheltuiala
  30.     :param expense:dictionary, dictionar
  31.     :param listExpense:list, lista de cheltuieli
  32.     :return:
  33.     '''
  34.     listExpense.append(expense)
  35.     save_to_file(listExpense)
  36.  
  37.  
  38. def addMoney(listExpense, date, sum):
  39.     '''
  40.     Adauga o valoare la suma dintr-o data citita
  41.     :param listExpense: lista de cheltuieli
  42.     :param date: data aleasa
  43.     :param sum: suma adaugata
  44.     :return:
  45.     '''
  46.     for expense in listExpense:
  47.         if date in getDate(expense):
  48.             setSum(expense, getSum(expense) + sum)
  49.  
  50.  
  51. def maxExpenseForType(listExpense):
  52.     '''
  53.     Determina cea mai mare cheltuiala pentru fiecare tip
  54.     :param listExpense: list, lista de cheltuieli
  55.     :return:
  56.     '''
  57.     dictExpense = {}
  58.  
  59.     for expense in listExpense:
  60.         expenseSum = getSum(expense)
  61.         expenseType = getType(expense)
  62.         if expenseType in dictExpense:
  63.             previousExpense = dictExpense[expenseType]
  64.             if expenseSum > getSum(previousExpense):
  65.                 dictExpense[expenseType] = expense
  66.         else:
  67.             dictExpense[expenseType] = expense
  68.  
  69.     return dictExpense
  70.  
  71.  
  72. def ordDesc(listExpense):
  73.     '''
  74.     Ordoneaza descrescator cheltuielile
  75.     :param listExpense:list, lista de cheltuieli
  76.     :return:
  77.     '''
  78.     for i in range(len(listExpense) - 1):
  79.         for j in range(i + 1, len(listExpense)):
  80.             if getSum(listExpense[i]) < getSum(listExpense[j]):
  81.                 aux = listExpense[i]
  82.                 listExpense[i] = listExpense[j]
  83.                 listExpense[j] = aux
  84.  
  85.  
  86. def removeExpense(apartment, listExpense):
  87.     '''
  88.     Sterge o cheltuiala
  89.     :param apartment:int, apartamentul care trebuie sters
  90.     :param listExpense: list, lista de cheltuieli
  91.     :return:
  92.     '''
  93.     for i in range(len(listExpense)):
  94.         if getApartment(listExpense[i]) == apartment:
  95.             listExpense.pop(i)
  96.             break
  97.  
  98.  
  99. def removeExpenses(listExpense, apartment):
  100.     '''
  101.     Sterge toate cheltuielile pentru un apartament
  102.     :param listEspense: list, lista de cheltuieli
  103.     :param apartment: int, numarul apartamentului
  104.     :return:
  105.     '''
  106.     listExpense2 = deepcopy(listExpense)
  107.     for expense in listExpense2:
  108.         if getApartment(expense) == apartment:
  109.             removeExpense(getApartment(expense), listExpense)
  110.  
  111.  
  112. def everyMonth(listExpense):
  113.     dict = {}
  114.     for expense in listExpense:
  115.         if getApartment(expense) not in dict:
  116.             dict[getApartment(expense)] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  117.         date = getDate(expense)
  118.         date = date.split('.')
  119.         month = int(date[1])
  120.         dict[getApartment(expense)][month - 1] += getSum(expense)
  121.  
  122.     return dict
  123.  
  124.  
  125. def updateDate(apartment, listExpense, newDate):
  126.     for expense in listExpense:
  127.         if getApartment(expense) == apartment:
  128.             setDate(expense, newDate)
  129.  
  130.  
  131. def updateExpense(apartment, listExpense, newSum, newDate, newType):
  132.     '''
  133.     Modifica o cheltuiala
  134.     :param apartment:int, nr apartament
  135.     :param listExpense: list, lista de cheltuieli
  136.     :param newSum: float, suma care trebuie platita
  137.     :param newDate: data scadenta
  138.     :param newType: tipul
  139.     :return:
  140.     '''
  141.  
  142.     for expense in listExpense:
  143.         if getApartment(expense) == apartment:
  144.             if newSum == '/n':
  145.                 setSum(expense, getSum(expense))
  146.             else:
  147.                 setSum(expense, newSum)
  148.             if newDate == '/n':
  149.                 setDate(expense, getDate(expense))
  150.             else:
  151.                 setDate(expense, newDate)
  152.             if newType == '/n':
  153.                 setType(expense, getType(expense))
  154.             else:
  155.                 setType(expense, newType)
  156.  
  157.  
  158. def updateSum(apartment, listExpense, newSum):
  159.     for expense in listExpense:
  160.         if getApartment(expense) == apartment:
  161.             setSum(expense, newSum)
  162.  
  163.  
  164. def updateType(apartment, listExpense, newType):
  165.     for expense in listExpense:
  166.         if getApartment(expense) == apartment:
  167.             setType(expense, newType)
  168.  
  169.  
  170. '''def Undo(undoList, redoList):
  171.     redoList.append(undoList[len(undoList) - 1])
  172.     undoList.pop(len(undoList) - 1)
  173.  
  174.  
  175. from copy import deepcopy
  176.  
  177.  
  178. def addUndo(listExpense, undoList, redoList):
  179.     c = []
  180.     for expense in listExpense:
  181.         c.append(expense)
  182.     undoList.append(c)
  183.     redoList.clear()'''
  184.  
  185. def Undo(undoList, redoList):
  186.     redoList.append(undoList[len(undoList) - 1])
  187.     undoList.pop(len(undoList) - 1)
  188.  
  189.  
  190. from copy import deepcopy
  191.  
  192.  
  193. def addUndo(listExpense, undoList, redoList):
  194.     c = listExpense[:]
  195.     undoList.append(c)
  196.     redoList.clear()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top