Assignment 2 with Jim O'Leary
#!/usr/bin/env python
# imports the string library
import string
import mods.ass2methods
M = mods.ass2methods
# initializes the dictionary myDict
myDict = {}
# opens the file northwind.xml for reading
lstLines = M.fcnopen('northwind.xml')
# strings that we are looking for
lookFor1 = "productname"
lookFor2 = "unitprice"
# loops through the lines of northwind.xml
for line in lstLines:
# find the first occurrence of the string
foundIndex1 = M.fcnsearch(line,lookFor1)
foundIndex2 = M.fcnsearch(line,lookFor2)
# if productname is found
if (foundIndex1 > -1):
productname=M.fcnfind(line,foundIndex1)
# if unitprice is found
elif (foundIndex2 > -1):
unitprice=M.fcnfind(line,foundIndex2)
# assuming that the xml is well-formed, always a productname before unitprice
# add the values to myDict
myDict[productname] = unitprice
# loop through the dictionary printing out keys and values
for key, value in myDict.items():
print "key is", key, "value is",value
'''
Audrey: I like your fcnsearch and fcnfind methods. They manage to look
for both productname and unitprice, depending on the parameters. This
is the whole idea of functions, resusable code. Great work!
Here are some suggestions for improvement:
1. Consider putting your display work in a function too. In a real
application, you might want to format the result.
2. For your fcnopen method, just have it open the path that the caller
gives to it. Right now it starts in the data directory. It is up
to the caller to enter the path to the file.
3. Consider making your variables names more understandable. I don't
know what "fcn" refers to, and when I first look at the code, it
delays my understanding of what is going on.
10/10
Jim
'''
Recent comments
22 weeks 3 days ago
22 weeks 3 days ago
22 weeks 3 days ago
22 weeks 3 days ago
22 weeks 3 days ago
22 weeks 4 days ago
22 weeks 4 days ago
22 weeks 4 days ago
22 weeks 4 days ago
22 weeks 4 days ago