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

Kent Johnson kent37 at tds.net
Wed May 25 16:19:39 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

not sure if your coordinates are strings like 'E0' or if that is a placeholder for an int. If they 
are ints you can use
minE = min(e for e,n in coordList)

If they are strings you have to convert
minE = min(int(e[1:]) for e,n in coordList)
etc

For Python < 2.4 you need another set of [ ] e.g. min([e for e,n in coordList])

> 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



More information about the Tutor mailing list