Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from catalogue import CountryCatalogue
- def processUpdates(cntryFileName, UpdatesName):
- shouldContinue = True
- countryDataname = cntryFileName
- UpdatesName = UpdatesName
- while shouldContinue:
- isThereFile = False
- try:
- countryData = open(countryDataname, "r")
- isThereFile = True
- except FileNotFoundError:
- print("Country file not found, Would you like to try with a new filename? Yes(Y) or No(N)")
- if not isThereFile:
- userChoice = input()
- if userChoice != "Y":
- outputUnsuccesfulUpdate()
- return False
- if userChoice == "Y":
- countryDataname = input("Enter new filename to use(with extension)\n")
- else:
- print("Invalid input")
- if isThereFile:
- catalogue = CountryCatalogue(countryData)
- updateisThereFile = False
- #opening updata data file
- try:
- Updates = open(UpdatesName, "r")
- updateisThereFile = True
- except FileNotFoundError:
- print("Update file not found, Would you like to try with a new filename? Yes(Y) or No(N)")
- if not updateisThereFile:
- userChoice = input()
- if userChoice != "Y":
- outputUnsuccesfulUpdate()
- countryData.close()
- return False
- if userChoice == "Y":
- UpdatesName = input("Enter new filename to use(with extension)\n")
- else:
- print("Invalid input")
- if updateisThereFile:
- listOfUpdates = extractUpdate(Updates)
- for item in listOfUpdates:
- countryName = item[0]
- if countryName in catalogue.getCountryCatalogueDictionary():
- if item[1] != "":
- catalogue.getCountryCatalogueDictionary()[countryName].setPopulation(item[1])
- if item[2] != "":
- catalogue.getCountryCatalogueDictionary()[countryName].setArea(item[2])
- if item[3] != "":
- catalogue.getCountryCatalogueDictionary()[countryName].setContinent(item[3])
- else:
- catalogue.addCountry(countryName, item[1], item[2], item[3])
- countriesCount = catalogue.saveCountryCatalogue("output.txt")
- countryData.close()
- Updates.close()
- return True
- def extractUpdate(Updates):
- listOfUpdates = []
- for line in Updates:
- countryName = ""
- newPop = ""
- newArea = ""
- newCont = ""
- fieldsList = line.split(";")
- fieldsListWithoutSpaces = []
- for field in fieldsList:
- fieldsListWithoutSpaces.append(field.strip(" \n"))
- countryName = fieldsList[0]
- for field in fieldsListWithoutSpaces:
- if field[0] == "P" and field[1] == "=":
- newPop = field[2 : ]
- if field[0] == "A" and field[1] == "=":
- newArea = field[2 : ]
- if field[0] == "C" and field[1] == "=":
- newCont = field[2 : ]
- listOfUpdates.append((countryName, newPop, newArea, newCont))
- return listOfUpdates
- #Unsuccessful output
- def outputUnsuccesfulUpdate():
- file = open("output.txt", "w")
- file.write("Update Unsuccessful\n")
- file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement