I'm hoping someone can spot what I'm missing here because I cannot figure out what's going wrong. I've written a function that reads a (fake) XML file and loads the information into a global dictionary. It works fine except for the code in red (right at the bottom):
Here's the (fake) XML this function is reading:
And here's the output from the various print statements:
One entry from each CityListInfo section (Pylos and Olympia) works as expected; Name and Info are blank and the code in red triggers successfully. But it seems that all the other city entries have this value changed at the same time, which I don't want. If it were working correctly "Key:" and "Change:" should be the same for each city, and "Name:" and "Info:" should be blank.
Any idea why this is happening and what I can do to fix it? This is the first time I've tried to use my own classes, have I set something wrong there perhaps?
(I realize globals are frowned upon but other functions in this module rely on it. And besides, passing CityDict as an argument had the same problem too.)
Spoiler :
Code:
### Imports
from CvPythonExtensions import *
### Globals
gc = CyGlobalContext()
CityDict = {}
PlayerDict = {}
### Classes
class CityInfo:
def __init__(self):
self.Civilizations = {}
self.Leaders = {}
self.Eras = {}
class CityNameInfo:
def __init__(self):
self.CityName = ""
self.Capital = False
self.Priority = 0
def readCityListInfos(file):
'Loads all CityListInfo entries from a file into the city dictionary'
global CityDict
for line in file.readlines():
# Set up city entry and list of names
if "<CityNames>" in line:
namelist = []
city = CityInfo()
elif "<CityName>" in line:
namelist.append(getXMLString(line))
# Parse Civilization info
elif "<CivilizationType>" in line:
civ = CityNameInfo()
civtype = gc.getInfoTypeForString(getXMLString(line))
elif "<iCivilizationPriority>" in line:
civ.Priority = int(getXMLString(line))
elif "</Civilization>" in line:
city.Civilizations[civtype] = civ
# Parse Leader info
elif "<LeaderType>" in line:
leader = CityNameInfo()
leadertype = gc.getInfoTypeForString(getXMLString(line))
elif "<iLeaderPriority>" in line:
leader.Priority = int(getXMLString(line))
elif "</Leader>" in line:
city.Leaders[leadertype] = leader
# Parse Era info
elif "<EraType>" in line:
era = CityNameInfo()
eratype = gc.getInfoTypeForString(getXMLString(line))
elif "<iEraPriority>" in line:
era.Priority = int(getXMLString(line))
elif "</Era>" in line:
city.Eras[eratype] = era
# Add each city entry to dictionary
elif "</CityListInfo>" in line:
for iName in range(len(namelist)):
CityDict[namelist[iName]] = city
del namelist[:]
for key, info in CityDict.iteritems():
iCiv = 9
print ""
print "Key: " + key
print "Name: " + CityDict[key].Civilizations[iCiv].CityName
print "Info: " + info.Civilizations[iCiv].CityName
[COLOR="Red"]if info.Civilizations[iCiv].CityName == "":
CityDict[key].Civilizations[iCiv].CityName = key[/COLOR]
print "Change: " + CityDict[key].Civilizations[iCiv].CityName
Here's the (fake) XML this function is reading:
Spoiler :
Code:
<Civ4CityListInfos>
<CityListInfos>
<!-- GREECE: Bronze Age Cities -->
<CityListInfo>
<CityNames>
<CityName>Tyrinthe</CityName>
<CityName>Pylos</CityName>
<CityName>Orchomenos</CityName>
</CityNames>
<Civilizations>
<Civilization>
<CivilizationType>CIVILIZATION_GREECE</CivilizationType>
<iCivilizationPriority>1<iCivilizationPriority>
</Civilization>
</Civilizations>
<Leaders>
<Leader>
<LeaderType>LEADER_AGAMEMNON</LeaderType>
<iLeaderPriority>2<iLeaderPriority>
</Leader>
</Leaders>
<Eras>
<Era>
<EraType>ERA_ANCIENT</EraType>
<iEraPriority>1<iEraPriority>
</Era>
<Eras>
</CityListInfo>
<!-- GREECE: Bronze/Iron Age Cities -->
<CityListInfo>
<CityNames>
<CityName>Argos</CityName>
<CityName>Thebae</CityName>
<CityName>Korinth</CityName>
<CityName>Megara</CityName>
<CityName>Helike</CityName>
<CityName>Olympia</CityName>
<CityName>Delphi</CityName>
<CityName>Chalkis</CityName>
<CityName>Eretria</CityName>
</CityNames>
<Civilizations>
<Civilization>
<CivilizationType>CIVILIZATION_GREECE</CivilizationType>
<iCivilizationPriority>2<iCivilizationPriority>
</Civilization>
</Civilizations>
<Leaders/>
<Eras>
<Era>
<EraType>ERA_CLASSICAL</EraType>
<iEraPriority>1<iEraPriority>
</Era>
<Eras>
</CityListInfo>
</CityListInfos>
</Civ4CityListInfos>
And here's the output from the various print statements:
Spoiler :
Code:
Key: Pylos
Name:
Info:
Change: Pylos
Key: Orchomenos
Name: Pylos
Info: Pylos
Key: Tyrinthe
Name: Pylos
Info: Pylos
Key: Olympia
Name:
Info:
Change: Olympia
Key: Chalkis
Name: Olympia
Info: Olympia
Key: Pylos
Name: Pylos
Info: Pylos
Key: Helike
Name: Olympia
Info: Olympia
Key: Thebae
Name: Olympia
Info: Olympia
Key: Argos
Name: Olympia
Info: Olympia
Key: Delphi
Name: Olympia
Info: Olympia
Key: Orchomenos
Name: Pylos
Info: Pylos
Key: Korinth
Name: Olympia
Info: Olympia
Key: Eretria
Name: Olympia
Info: Olympia
Key: Megara
Name: Olympia
Info: Olympia
Key: Tyrinthe
Name: Pylos
Info: Pylos
One entry from each CityListInfo section (Pylos and Olympia) works as expected; Name and Info are blank and the code in red triggers successfully. But it seems that all the other city entries have this value changed at the same time, which I don't want. If it were working correctly "Key:" and "Change:" should be the same for each city, and "Name:" and "Info:" should be blank.
Any idea why this is happening and what I can do to fix it? This is the first time I've tried to use my own classes, have I set something wrong there perhaps?
(I realize globals are frowned upon but other functions in this module rely on it. And besides, passing CityDict as an argument had the same problem too.)