Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. from catalogue import CountryCatalogue
  2.  
  3. def processUpdates(cntryFileName, UpdatesName):
  4.  
  5. shouldContinue = True
  6.  
  7. countryDataname = cntryFileName
  8. UpdatesName = UpdatesName
  9.  
  10. while shouldContinue:
  11.  
  12. isThereFile = False
  13.  
  14. try:
  15. countryData = open(countryDataname, "r")
  16. isThereFile = True
  17. except FileNotFoundError:
  18. print("Country file not found, Would you like to try with a new filename? Yes(Y) or No(N)")
  19.  
  20. if not isThereFile:
  21. userChoice = input()
  22.  
  23. if userChoice != "Y":
  24.  
  25. outputUnsuccesfulUpdate()
  26. return False
  27.  
  28. if userChoice == "Y":
  29.  
  30. countryDataname = input("Enter new filename to use(with extension)\n")
  31.  
  32. else:
  33.  
  34. print("Invalid input")
  35.  
  36. if isThereFile:
  37.  
  38. catalogue = CountryCatalogue(countryData)
  39.  
  40. updateisThereFile = False
  41.  
  42. #opening updata data file
  43. try:
  44. Updates = open(UpdatesName, "r")
  45. updateisThereFile = True
  46. except FileNotFoundError:
  47. print("Update file not found, Would you like to try with a new filename? Yes(Y) or No(N)")
  48.  
  49.  
  50. if not updateisThereFile:
  51.  
  52. userChoice = input()
  53.  
  54. if userChoice != "Y":
  55.  
  56. outputUnsuccesfulUpdate()
  57. countryData.close()
  58. return False
  59.  
  60. if userChoice == "Y":
  61.  
  62. UpdatesName = input("Enter new filename to use(with extension)\n")
  63.  
  64. else:
  65. print("Invalid input")
  66.  
  67. if updateisThereFile:
  68.  
  69. listOfUpdates = extractUpdate(Updates)
  70.  
  71. for item in listOfUpdates:
  72.  
  73. countryName = item[0]
  74.  
  75. if countryName in catalogue.getCountryCatalogueDictionary():
  76. if item[1] != "":
  77. catalogue.getCountryCatalogueDictionary()[countryName].setPopulation(item[1])
  78. if item[2] != "":
  79. catalogue.getCountryCatalogueDictionary()[countryName].setArea(item[2])
  80. if item[3] != "":
  81. catalogue.getCountryCatalogueDictionary()[countryName].setContinent(item[3])
  82.  
  83. else:
  84. catalogue.addCountry(countryName, item[1], item[2], item[3])
  85.  
  86. countriesCount = catalogue.saveCountryCatalogue("output.txt")
  87.  
  88.  
  89. countryData.close()
  90. Updates.close()
  91.  
  92.  
  93. return True
  94.  
  95. def extractUpdate(Updates):
  96.  
  97. listOfUpdates = []
  98.  
  99. for line in Updates:
  100.  
  101. countryName = ""
  102. newPop = ""
  103. newArea = ""
  104. newCont = ""
  105.  
  106. fieldsList = line.split(";")
  107.  
  108. fieldsListWithoutSpaces = []
  109.  
  110. for field in fieldsList:
  111. fieldsListWithoutSpaces.append(field.strip(" \n"))
  112.  
  113. countryName = fieldsList[0]
  114.  
  115. for field in fieldsListWithoutSpaces:
  116.  
  117. if field[0] == "P" and field[1] == "=":
  118. newPop = field[2 : ]
  119. if field[0] == "A" and field[1] == "=":
  120. newArea = field[2 : ]
  121. if field[0] == "C" and field[1] == "=":
  122. newCont = field[2 : ]
  123.  
  124.  
  125. listOfUpdates.append((countryName, newPop, newArea, newCont))
  126.  
  127. return listOfUpdates
  128.  
  129. #Unsuccessful output
  130. def outputUnsuccesfulUpdate():
  131.  
  132. file = open("output.txt", "w")
  133.  
  134. file.write("Update Unsuccessful\n")
  135.  
  136. file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement