some more doubts:? RE: [Tutor] How to convert integer to binay packed string...,?

dman dsh8290@rit.edu
Wed, 12 Sep 2001 12:09:26 -0400


On Wed, Sep 12, 2001 at 04:36:56PM +0530, Ajaya Babu wrote:
| Hi,
| I've some more doubts regarding serilizing objects and converting integer
| data to binary packed string.
| 
| 
| First when I use struct module...there is problem I am facing.., My code
| looks like
| 
| class EventDial(Event):
| 
|     def __init__(self, Pipe, DigitStr):
|         Event.__init__(self, Pipe)
|         self.DialedNumber = DigitStr
|         print self.DialedNumber
|         self.EventType = 1
| 
|     def Serialize(self):
|         a = len(self.DialedNumber)
|         data = pack('IIs', self.EventType, len(self.DialedNumber),
|                     self.DialedNumber)
|         self.Pipe.SendData(data)
| 
| but packing data using 'IIs' givign problem when my string size is more than
| 1. For example if I dail a number 911 my string size is 3 when I pack and
| receive at the toher end
| 
| It is giving like this...,
| 1l, 3l, '9'
| 
| When i went through the documentation i got a feeling that I should give
| sring size as a prefix to the 's' parameter. But i've variable string
| lenght. So How can I use this prefix notation...,?
 

        data = pack('II%ds' % len( self.DialedNumber ) , self.EventType, len(self.DialedNumber),
                    self.DialedNumber)

Use string interpolation or addition to add the number in dynamically.
Nothing says that the string has to be static.  I don't know how this
would work on the receiving side though.  Perhaps you would have to
extract the length integer first, then you would be able to construct
the format string to unpack the data string.

| Second thing when I tried using pickle. I confused with the documentation.
| there is a pcikle.dump(obj, file, bin=0) function.., but it always needs
| file. If I want to send to network (LAN) then why I need to have a file
| operation in between it is slow right? If I don't need to do this plese give
| me example or  link to the docs. I quite confused with the pickle...,

Does it need a 'file' or a 'file-like object'?  (I haven't checked the
docs myself)  If it is the latter, then a socket connection would
work.  It definitely needs something that it can write() to to dump
the object and something it can read() from to retrieve the object.

HTH,
-D