def __init__(self, name): print(f'Hello, {name}!') self.option = 0 self.parser = Parser("money_tracker.txt") self.parser.make_rows() self.agg = AggregatedMoneyTraker(self.parser) self.agg.generate_categories_from_parser() self.mt = MoneyTracker(self.agg)
def main(): par = Parser("money_tracker.txt") par.make_rows() agg = AggregatedMoneyTraker() agg.generate_categories_from_parser(par) print([str(income) for income in agg.incomes]) print([str(income) for income in agg.expenses])
def main(): print("Hello!") parser = Parser() parsed_content = parser.parse_file('money_tracker.txt') mt = MoneyTracker(parsed_content) mt.create_database() while True: choice = get_input() if choice == 1: print(mt) elif choice == 2: mt.print_records_in_date(parser.get_date()) elif choice == 3: mt.print_records_in_category_ordered_by_labels('expense') elif choice == 4: amount = parser.get_amount() date = parser.get_date() label = input('label: ') mt.add_record(date, amount, label, 'income') elif choice == 5: amount = parser.get_amount() date = parser.get_date() label = input('label: ') mt.add_record(date, amount, label, 'expense') elif choice == 6: break elif choice == -1: continue
class AggregatedMoneyTrakerTests(unittest.TestCase): def setUp(self): self.par = Parser("money_tracker.txt") self.par.make_rows() self.agg = AggregatedMoneyTraker(self.par) self.agg.generate_categories_from_parser() def test_aggregated_incomes(self): expected = [ '760, Salary, New Income', '50, Savings, New Income', '200, Deposit, New Income' ] actual = [str(income) for income in self.agg.incomes] self.assertEqual(actual, expected) def test_aggregated_expenses(self): expected = [ '5.5, Eating Out, New Expense', '34, Clothes, New Expense', '41.79, Food, New Expense', '12, Eating Out, New Expense', '7, House, New Expense', '14, Pets, New Expense', '112.4, Bills, New Expense', '21.5, Transport, New Expense', '15, Food, New Expense', '5, Sports, New Expense' ] actual = [str(income) for income in self.agg.expenses] self.assertEqual(actual, expected) def test_add_income(self): expected = [ '760, Salary, New Income', '50, Savings, New Income', '200, Deposit, New Income', '350, Savings, New Income' ] self.agg.add_income(Income(350, 'Savings', '22-03-2018')) actual = [str(income) for income in self.agg.incomes] self.assertEqual(actual, expected) def test_add_expense(self): expected = [ '5.5, Eating Out, New Expense', '34, Clothes, New Expense', '41.79, Food, New Expense', '12, Eating Out, New Expense', '7, House, New Expense', '14, Pets, New Expense', '112.4, Bills, New Expense', '21.5, Transport, New Expense', '15, Food, New Expense', '5, Sports, New Expense', '50, Food, New Expense' ] self.agg.add_expense(Expense(50, 'Food', '22-03-2018')) actual = [str(income) for income in self.agg.expenses] self.assertEqual(actual, expected)
class MoneyTrackerMenu: def __init__(self, name): print(f'Hello, {name}!') self.option = 0 self.parser = Parser("money_tracker.txt") self.parser.make_rows() self.agg = AggregatedMoneyTraker(self.parser) self.agg.generate_categories_from_parser() self.mt = MoneyTracker(self.agg) def start(self): while True: self.option = int(input("""Choose one of the following options to continue: 1 - show all data 2 - show data for specific date 3 - show expenses, ordered by categories 4 - add new income 5 - add new expense 6 - exit """)) if type(self.option) is not int: raise TypeError if self.option < 1 or self.option > 6: raise ValueError if self.option == 1: print(self.mt.show_user_data()) elif self.option == 2: date = input("Enter date: ") print(self.mt.show_user_data_for_specific_date(date)) elif self.option == 3: print(self.mt.show_user_expenses_ordered_by_categories()) elif self.option == 4: amount = float(input("New income amount: ")) name = input("New income type: ") date = input("New income date: ") self.mt.add_income(amount, name, date) elif self.option == 5: amount = float(input("New expense amount: ")) name = input("New expense type: ") date = input("New expense date: ") self.mt.add_expense(amount, name, date) if self.option == 6: self.mt.save() exit()
class ParserTests(unittest.TestCase): def setUp(self): self.one = Parser("money_tracker.txt") def test_make_rows(self): expected = [ '=== 22-03-2018 ===', '760, Salary, New Income', '5.5, Eating Out, New Expense', '34, Clothes, New Expense', '41.79, Food, New Expense', '12, Eating Out, New Expense', '7, House, New Expense', '14, Pets, New Expense', '112.4, Bills, New Expense', '21.5, Transport, New Expense', '=== 23-03-2018 ===', '50, Savings, New Income', '15, Food, New Expense', '200, Deposit, New Income', '5, Sports, New Expense' ] self.one.make_rows() actual = self.one.file self.assertEqual(actual, expected)
def test_when_parsing_then_return_list_of_lines(self): expected_result = [ '=== 22-03-2019 ===\n', '760, Salary, New Income\n', '5.5, Eating Out, New Expense\n', '34, Clothes, New Expense\n', '41.79, Food, New Expense\n', '12, Eating Out, New Expense\n', '7, House, New Expense\n', '14, Pets, New Expense\n', '112.40, Bills, New Expense\n', '21.5, Transport, New Expense\n', '=== 23-03-2019 ===\n', '50, Savings, New Income\n', '15, Food, New Expense\n', '200, Deposit, New Income\n', '5, Sports, New Expense' ] self.assertEqual(Parser.parse_money_tracker_data('money_tracker.txt'), expected_result)
def __init__(self, file_name): self.records = {} data = Parser.parse_money_tracker_data(file_name) for line in data: if line.startswith('==='): current_date = line[4:-5] self.records[current_date] = {'Expenses': [], 'Incomes': []} else: record = line.split(', ') if 'Income' in record[2]: self.records[current_date]['Incomes'].append( Income(float(record[0]), record[1], current_date)) if 'Expense' in record[2]: self.records[current_date]['Expenses'].append( Expense(float(record[0]), record[1], current_date))
def setUp(self): self.one = Parser("money_tracker.txt")
def get_records_with_label(self, target_label): records = [] for row in self.database: date, cat_obj, category = row if target_label == cat_obj.get_label(): records.append(cat_obj) return records def get_records_in_category_ordered_by_labels(self, target_category): records = self.get_records_in_category(target_category) records.sort(key=lambda cat_obj: cat_obj.get_label()) return records def print_records_in_category_ordered_by_labels(self, target_category): records = self.get_records_in_category_ordered_by_labels(target_category) for record in records: print(record) if __name__ == '__main__': parser = Parser() mt = MoneyTracker(parser.parse_file('money_tracker.txt')) mt.create_database() print(mt) mt.add_record(datetime.strptime('22-03-2019', '%d-%m-%Y'), 250, 'loan', 'expense') mt.sort_database() # mt.print_records_in_date(datetime.strptime('22-03-2019', '%d-%m-%Y')) # print(mt.get_records_in_category_ordered_by_labels('income')) # # [print(row) for row in mt.database]
def setUp(self): self.par = Parser("money_tracker.txt") self.par.make_rows() self.agg = AggregatedMoneyTraker(self.par) self.agg.generate_categories_from_parser()
def _savings(self): result = [cat_obj for date in self.user_data for cat_obj in self.user_data[date]['income'] if cat_obj.get_label() == 'Savings'] return result def _deposits(self): result = [cat_obj for date in self.user_data for cat_obj in self.user_data[date]['income'] if cat_obj.get_label() == 'Deposit'] return result def _get_user_expenses_ordered_by_categories(self): expenses = self._expenses() return sorted(expenses, key=lambda expense: expense.get_label()) def print_user_expenses_ordered_by_categories(self): print('Expenses by categories: ') for expense in self._get_user_expenses_ordered_by_categories(): print(expense) @staticmethod def int_or_float(n: float): return int(n) if n.is_integer() else n if __name__ == '__main__': parser = Parser('money_tracker.txt') mt = MoneyTracker(parser.content()) print(mt.list_user_data())
class MoneyTrakerTests(unittest.TestCase): def setUp(self): self.par = Parser("money_tracker.txt") self.par.make_rows() self.agg = AggregatedMoneyTraker(self.par) self.agg.generate_categories_from_parser() self.mt = MoneyTracker(self.agg) def test_init_money_tracker(self): with self.assertRaises(TypeError): MoneyTracker(self.par) def test_show_user_data(self): expected = "\n".join(['=== 22-03-2018 ===', '760, Salary, New Income', '5.5, Eating Out, New Expense', '34, Clothes, New Expense', '41.79, Food, New Expense', '12, Eating Out, New Expense', '7, House, New Expense', '14, Pets, New Expense', '112.4, Bills, New Expense', '21.5, Transport, New Expense', '=== 23-03-2018 ===', '50, Savings, New Income', '15, Food, New Expense', '200, Deposit, New Income', '5, Sports, New Expense']) actual = self.mt.show_user_data() self.assertEqual(actual, expected) def test_show_user_data_for_specific_date(self): date = '22-03-2018' expected = "\n".join(['760, Salary, New Income', '5.5, Eating Out, New Expense', '34, Clothes, New Expense', '41.79, Food, New Expense', '12, Eating Out, New Expense', '7, House, New Expense', '14, Pets, New Expense', '112.4, Bills, New Expense', '21.5, Transport, New Expense']) actual = self.mt.show_user_data_for_specific_date(date) self.assertEqual(actual, expected) def test_show_user_expenses_ordered_by_categories(self): expected = "\n".join(['112.4, Bills, New Expense', '34, Clothes, New Expense', '5.5, Eating Out, New Expense', '12, Eating Out, New Expense', '41.79, Food, New Expense', '15, Food, New Expense', '7, House, New Expense', '14, Pets, New Expense', '5, Sports, New Expense', '21.5, Transport, New Expense']) actual = self.mt.show_user_expenses_ordered_by_categories() self.assertEqual(actual, expected) def test_list_incomes(self): expected = "\n".join(['760, Salary, New Income', '50, Savings, New Income', '200, Deposit, New Income']) actual = self.mt.list_incomes() self.assertEqual(actual, expected) def test_list_expenses(self): expected = "\n".join(['5.5, Eating Out, New Expense', '34, Clothes, New Expense', '41.79, Food, New Expense', '12, Eating Out, New Expense', '7, House, New Expense', '14, Pets, New Expense', '112.4, Bills, New Expense', '21.5, Transport, New Expense', '15, Food, New Expense', '5, Sports, New Expense']) actual = self.mt.list_expenses() self.assertEqual(actual, expected) def test_add_income(self): expected = ['=== 22-03-2018 ===', '350, Savings, New Income', '760, Salary, New Income', '5.5, Eating Out, New Expense', '34, Clothes, New Expense', '41.79, Food, New Expense', '12, Eating Out, New Expense', '7, House, New Expense', '14, Pets, New Expense', '112.4, Bills, New Expense', '21.5, Transport, New Expense', '=== 23-03-2018 ===', '50, Savings, New Income', '15, Food, New Expense', '200, Deposit, New Income', '5, Sports, New Expense'] amount = 350 name = "Savings" date = '22-03-2018' self.mt.add_income(amount, name, date) actual = self.mt.aggregated.data self.assertEqual(actual, expected) def test_add_expense(self): expected = ['=== 22-03-2018 ===', '760, Salary, New Income', '5.5, Eating Out, New Expense', '34, Clothes, New Expense', '41.79, Food, New Expense', '12, Eating Out, New Expense', '7, House, New Expense', '14, Pets, New Expense', '112.4, Bills, New Expense', '21.5, Transport, New Expense', '=== 23-03-2018 ===', '50, Food, New Expense', '50, Savings, New Income', '15, Food, New Expense', '200, Deposit, New Income', '5, Sports, New Expense'] amount = 50 name = "Food" date = '23-03-2018' self.mt.add_expense(amount, name, date) actual = self.mt.aggregated.data self.assertEqual(actual, expected)