[Tutor] Am I making this harder than it needs to be?

Ron Phillips RPhillips at engineer.co.summit.oh.us
Thu May 26 13:22:25 CEST 2005


Ron Phillips wrote:
>> short version: I  need a way to get max and min E and N out of 
>> [(E0,N0),(E1,N1)...(En,Nn)] without reordering the list

>>> Kent Johnson kent37 at tds.net> 5/25/2005 10:19 AM >>
<clip>
>For Python < 2.4 you need another set of [ ] e.g. min([e for e,n in
coordList])
</clip>

I knew I was working too hard!! That's exactly what I needed -- for
some reason, ESRI (big name in geographic work) uses Python 2.2.

 
Ron Phillips wrote:
>> long version:
>>  
>> I would like a list of geographic coordinates (Easting, Northing) to

>> maintain a "bounding box" 
>> [(minEasting,minNorthing),(maxEasting,maxNorthing)] attribute.
>>
>>I did it like this:
>>
>>class geoCoordinateList(UserList):
>>def __init__(self):
>>UserList.__init__(self)
>>self.boundingBox = geoBox(minEN=geoCoordinate(),
>>maxEN=geoCoordinate())
>>def append(self, geoCoord):
>>self.extend([geoCoord])
>>self.boundingBox.challenge(geoCoord)#bumps the max and min if 
>>necessary
>>but I'd have to override almost all the UserList methods, unless
there's 
>>a way to call boundingBox.challenge() on any change to the list.

>If you don't need too much list functionality it might be easier to
wrap a list and delegate the 
>things you need:

>class geoCoordinateList(object):
>def __init__(self):
>self.list = list()
>self.boundingBox = geoBox(minEN=geoCoordinate(),
>maxEN=geoCoordinate())
>def append(self, geoCoord):
>self.list.append(geoCoord) # No need for extend() here...
>self.boundingBox.challenge(geoCoord)#bumps the max and min if
necessary

>I think you will want at least __getitem__() and __len__() as well.

>Kent

Yep, that was my problem; I had "too much list functionality" to
delegate -- your way is much better for the purpose. I can expose that
class without promising all the functionality of a list.
 
In most languages there are a half-dozen stupid, painful ways to do a
job, and maybe one that's merely painful, but in Python there's a
half-dozen "ok" ways to do a job, and at least one that's a real
pleasure!
 
Thanks for the answers!
 
Ron

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050526/4ed2be7a/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Header
Type: application/octet-stream
Size: 472 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20050526/4ed2be7a/Header.obj


More information about the Tutor mailing list