comparison pyikriam/example.py @ 177:6adad3bcca78

Refactory to functions for sychronizing object attributes with pages. - We add more building types. - All of them need to sync attributes with content of Ikariam's online page. - These functions are refactoried to functions.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 15:14:51 +0800
parents 3ba3edda6d1e
children bf4ddf5bffb9
comparison
equal deleted inserted replaced
176:3ba3edda6d1e 177:6adad3bcca78
1 import sys 1 import sys
2 import buildings
2 from ikariam import Ikariam 3 from ikariam import Ikariam
3 4
4 if len(sys.argv) != 2: 5 if len(sys.argv) != 2:
5 print >> sys.stderr, 'Usage: %s <city id>' % (sys.argv[0]) 6 print >> sys.stderr, 'Usage: %s <city id>' % (sys.argv[0])
6 sys.exit(1) 7 sys.exit(1)
21 print 'marble is ' + city.marble 22 print 'marble is ' + city.marble
22 print 'crystal is ' + city.crystal 23 print 'crystal is ' + city.crystal
23 print 'sulfur is ' + city.sulfur 24 print 'sulfur is ' + city.sulfur
24 print 'positions ' + repr(city.positions) 25 print 'positions ' + repr(city.positions)
25 26
26 city.positions[0].sync() 27 for idx, pos in enumerate(city.positions):
27 city_attrs = ('level', 'occupied', 'rooms', 'growth', 'happiness', 28 if not isinstance(pos, buildings.position):
28 'interest_base', 'interest_research', 'interest_capital', 29 continue
29 'pop_citizens', 'pop_woodworkers', 'pop_specialworkers', 30 pos.sync()
30 'pop_scientists', 'is_upgrading', 'upgrade_uri') 31 building_attrs = filter(lambda attr: not attr[0].startswith('_'),
31 for city_attr in city_attrs: 32 pos.__dict__.items())
32 value = getattr(city.positions[0], city_attr) 33 building_attrs.sort(key=lambda x: x[0])
33 print 'positions[0].%s is %s' % (city_attr, str(value)) 34 print
34 pass 35 print 'positions[%d]' % (idx)
36 for building_attr, value in building_attrs:
37 print '\t%s: %s' % (building_attr, repr(value))
38 pass