def __init__(self): self.rates_file = os.path.join(settings.BASE_DIR, settings.CURRENCY_RATES_FILE) try: with open(self.rates_file, 'rb') as f: self.rates = pickle.load(f) except FileNotFoundError: print( 'Warning. Probably the first run because rates.txt file does not exist.' ) xrates.install('money.exchange.SimpleBackend') self.update_status = 'not updated' self.last_modified = ''
def test_multiple_xrates(self): xrates.install('money.exchange.SimpleBackend') self.assertTrue(xrates) xrates.base = 'XXX' xrates.setrate('AAA', Decimal('2')) from money.exchange import ExchangeRates another = ExchangeRates() another.install('money.exchange.SimpleBackend') self.assertTrue(another) another.base = 'XXX' another.setrate('AAA', Decimal('100')) self.assertEqual(xrates.rate('AAA'), Decimal('2')) self.assertEqual(another.rate('AAA'), Decimal('100'))
def test_multiple_xrates(self): xrates.install("money.exchange.SimpleBackend") self.assertTrue(xrates) xrates.base = "XXX" xrates.setrate("AAA", Decimal("2")) from money.exchange import ExchangeRates another = ExchangeRates() another.install("money.exchange.SimpleBackend") self.assertTrue(another) another.base = "XXX" another.setrate("AAA", Decimal("100")) self.assertEqual(xrates.rate("AAA"), Decimal("2")) self.assertEqual(another.rate("AAA"), Decimal("100"))
def setup_xrates(base, rates): """ If using the Python money package, this will set up the xrates exchange rate data. :param base: The string currency code to use as the base :param rates: A dict with keys that are string currency codes and values that are a Decimal of the exchange rate for that currency. """ xrates.install('money.exchange.SimpleBackend') xrates.base = base for code, value in rates.items(): xrates.setrate(code, value)
def __init__(self): self.currencies = ["JPY", "EUR", "CAD", "CNY", "GBP"] self.rates = [ Decimal("122.690"), Decimal("0.92823"), Decimal("1.32925"), Decimal("6.37419"), Decimal("0.65742"), ] xrates.install("money.exchange.SimpleBackend") xrates.base = "USD" for i in range(len(self.currencies)): xrates.setrate(self.currencies[i], self.rates[i])
def setUp(self): xrates.install('money.exchange.SimpleBackend')
def test_unregister(self): xrates.install("money.exchange.SimpleBackend") self.assertEqual(xrates.backend_name, "SimpleBackend") xrates.uninstall() self.assertFalse(xrates) self.assertIsNone(xrates.backend_name)
def test_register_instance(self): self.assertFalse(xrates) self.assertIsNone(xrates.backend_name) xrates.install(SimpleBackend()) self.assertEqual(xrates.backend_name, "SimpleBackend")
def test_unavailable_rate_conversion_error(self): xrates.install("money.exchange.SimpleBackend") with self.assertRaises(ExchangeRateNotFound): self.MoneyClass("2", "AAA").to("BBB")
def setUp(self): xrates.install("money.exchange.SimpleBackend")
def test_unavailable_rate_conversion_error(self): xrates.install('money.exchange.SimpleBackend') with self.assertRaises(ExchangeRateNotFound): self.MoneyClass('2', 'AAA').to('BBB')
def setUpClass(cls): xrates.install('money.exchange.SimpleBackend') xrates.base = 'XXX' xrates.setrate('AAA', Decimal('2')) xrates.setrate('BBB', Decimal('8'))
def test_unregister(self): xrates.install('money.exchange.SimpleBackend') self.assertEqual(xrates.backend_name, 'SimpleBackend') xrates.uninstall() self.assertFalse(xrates) self.assertIsNone(xrates.backend_name)
def test_register_instance(self): self.assertFalse(xrates) self.assertIsNone(xrates.backend_name) xrates.install(SimpleBackend()) self.assertEqual(xrates.backend_name, 'SimpleBackend')
def test_base_not_set_warning(self): xrates.uninstall() xrates.install('money.exchange.SimpleBackend') with self.assertRaises(Warning): xrates.setrate('AAA', Decimal('2'))
from money import XMoney from money import xrates import decimal import pandas as pd # install abstract base class xrates.install('money.exchange.SimpleBackend') #set US Dollar as the currency the others will be converted to xrates.base = 'USD' # set the exchange rate for EUR to USD xrates.setrate('EUR', decimal.Decimal('0.9')) # set the exchange rate for GBP to USD xrates.setrate('GBP', decimal.Decimal('0.8')) # data frame - uses Pandas' Extension Array Interface df = pd.DataFrame({"money": MoneyArray(['GBP 100', 'EUR 300'], 'USD')}) print("The table with the currencies:\n", df) # the data frame table is accessed via the row, ie 0 is row 1 & 1 is row 2. # the subtraction method needs accessed through XMoney, a money subclass. Then it's converted to USD difference = (df['money'][0].__sub__(df['money'][1])).to('USD') print("\nDifference between GBP 100 & EUR 300, in USD:\n", difference)
from money import XMoney, xrates, Money from decimal import Decimal #Exchange rates current as of June 8th, 2016 xrates.install('money.exchange.SimpleBackend') xrates.base = 'USD' rates = {'EUR': 0.88} for k, v in rates.iteritems(): xrates.setrate(k, Decimal(v)) def money_converter(amount): """Returns value in USD """ if amount == None: return None if amount[0] == '$': return int(round(float(amount[1:].replace(',', '')))) if amount[0] == '\xe2': #Euro return int(round(float(str(Money(amount[3:].replace(',', ''), 'EUR').to('USD')).replace(',', '')[4:]))) return None
def __init__(self): """ Returns a new instance of the Rates and register this as the new default for currency conversion for *all* calculations. """ xrates.install(self)
def test_register_class(self): self.assertFalse(xrates) self.assertIsNone(xrates.backend_name) xrates.install(SimpleBackend) self.assertEqual(xrates.backend_name, 'SimpleBackend')
def test_register(self): self.assertFalse(xrates) self.assertIsNone(xrates.backend_name) xrates.install('money.exchange.SimpleBackend') self.assertEqual(xrates.backend_name, 'SimpleBackend')