Lab 3

Part 1 dict to XML and back

  1. Creat a working directory week4 and download the source files from week3 Tutorial folder. Import all files into Eclipse.
  2. install dicttoxml and test run "filep6.py"

    • Shift+Right-Click to start command prompt in the directory created
    • type "pip install dicttoxml"
    • type "python filep6.py"
    • open "client.xml" with IE

  3. install xmltodict, print data in JSON, and creat a list of dictionary from client.xml

    • type "pip install xmltodict" in command prompt
    • In Eclipse, copy and paste the following program to create a new module:
      
      import xmltodict
      import json
      import urllib.request
      fileIn=urllib.request.urlopen('http://personal.cityu.edu.hk/~dcywchan/1718SemB121COM/client.xml')
      
      byteStr = fileIn.read()
      lines=byteStr.decode('utf-8')
      dl=xmltodict.parse(lines)
      
      print(json.dumps(dl,indent=4))
          
      newdl=[]
      
      for e in dl['root']['item']:
          tmpdl={}
          for key,value in e.items():
              if key != "@type":
                  value=value["#text"]
                  tmpdl.update({key:value})
                  
          newdl.append(tmpdl)
      
      #print out dictionary list created
      
      
    • complete the programe to see the dictionary list created

Part 2 Individual Assignment

  1. Creat a working directory assignment and download the source files from assignment download link and gradeV0.py from week 4 Tutorial folder. Import all files into Eclipse.

  2. Install and link to class required.

    • type "pip install matplotlib" in command prompt
    • go to Window -> Preferences -> PyDev -> Interpreters -> Python Interpreter
    • click the "New Folder" button under Libraries
    • add the path to folder containing Student

  3. Using reference from matplotlib.org and examples from week 3 tutorial folder, modify the program to create list of students and produce a bar chart of grade distribution as follow.

Part 3 Programming exercises. Answers

  1. Download (i.e. Pull) the following repository from GitHub:

    https://github.com/SCOPEterenceChan/CHK121COMlab3q1SimpleBillingFromXML.

    Dissect and modify the code to add header, block, and inline comment to the program. After each modification test the code still works then version control in your local repository with Git.

  2. Given the above programs to process XML data. Write a program to use daily forex rate in XML format published by the European Central Bank (ECB).

    http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

    The program will prompt user for Currency Code and output today currency rate. For example, when user enters "CNY", the program will print

    "On 2018-02-20 3pm Central European Time, One euro is 7.8290 CNY (CHINA RENMINBI)" as output.

    Special drawing rights (XDR) is not an actual currency. However, it is used to define value for various purposes by organisations like the International Monetary Fund. It is defined as:

    1 XDR = 0.423 Euros + 12.1 Japanese Yen + 0.111 British Pounds + 0.66 US Dollars.

    Your program should support XDR as a currency.

  3. Another way to handle XML in Python is to use the xml.etree.ElementTree in the standard library.

    For example, the following program will print the HKD currency rate using xml.etree.ElementTree

    import xml.etree.ElementTree as ET
    import urllib.request
    
    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)
    
    for element in dataTree[2][0]:
        if element.attrib['currency'] == 'HKD':
            HKDrate = element.attrib['rate']
            
    print("On " + dataTree[2][0].attrib['time'] + " One euro is " + HKDrate + " HKD (Hong Kong Dollar)") 
    
    Rewite program in previous question using xml.etree.ElementTree.