def get_data(): return {'default': prop.get_currency_default(), 'selected': get_selected_currency(), 'all': prop.get_currencies(), 'rates': get_rates()}
def get_rates(): xml_string = None try: ro = fetch('http://www.bnr.ro/nbrfxrates.xml') if int(ro.status_code) < 400: xml_string = ro.content except Exception, e: logging.warn('Couldn\'t load currency rates', e) if xml_string is None: xml_string = rates_string tree = etree.fromstring(xml_string) rates = {} for rate in tree.findall('.//*[@currency]'): cur_name = rate.attrib['currency'] if cur_name in prop.get_currencies(): rates[cur_name] = { 'val': float(rate.text), 'multiplier': int(rate.attrib['multiplier']) if 'multiplier' in rate.attrib else 1 } rates[prop.get_currency_default()] = {'val': 1, 'multiplier': 1} return rates def convert(val, currency=None, rates=None): currency = currency if currency is not None else get_selected_currency() rates = rates if rates is not None else get_rates() rate = rates[currency]