def convert(self, from_monetary, to_currency): """ Convert monetary to different currency :param from_monetary: the monetary unit to be converted :param to_currency: target currency :return: Monetary unit of target currency """ try: rate = self.rates[from_monetary.currency][to_currency] return Monetary(from_monetary * rate, to_currency) except KeyError: raise ExchangeRateNotDefined(from_monetary.currency, to_currency)
def list_accounts(): """ :return: list of Account objects """ conn = sqlite3.connect(db_file) cursor = conn.cursor() cursor.execute("SELECT * FROM accounts") result = [] for row in cursor.fetchall(): result.append( Account(row[0], row[1], row[2], row[3], Monetary(row[4], row[5]))) conn.close() return result
def get_account(ban): """ :param ban: Account.ban (Bank Account Number) :return: Account object """ conn = sqlite3.connect(db_file) cursor = conn.cursor() cursor.execute("SELECT * FROM accounts WHERE ban=?", (ban, )) row = cursor.fetchone() if row: result = Account(row[0], row[1], row[2], row[3], Monetary(row[4], row[5])) else: raise AccountNotFound(ban) conn.close() return result
def list_accounts(): """ :return: list of Account objects """ conn = mysql.connector.connect(host=db_host, database=db_name, user=db_user, password=db_password) cursor = conn.cursor() cursor.execute("SELECT * FROM accounts") result = [] for row in cursor.fetchall(): result.append( Account(row[0], row[1], row[2], row[3], Monetary(row[4], row[5]))) cursor.close() conn.close() return result
def get_account(ban): """ :param ban: Account.ban (Bank Account Number) :return: Account object """ conn = mysql.connector.connect(host=db_host, database=db_name, user=db_user, password=db_password) cursor = conn.cursor() cursor.execute("SELECT * FROM accounts WHERE ban=%s", (ban, )) row = cursor.fetchone() if row: result = Account(row[0], row[1], row[2], row[3], Monetary(row[4], row[5])) else: raise AccountNotFound(ban) cursor.close() conn.close() return result
from money import Monetary, CurrenciesMismatchError amount1 = Monetary(12, "PLN") print(amount1) print(amount1 > Monetary(1, "PLN")) print(amount1 + Monetary(13, "PLN") * 2 - Monetary(10, "PLN") / 2) print(Monetary(-2, "PLN") ** 3) print(Monetary(10, "PLN") % 3) try: Monetary(12, "PLN") + Monetary(13, "USD") except CurrenciesMismatchError as e: print(e)
from bank_db_sqlite import create_account, list_accounts, get_account, update_account, delete_account, AccountNotFound, \ init_db, transfer_money from money import Monetary a1 = Account("Jan", "Kowalski", "ul. Kasztanowa 12, 31-092 Kraków", "1234-1234-1234-1234") a2 = Account("Adam", "Kowalski", "ul. Opolska 100, 31-201 Kraków", "1234-5678-9012-3456") init_db() create_account(a1) create_account(a2) print(list_accounts()) print(get_account("1234-1234-1234-1234")) a1.owner_last_name = "Nowak" a1.balance.amount = 11.00 update_account(a1) print(get_account("1234-1234-1234-1234")) # test transfer transfer_money(a1, a2, Monetary(1, "PLN")) assert a1.balance.amount == 10.00 assert a2.balance.amount == 1.00 delete_account(a1.ban) delete_account(a2.ban) try: print(get_account("1234-1234-1234-1234")) except AccountNotFound: print("The exception was raised, as expected")
from money import Monetary from rate_convert import FixedRateConverter, ExchangeRateNotDefined, DynamicRateConverter converter = FixedRateConverter() print(converter.convert(Monetary(10, "USD"), "EUR")) print(converter.convert(Monetary(10, "USD"), "USD")) try: print(converter.convert(Monetary(10, "USD"), "GHD")) except ExchangeRateNotDefined as error: print(error) converter.import_rates_from_csv('exchange_rates.csv') print(converter.convert(Monetary(10, "USD"), "EUR")) print(converter.convert(Monetary(10, "USD"), "USD")) converter = DynamicRateConverter() print(converter.convert(Monetary(10, "USD"), "EUR")) print(converter.convert(Monetary(10, "USD"), "USD")) print(converter.convert(Monetary(10, "USD"), "GBP"))