[Tfug] Language wars ( was Language choices )
Stephen Hooper
stephen.hooper at gmail.com
Fri Nov 3 18:30:27 MST 2006
On 11/3/06, Tim Ottinger <tottinge at gmail.com> wrote:
> > ByteList = readIPMI(4)
> > IConv = struct.unpack('f',struct.pack('BBBB', ByteList[3], ByteList[2],
> > ByteList[1], ByteList[0]))[0]
>
>
> Right. Better to have that buried in a named function (explanatory
> function).
> Maybe "current_conversion_value = power_supply.current_conversion_value();"
>
>
Seems like it would make a nice class:
class IPMI:
def init(self,hwaddr,word = ""):
self.__hwaddr = hwaddr
self.__word = word
def read(self):
bytes = # do read from hardware
word = "".join([chr(byte) for byte in reversed(bytes)]
return IPMI(self.__hwaddr,word)
def __float__(self):
return struct.unpack("f",self.__word))[0]
def __int__(self):
return struct.unpack("L",self.__word))[0]
def __hex__(self):
return long.__hex__(self,int(self))
... etc ..
Then you could just do:
ipmi1 = IPMI(foo_addr)
IConv = float(ipmi.read())
Or even make it iteratable...
More information about the tfug
mailing list