D41d8cd98f00b204e9800998ecf8427e

I am mapping objects (countries for diplomacy) from confobject to objects.
It looks like this:

[countries]
[[abc]]
related = def,
[[def]]
related = abc,ghi,
[[ghi]]
related = def,

Which looks like [1] in python:

I am currently using [2] but it just seems horrible.
self.countries is a list of Country objects. Country Objects have a list pointing to related Countrys.

[1]

{
    'abc': {'related': ['def']},
    'def': {'related': ['abc', 'ghi']}, 
    'ghi': {'related': ['def']}
}        

[2]

        for basecountry in self.countries:
            for country in countriesdict:
                relateddict = countriesdict[country]
                for related in relateddict:
                    relatedlist = relateddict[related]
                    for relatedcountry in relatedlist:
                        if basecountry.name == country:
                            for possiblecountry in self.countries:
                                if possiblecountry.name == relatedcountry:
                                    basecountry.relatedCountries.append(possiblecountry)

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

Anonymous

June 2, 2009, June 02, 2009 20:09, permalink

No rating. Login to rate!

Can you provide a minimal example that will run? I'm getting kind of confused by what stuff is implied to exist.

D41d8cd98f00b204e9800998ecf8427e

hyposaurus

July 3, 2009, July 03, 2009 05:24, permalink

No rating. Login to rate!

this should run, you will need Confobject and the validator that goes with it

from os import sys
from configobj import ConfigObj
from validate import Validator

from Country import Country

class Map():
''' Map is responsible for holding the state of the game.
This includes loading and saving the configuration file'''

def __init__(self):
self.name = ''
self.countries = []

def loadCountries(self, countriesdict):

countries = []

for country in countriesdict:
countries.append(Country(country))

for basecountry in countries:
for country in countriesdict:
relateddict = countriesdict[country]
for related in relateddict:
relatedlist = relateddict[related]
for relatedcountry in relatedlist:
if basecountry.name == country:
for possiblecountry in countries:
if possiblecountry.name == relatedcountry:
basecountry.relatedCountries.append(possiblecountry)
return countries

def loadMapFromFile(self, mapToLoad):
validator = Validator()
config = ConfigObj('maps/' + mapToLoad + '.txt')# configspec='configs/mapconf.ini')
valid = True #config.validate(validator)
print 'Validating game file.'

if valid is False:
print 'The configuration file is really broken: nothing matches against validation.',
print 'Try again.'
sys.exit(0)

if type(valid) is dict:
print 'Game file does not appear valid.'
for entry in valid:
if valid[entry] == False:
print '"%s" appears malformed.' % entry
sys.exit(0)
else:
print 'Game file appears valid. Continuing.'

self.name = config['mapname']

print config['countries']

self.countries = self.loadCountries(config['countries'])

def addCountryToMap(self, countryToAdd):
self.countries.append(countryToAdd)

if __name__ == '__main__':
game = Map()
game.loadMapFromFile('gaia')

for country in game.countries:
print country, ':',
for name in country.relatedCountries:
print name,
print ''

class Country():
def __init__(self, name):
self.name = name
self.relatedCountries = []
self.unitsInCountry = []
self.ownedBy = None

def setRelated(self, country):
self.relatedCountries.append(country)

def isRelated(self, Country):
if Country in self.relatedCountries:
return True
return False

def addUnitToCountry(self, unit):
self.unitsInCountry.append(unit)

def removeUnitFromCountry(self, unit):
self.unitsInCountry.remove(unit)

def getUnitsInCountry(self):
return self.unitsInCountry

def isUnitInCountry(self, unit):
if unit in self.unitsInCountry:
return True
return False

def __str__(self):
return self.name

if __name__ == '__main__':
pass

mapname = gaia

[countries]
[[abc]]
related = def,
[[def]]
related = abc,ghi,
[[ghi]]
related = def,jkl
[[jkl]]
related = ghi,

[players]
[player1]
countries = abc, def
armies = abd, def,
fleets = ,
[player2]
countries = ghi,
armies = ghi,
fleets = ,

Your refactoring





Format Copy from initial code

or Cancel