On Wed, 28 Sep 2005, Ron Phillips wrote:
> Maybe it's just me. I am always haunted by "coder's remorse" (the > certainty that there's a more compact, beautiful, fun, maintainable way > to code anything I finish.) > > At any rate, I am currently working on a script to model an ordered > collection of geographic points, all of which must share the same > collection of attributes. So, a little collection might be: > > pointList = [ > {lat:40.123,lon:-81.456, > 'attributes':{'msg':'example','beavers':34, 'distance':18.132} > }, > {lat:40.12345,lon:-81.45678, > 'attributes':{'msg':'','beavers':0, 'distance':0.0} > } > ] Hi Ron, You may want to change the data structure. Rather than have points all be peers, reorganize them so that there's some kind of hierarchy: that'll help you directly represent the "sharing" of attributes. Explicitly: ######################################################### class Point: def __init__(self, lat, lon, atts, parent=None): (self.lat, self.lon, self.atts, self.parent) = ( lat, long, atts, parent) def lookup(self, name): if name in self.atts: return self.atts[name] if self.parent: return self.parent.lookup(name) raise KeyError ######################################################### The idea here is that we impose some structure among the points. We can keep a central Point that has a set of child points within its satellite. ###### >>> centralPoint = Point(40, -81, {}) >>> satellitePoint = Point(40, -81, {}, centralPoint) ###### Once we formalize this hierarchical relationship --- satellitePoint's "parent" is centralPoint --- then we can test out how attribute lookup and sharing might work: ###### >>> satellitePoint = Point(40, -81, {}, centralPoint) >>> centralPoint.atts['color'] = 'purple' >>> satellitePoint.lookup('color') 'purple' ###### Does this make sense? _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor