Lab 3

Part 2 Individual Assignment
Part 3 Programming Exercise
  1. lab3q2.py
    
    import xmltodict
    import urllib.request
    
    userCurrency = input('Enter Currency Code : ')
    
    fileIn=urllib.request.urlopen('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')
    byteStr = fileIn.read()
    lines=byteStr.decode('utf-8')
    dl=xmltodict.parse(lines)
    
    if userCurrency == 'XDR':
        for e in dl['gesmes:Envelope']['Cube']['Cube']['Cube']:
            if e.get('@currency') == 'JPY':
                jpyRate = e.get('@rate')
                
            if e.get('@currency') == 'CNY':
                cnyRate = e.get('@rate')
                    
            if e.get('@currency') == 'GBP':
                gbpRate = e.get('@rate')
            
            if e.get('@currency') == 'USD':
                usaRate = e.get('@rate')
                    
        userCurrencyRate = "{:.4f}".format(1/(0.38671 + 11.9 / float(jpyRate) + 1.0174 / float(cnyRate) + 
                               0.085946 / float(gbpRate) + 0.58252 / float(usaRate)))
    else:           
        for e in dl['gesmes:Envelope']['Cube']['Cube']['Cube']:
            if e.get('@currency') == userCurrency:
                userCurrencyRate = e.get('@rate')
           
    print("On " + dl['gesmes:Envelope']['Cube']['Cube']['@time'] + 
          " One euro is " + userCurrencyRate + ' ' + userCurrency)
    
    
  2. lab3q3.py
    
    import xml.etree.ElementTree as ET
    import urllib.request
    
    userCurrency = input('Enter Currency Code : ')
    
    fileIn = urllib.request.urlopen("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
    byteStr  = fileIn.read()
    dataStr = byteStr.decode('utf-8')
    dataTree = ET.fromstring(dataStr)
    
    if userCurrency == 'XDR':
        for element in dataTree[2][0]:
            if element.attrib['currency'] == 'JPY':
                jpyRate = element.attrib['rate']
                
            if element.attrib['currency'] == 'CNY':
                cnyRate = element.attrib['rate']
                    
            if element.attrib['currency'] == 'GBP':
                gbpRate = element.attrib['rate']
            
            if element.attrib['currency'] == 'USD':
                usaRate = element.attrib['rate']
                    
        userCurrencyRate = "{:.4f}".format(1/(0.38671 + 11.9 / float(jpyRate) + 1.0174 / float(cnyRate) + 
                               0.085946 / float(gbpRate) + 0.58252 / float(usaRate)))
    else:
        for element in dataTree[2][0]:
            if element.attrib['currency'] == userCurrency:
                userCurrencyRate = element.attrib['rate']
             
    print("On " + dataTree[2][0].attrib['time'] + " One euro is " + userCurrencyRate + ' ' + userCurrency)