def set_currency(cls, currency: str): """ Alter the currency for all following budget elements (defaults to EUR). Currencies are expressed in a three lettered code as stated in the ISO 4217 standard. """ Group._set_currency(currency) Entry._set_currency(currency) Rates._set_currency(currency)
def test_simple_sum(self): """ Tests the total of a group consisting of two entries with different currencies. """ rates = Rates() rates.add_currency("USD", 2) group = Group("Set Design", [ Entry("Expense 1", 100), Entry("Expense 2", 200, currency="USD"), ]) self.assertEqual(group.total(), Money(200, "EUR"))
def test_total_two_entries_usd(self): """Test the total method while having one entry in USD.""" rates = Rates() rates.add_currency("USD", 2) group = Group("Test Group", [ Entry("Entry 1", 100), Entry("Entry 2", 200, currency="USD"), ]) rsl = group.total() self.assertEqual(rsl, Money(200, "EUR")) self.assertNotEqual(group.total(), Money(300, "EUR")) self.assertNotEqual(group.total(), Money(200, "USD")) self.assertNotEqual(group.total(), Money(300, "USD"))
def __init__(self, expenses: List[Union[Entry, Group]] = [], incomes: List[Union[Entry, Group]] = [], rates: Rates = Rates()): self.expenses = expenses self.incomes = incomes self.__rates = rates
def test_total_complex_group_currencies(self): """ Tests the total function with some more complex (sub)-groups and multiple currencies. """ rates = Rates() rates.add_currency("USD", 2) rates.add_currency("CHF", 0.5) group = Group("Test Group", [ Entry("Entry 1", 100), Group("Sub Group", [ Entry("Sub Entry", 100), Group( "Sub-Sub Group", [ Entry("Sub-Sub Entry 1", 100, currency="CHF"), Entry("Sub-Sub Entry 2", 200, currency="USD"), ], currency="CHF", ) ]), Entry("Sub-Sub Entry 2", 100, currency="CHF"), ]) self.assertEqual(group.total(), Money(700, "EUR"))
def test_complex_sum(self): """Some more currencies.""" rates = Rates() rates.add_currency("USD", 2) rates.add_currency("CHF", 0.5) group = Group("Set Design", [ Entry("Expense 1", 100), Entry("Expense 2", 200, currency="USD"), Entry("Expense 3", 100, currency="CHF"), ]) self.assertEqual(group.total(), Money(400, "EUR"))
class Budget: """ The Budget class is the main class for every ipybudget based budget. It contains the different expense and income groups and provides export functionality for the whole budget. For displaying the each entry it's advised to use the display functionality of the Groups and using Jupyter's markdown blocks to express the structure of the budget. To accommodate multiple currencies in one budget you can define fixed exchanged rates for your budget. """ expenses = List[Union[Entry, Group]] """ All groups and/or entries (also known as Item) which create some financial obligation for the project/company. """ incomes = List[Union[Entry, Group]] """ All groups and/or entries (also known as Item) which create some financial income for the project/company. """ __rates: Rates = Rates() """ Contains the currency exchange rates. The functionality is outsourced from the budget class to offer the separate rendering of exchange rates in the Jupyter notebooks and it's exported documents. """ def __init__(self, expenses: List[Union[Entry, Group]] = [], incomes: List[Union[Entry, Group]] = [], rates: Rates = Rates()): self.expenses = expenses self.incomes = incomes self.__rates = rates @classmethod def set_currency(cls, currency: str): """ Alter the currency for all following budget elements (defaults to EUR). Currencies are expressed in a three lettered code as stated in the ISO 4217 standard. """ Group._set_currency(currency) Entry._set_currency(currency) Rates._set_currency(currency)