The method numberToAlpha in class CvMercenaryManager is not safe. Or more precisely, there is a potential risk in the way you use it.
For example, you do check whether unit.objUnit is None, before you pass its id to numberToAlpha.
Unfortunately this check is not enough.
Under some circumstances, Civ4 program can create a CyUnit object. It is valid from perspective of Python, but it does not represent any CvUnit. Namely, from perspective of C++, this object is not NULL, but its member, m_pUnit, is NULL.
In this case, unit.objUnit.getID() yields -1. If you pass -1 to numberToAlpha, numberToAlpha will throw an error like "ValueError: invalid literal for int(): -".
Thus, in my opinion, a safer way is:
Code:
def numberToAlpha(self, iNum):
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
alphaList = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
strNum = str(iNum)
strAlpha = ""
# Go though the alphaList and convert the numbers to letters
for i in range (len(strNum)):
strAlpha = strAlpha + alphaList[int(strNum[i])]
return strAlpha
For example, you do check whether unit.objUnit is None, before you pass its id to numberToAlpha.
Code:
if(unit.objUnit != None):
unitID = unitName + "-" + self.numberToAlpha(unit.objUnit.getID())
Unfortunately this check is not enough.
Under some circumstances, Civ4 program can create a CyUnit object. It is valid from perspective of Python, but it does not represent any CvUnit. Namely, from perspective of C++, this object is not NULL, but its member, m_pUnit, is NULL.
Code:
int CyUnit::getID()
{
return m_pUnit ? m_pUnit->getID() : -1;
}
In this case, unit.objUnit.getID() yields -1. If you pass -1 to numberToAlpha, numberToAlpha will throw an error like "ValueError: invalid literal for int(): -".
Thus, in my opinion, a safer way is:
Code:
if(unit.objUnit is not None and not unit.objUnit.isNone()):
unitID = unitName + "-" + self.numberToAlpha(unit.objUnit.getID())