예제 #1
0
 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 = ''
예제 #2
0
 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'))
예제 #3
0
    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'))
예제 #4
0
    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"))
예제 #5
0
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)
예제 #6
0
    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])
예제 #7
0
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)
예제 #8
0
 def setUp(self):
     xrates.install('money.exchange.SimpleBackend')
예제 #9
0
 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)
예제 #10
0
 def test_register_instance(self):
     self.assertFalse(xrates)
     self.assertIsNone(xrates.backend_name)
     xrates.install(SimpleBackend())
     self.assertEqual(xrates.backend_name, "SimpleBackend")
예제 #11
0
 def test_unavailable_rate_conversion_error(self):
     xrates.install("money.exchange.SimpleBackend")
     with self.assertRaises(ExchangeRateNotFound):
         self.MoneyClass("2", "AAA").to("BBB")
예제 #12
0
 def setUp(self):
     xrates.install("money.exchange.SimpleBackend")
예제 #13
0
 def test_unavailable_rate_conversion_error(self):
     xrates.install('money.exchange.SimpleBackend')
     with self.assertRaises(ExchangeRateNotFound):
         self.MoneyClass('2', 'AAA').to('BBB')
예제 #14
0
파일: test_xmoney.py 프로젝트: spoqa/money
 def setUpClass(cls):
     xrates.install('money.exchange.SimpleBackend')
     xrates.base = 'XXX'
     xrates.setrate('AAA', Decimal('2'))
     xrates.setrate('BBB', Decimal('8'))
예제 #15
0
 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)
예제 #16
0
 def test_register_instance(self):
     self.assertFalse(xrates)
     self.assertIsNone(xrates.backend_name)
     xrates.install(SimpleBackend())
     self.assertEqual(xrates.backend_name, 'SimpleBackend')
예제 #17
0
 def test_unavailable_rate_conversion_error(self):
     xrates.install('money.exchange.SimpleBackend')
     with self.assertRaises(ExchangeRateNotFound):
         self.MoneyClass('2', 'AAA').to('BBB')
예제 #18
0
 def test_base_not_set_warning(self):
     xrates.uninstall()
     xrates.install('money.exchange.SimpleBackend')
     with self.assertRaises(Warning):
         xrates.setrate('AAA', Decimal('2'))
예제 #19
0
 def setUpClass(cls):
     xrates.install('money.exchange.SimpleBackend')
     xrates.base = 'XXX'
     xrates.setrate('AAA', Decimal('2'))
     xrates.setrate('BBB', Decimal('8'))
예제 #20
0
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)


예제 #21
0
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
예제 #22
0
 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)
예제 #23
0
 def test_register_class(self):
     self.assertFalse(xrates)
     self.assertIsNone(xrates.backend_name)
     xrates.install(SimpleBackend)
     self.assertEqual(xrates.backend_name, 'SimpleBackend')
예제 #24
0
 def test_register(self):
     self.assertFalse(xrates)
     self.assertIsNone(xrates.backend_name)
     xrates.install('money.exchange.SimpleBackend')
     self.assertEqual(xrates.backend_name, 'SimpleBackend')