[Python-talk] struct pack

Bruce Labitt bruce.labitt at myfairpoint.net
Thu Aug 27 19:28:49 EDT 2009


Thank you!  That is very helpful!
Bruce


Lloyd Kvam wrote:
> On Thu, 2009-08-27 at 12:01 -0400, bruce.labitt at autoliv.com wrote:
>   
>> Is there a way to use struct.pack on an array? or a list?
>>
>> # CME
>> from struct import pack, unpack
>> from numpy import array, random
>>
>> anum = random.normal(0., 0.1, 10)       # make array of 10 random numbers
>> fmt = '!'+str(anum.size)+'d'            # code as 10 doubles, network 
>> order
>>
>> apackt = pack( fmt, anum )              # <== pseudo-code, but that is my 
>> intent...
>>     
>
> In [11]: apackt = pack( fmt, *anum )              # <== pseudo-code,
>
> pack expects separate args after the format string.
> The * notation explodes a sequence into separate args to fit the pack
> function signature.
>
>   
>> apack = array(apackt)                   # convert returned tuple to an 
>> array
>>
>>
>> One gets the error, "error: pack requires exactly 10 arguments"
>>
>> Is there a way to do this besides processing each element and 
>> concatenating all the resultant strings?  I would imagine this to be quite 
>> slow for 308 x 32K byte strings = 1 FFT
>>
>> I did not understand the Section 8.3.1 Struct Objects. 
>> http://docs.python.org/library/struct.html
>>
>> Can someone provide a simple example (somewhat like above) that uses 
>> struct objects?
>>
>> Supposedly using struct objects is faster.  If I have to process every 
>> element, it could be useful.
>>
>> Thanks!
>> Bruce
>>
>> Bruce Labitt
>> Autoliv Electronics
>> 1011B Pawtucket Blvd, PO Box 1858
>> Lowell, MA  01853
>>
>> Email: bruce.labitt at autoliv.com. 
>> Tel:  (978) 674-6526
>> Fax: (978) 674-6581 
>>
>> ******************************
>> Neither the footer nor anything else in this E-mail is intended to or constitutes an <br>electronic signature and/or legally binding agreement in the absence of an <br>express statement or Autoliv policy and/or procedure to the contrary.<br>This E-mail and any attachments hereto are Autoliv property and may contain legally <br>privileged, confidential and/or proprietary information.<br>The recipient of this E-mail is prohibited from distributing, copying, forwarding or in any way <br>disseminating any material contained within this E-mail without prior written <br>permission from the author. If you receive this E-mail in error, please <br>immediately notify the author and delete this E-mail.  Autoliv disclaims all <br>responsibility and liability for the consequences of any person who fails to <br>abide by the terms herein. <br>
>> ******************************
>> _______________________________________________
>> Python-talk mailing list
>> Python-talk at dlslug.org
>> http://dlslug.org/mailman/listinfo/python-talk
>>     



More information about the Python-talk mailing list