[Tutor] Encoding and Decoding
Carlos
carloslara at web.de
Tue Jan 2 21:07:55 CET 2007
Kent,
Will give this a try.
Thanks for your help,
Carlos
Kent Johnson wrote:
> OK, off the top of my head (not tested) here are some things to get
> you started.
>
> You could write a function that would retrieve a coordinate value
> given an index number, for example:
> def getCoord(data, ix):
> base = ix*4
> value = data[ix]*10 + data[ix+1] + data[ix+2]/10.0
> if data[ix+3] < 5:
> value = -value
> return value
>
> Now if data is your big list, you can write getCoord(data, 5) to get
> the value stored at data[20] to data[23]. Similarly you could write a
> setter and maybe a getXY() function that returns a pair (x, y). So
> that is a place to start.
>
> If you want to avoid passing the list around it might make sense to
> make a class to hold it. Then you would have something like
> class Data(object):
> def __init__(self, lst):
> self.data = lst
>
> def getCoord(self, ix):
> base = ix*4
> value = self.data[ix]*10 + self.data[ix+1] + self.data[ix+2]/10.0
> if self.data[ix+3] < 5:
> value = -value
> return value
>
> Now you can create a Data object from a list of values and ask it for
> values:
> d = Data(<some list>)
> d.getCoord(5)
>
> I'm not sure this is much improvement over passing around the list,
> actually; you still have to pass around the Data object...it might
> just be a matter of taste.
>
> HTH,
> Kent
More information about the Tutor
mailing list