Large iteration

OrionVeteran

Deity
Joined
Dec 25, 2003
Messages
2,443
Location
Newport News VA
I want to improve my iteration functions by utilizing one large standard data script. Problem is my separators are not working. How can I fix this?

Code:
lReligionData = {
		"RELIGION_JUDAISM": "BUILDING_JEWISH_MONASTERY": "BUILDING_JEWISH_TEMPLE": "BUILDING_JEWISH_SHRINE": "BUILDING_JEWISH_HOLY_OFFICE": "UNIT_JEWISH_MISSIONARY": "UNIT_JEWISH_INQUISITOR",
		"RELIGION_CHRISTIANITY": "BUILDING_CHRISTIAN_MONASTERY": "BUILDING_CHRISTIAN_TEMPLE": "BUILDING_CHRISTIAN_SHRINE": "BUILDING_CHRISTIAN_HOLY_OFFICE": "UNIT_CHRISTIAN_MISSIONARY": "UNIT_CHRISTIAN_INQUISITOR",
		"RELIGION_ISLAM": "BUILDING_ISLAMIC_MONASTERY": "BUILDING_ISLAMIC_TEMPLE": "BUILDING_ISLAMIC_SHRINE": "BUILDING_ISLAMIC_HOLY_OFFICE": "UNIT_ISLAMIC_MISSIONARY": "UNIT_ISLAMIC_INQUISITOR",
		"RELIGION_HINDUISM": "BUILDING_HINDU_MONASTERY": "BUILDING_HINDU_TEMPLE": "BUILDING_HINDU_SHRINE": "BUILDING_HINDU_HOLY_OFFICE": "UNIT_HINDU_MISSIONARY": "UNIT_HINDU_INQUISITOR",
		"RELIGION_BUDDHISM": "BUILDING_BUDDHIST_MONASTERY": "BUILDING_BUDDHIST_TEMPLE": "BUILDING_BUDDHIST_SHRINE": "BUILDING_BUDDHIST_HOLY_OFFICE": "UNIT_BUDDHIST_MISSIONARY": "UNIT_BUDDHIST_INQUISITOR",
		"RELIGION_CONFUCIANISM": "BUILDING_CONFUCIAN_MONASTERY": "BUILDING_CONFUCIAN_TEMPLE": "BUILDING_CONFUCIAN_SHRINE": "BUILDING_CONFUCIAN_HOLY_OFFICE": "UNIT_CONFUCIAN_MISSIONARY": "UNIT_CONFUCIAN_INQUISITOR",
		"RELIGION_TAOISM": "BUILDING_TAOIST_MONASTERY": "BUILDING_TAOIST_TEMPLE": "BUILDING_TAOIST_SHRINE": "BUILDING_TAOIST_HOLY_OFFICE": "UNIT_TAOIST_MISSIONARY": "UNIT_TAOIST_INQUISITOR"
	}

Example Function:

Code:
def getReligionShrine(iStateReligion):
	# Orion's Inquisition Mod
	# Returns the Player's State Religion Shrine Name
	rShrine = -1
	
	for szReligion, iMonastery, iTemple, iShrine, iHolyOffice, iMissionary, iInquisitor in lReligionData.iteritems():
		iReligion = gc.getInfoTypeForString(szReligion)
		if iReligion == iStateReligion:
			rShrine = str(iShrine)
			break
		
	return str(iShrine)


Orion Veteran :cool:
 
I want to improve my iteration functions by utilizing one large standard data script. Problem is my separators are not working. How can I fix this?

Code:
lReligionData = {
		"RELIGION_JUDAISM": "BUILDING_JEWISH_MONASTERY": "BUILDING_JEWISH_TEMPLE": "BUILDING_JEWISH_SHRINE": "BUILDING_JEWISH_HOLY_OFFICE": "UNIT_JEWISH_MISSIONARY": "UNIT_JEWISH_INQUISITOR",
		"RELIGION_CHRISTIANITY": "BUILDING_CHRISTIAN_MONASTERY": "BUILDING_CHRISTIAN_TEMPLE": "BUILDING_CHRISTIAN_SHRINE": "BUILDING_CHRISTIAN_HOLY_OFFICE": "UNIT_CHRISTIAN_MISSIONARY": "UNIT_CHRISTIAN_INQUISITOR",
		"RELIGION_ISLAM": "BUILDING_ISLAMIC_MONASTERY": "BUILDING_ISLAMIC_TEMPLE": "BUILDING_ISLAMIC_SHRINE": "BUILDING_ISLAMIC_HOLY_OFFICE": "UNIT_ISLAMIC_MISSIONARY": "UNIT_ISLAMIC_INQUISITOR",
		"RELIGION_HINDUISM": "BUILDING_HINDU_MONASTERY": "BUILDING_HINDU_TEMPLE": "BUILDING_HINDU_SHRINE": "BUILDING_HINDU_HOLY_OFFICE": "UNIT_HINDU_MISSIONARY": "UNIT_HINDU_INQUISITOR",
		"RELIGION_BUDDHISM": "BUILDING_BUDDHIST_MONASTERY": "BUILDING_BUDDHIST_TEMPLE": "BUILDING_BUDDHIST_SHRINE": "BUILDING_BUDDHIST_HOLY_OFFICE": "UNIT_BUDDHIST_MISSIONARY": "UNIT_BUDDHIST_INQUISITOR",
		"RELIGION_CONFUCIANISM": "BUILDING_CONFUCIAN_MONASTERY": "BUILDING_CONFUCIAN_TEMPLE": "BUILDING_CONFUCIAN_SHRINE": "BUILDING_CONFUCIAN_HOLY_OFFICE": "UNIT_CONFUCIAN_MISSIONARY": "UNIT_CONFUCIAN_INQUISITOR",
		"RELIGION_TAOISM": "BUILDING_TAOIST_MONASTERY": "BUILDING_TAOIST_TEMPLE": "BUILDING_TAOIST_SHRINE": "BUILDING_TAOIST_HOLY_OFFICE": "UNIT_TAOIST_MISSIONARY": "UNIT_TAOIST_INQUISITOR"
	}

The ":" separator is used for a dictionary and there can only be one per entry.
I would suggest making a dictionary where the value is a list. Perhaps something like this (I changed the prefix from "l" to "d" to indicate "dictionary"):
Code:
dReligionData = {
		"RELIGION_JUDAISM": ("BUILDING_JEWISH_MONASTERY", "BUILDING_JEWISH_TEMPLE", "BUILDING_JEWISH_SHRINE", "BUILDING_JEWISH_HOLY_OFFICE", "UNIT_JEWISH_MISSIONARY", "UNIT_JEWISH_INQUISITOR"),
		"RELIGION_CHRISTIANITY": ("BUILDING_CHRISTIAN_MONASTERY", "BUILDING_CHRISTIAN_TEMPLE", "BUILDING_CHRISTIAN_SHRINE", "BUILDING_CHRISTIAN_HOLY_OFFICE", "UNIT_CHRISTIAN_MISSIONARY", "UNIT_CHRISTIAN_INQUISITOR"),
		"RELIGION_ISLAM": ("BUILDING_ISLAMIC_MONASTERY", "BUILDING_ISLAMIC_TEMPLE", "BUILDING_ISLAMIC_SHRINE", "BUILDING_ISLAMIC_HOLY_OFFICE", "UNIT_ISLAMIC_MISSIONARY", "UNIT_ISLAMIC_INQUISITOR"),
		"RELIGION_HINDUISM": ("BUILDING_HINDU_MONASTERY", "BUILDING_HINDU_TEMPLE", "BUILDING_HINDU_SHRINE", "BUILDING_HINDU_HOLY_OFFICE", "UNIT_HINDU_MISSIONARY", "UNIT_HINDU_INQUISITOR"),
		"RELIGION_BUDDHISM": ("BUILDING_BUDDHIST_MONASTERY", "BUILDING_BUDDHIST_TEMPLE", "BUILDING_BUDDHIST_SHRINE", "BUILDING_BUDDHIST_HOLY_OFFICE", "UNIT_BUDDHIST_MISSIONARY", "UNIT_BUDDHIST_INQUISITOR"),
		"RELIGION_CONFUCIANISM": ("BUILDING_CONFUCIAN_MONASTERY", "BUILDING_CONFUCIAN_TEMPLE", "BUILDING_CONFUCIAN_SHRINE", "BUILDING_CONFUCIAN_HOLY_OFFICE", "UNIT_CONFUCIAN_MISSIONARY", "UNIT_CONFUCIAN_INQUISITOR"),
		"RELIGION_TAOISM": ("BUILDING_TAOIST_MONASTERY", "BUILDING_TAOIST_TEMPLE", "BUILDING_TAOIST_SHRINE", "BUILDING_TAOIST_HOLY_OFFICE", "UNIT_TAOIST_MISSIONARY", "UNIT_TAOIST_INQUISITOR")
	}

Example Function:

Code:
def getReligionShrine(iStateReligion):
	# Orion's Inquisition Mod
	# Returns the Player's State Religion Shrine Name
	rShrine = -1
	
	for szReligion, iMonastery, iTemple, iShrine, iHolyOffice, iMissionary, iInquisitor in lReligionData.iteritems():
		iReligion = gc.getInfoTypeForString(szReligion)
		if iReligion == iStateReligion:
			rShrine = str(iShrine)
			break
		
	return str(iShrine)


Orion Veteran :cool:

I would then do this differently. Why loop to find it when it is already a dictionary?
Code:
def getReligionShrine(iStateReligion):

	pReligion = gc.getReligionInfo(iStateReligion)

	iMonastery, iTemple, iShrine, iHolyOffice, iMissionary, iInquisitor = dReligionData[pReligion.getType()]

	return iShrine

Note that I havn't tried the above code, but it may actually work anyway (and should be pretty close if it doesn't). Note that there is no error checking to insure that pReligion was set or anything else.
 
I would then do this differently. Why loop to find it when it is already a dictionary?
Code:
def getReligionShrine(iStateReligion):

	pReligion = gc.getReligionInfo(iStateReligion)

	iMonastery, iTemple, iShrine, iHolyOffice, iMissionary, iInquisitor = dReligionData[pReligion.getType()]

	return iShrine

Note that I havn't tried the above code, but it may actually work anyway (and should be pretty close if it doesn't). Note that there is no error checking to insure that pReligion was set or anything else.

The recommended function fails on this line:

Code:
iMonastery, iTemple, iShrine, iHolyOffice, iMissionary, iInquisitor = dReligionData[pReligion.getType()]

Error: 'NoneType' object has no attribute 'getType'

Orion Veteran :cool:
 
The recommended function fails on this line:

Code:
iMonastery, iTemple, iShrine, iHolyOffice, iMissionary, iInquisitor = dReligionData[pReligion.getType()]

Error: 'NoneType' object has no attribute 'getType'

Orion Veteran :cool:

That would seem to indicate that pReligion is not being set to a valid CvReligionInfo. Since a CvReligionInfo class object certainly has a getType method, if it says it doesn't then the most likely explanation is that the assignment didn't work as expected so the call to gc.getReligionInfo failed so instead of a CvReligionInfo pReligion is either nothing or the integer -1 (as seems to be the error indicator of choice in Civ).

Two reasons for this to fail come to mind:

1) Is this function "stand-alone" or being added to a class?

If this is added to a class, then you will need to add a "self" argument before the "iStateReligion" argument. This would explain why it could not set pReligion, as the first argument is the "self" data, not the data you passed when you called the function.

If this is not the problem, then...

2) When you call this function, what data are you passing to it in iStateReligion?

Currently it's expecting a number, like you get from gc.getInfoTypeForString() or that sort of thing. If you are passing something else, like the actual string "RELIGION_JUDAISM" for example, then it wouldn't work as-is.

Actually, if you are passing such a string it would be easier since you could just use the passed string as the key into the dictionary directly, skipping the pReligion stuff. Since your original version had "iReligion = gc.getInfoTypeForString(szReligion)" , the right hand side of which is getting the integer defined to match the string, and you then compared this to see if it was the same as what was passed I assumed you were passing a ReligionType integer (and iReligion has the "i" prefix, too).

(By the way, I forgot to change the names of the variables like "iShrine", the "i" prefix is used to indicate that you expect it to be an integer, but these are text strings. It doesn't matter in terms of how it works, but it could confuse somebody who is trying to figure out what it is doing.)
 
I tried various different changes to try and get your function to work, but no luck. However, My "for" function works perfectly. So for now, I will stick with what works. Here is an example for the Monastery:

Code:
def getReligionMonastery(iStateReligion):
	# Orion's Inquisition Mod
	# Returns the Religion Monastery for the specified State Religion
	
	for szReligion, (iMonastery, iTemple, iShrine, iHolyOffice, iMissionary, iInquisitor) in dReligionData.iteritems():
		iReligion = gc.getInfoTypeForString(szReligion)
		if iReligion == iStateReligion:
			break
	
	#CyInterface().addMessage(CyGame().getActivePlayer(),True,25,str(iMonastery),'AS2D_DISCOVERBONUS',1,'Art/Interface/Buttons/TerrainFeatures/Forest.dds',ColorTypes(8),0,0,False,False)
	return str(iMonastery)

Thanks for your assistance,

Sincerely,

Orion Veteran :cool:
 
Top Bottom