Re: struct calcsize discrepency?

2011-12-06 Thread Nobody
On Mon, 05 Dec 2011 00:20:32 -0800, Mark Dickinson wrote: >> May be, yes, but since calcsize() is returning 12 when the elements >> are put in the other order, it would seem to be not counting such >> padding. > > Indeed. That's arguably a bug in the struct module, There's no "arguably" about i

Re: struct calcsize discrepency?

2011-12-05 Thread Mark Dickinson
On Dec 5, 8:09 am, Chris Angelico wrote: > May be, yes, but since calcsize() is returning 12 when the elements > are put in the other order, it would seem to be not counting such > padding. Indeed. That's arguably a bug in the struct module, and one that people have had to find workarounds for i

Re: struct calcsize discrepency?

2011-12-05 Thread Chris Angelico
On Mon, Dec 5, 2011 at 6:42 PM, Mark Dickinson wrote: > That's a strange way to think of it, especially since the padding also > happens for a single struct object when there's no array present.  I > find it cleaner to think of C as having no padding in arrays, but > padding at the end of a struct

Re: struct calcsize discrepency?

2011-12-05 Thread Mark Dickinson
On Dec 4, 3:17 pm, Chris Angelico wrote: > On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel wrote: > > In C, the padding to the largest alignment occurs at the > > end of a structure as well as between items.  Otherwise, an array of the > > struct would not be safely aligned.  if you have an 8byte item

Re: struct calcsize discrepency?

2011-12-04 Thread Chris Angelico
On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel wrote: > On 12/04/2011 09:35 AM, Chris Angelico wrote: >> > struct.calcsize("4sQ") >> >> 16 > > struct.calcsize("Q4s") >> >> 12 >> >> The eight-byte integer is aligned on an eight-byte boundary, so when >> it follows a four-byte string, you ge

Re: struct calcsize discrepency?

2011-12-04 Thread Glen Rice
On Dec 4, 9:38 am, Duncan Booth wrote: > Glen Rice wrote: > > In IPython: > >>import struct > >>struct.calcsize('4s') > > 4 > >>struct.calcsize('Q') > > 8 > >>struct.calcsize('4sQ') > > 16 > > > This doesn't make sense to me.  Can anyone explain? > > When you mix different types in a struct there

Re: struct calcsize discrepency?

2011-12-04 Thread Peter Otten
Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. Can anyone explain? A C compiler can insert padding bytes into a struct: """By default, the result of packing a given C struct

Re: struct calcsize discrepency?

2011-12-04 Thread Dave Angel
On 12/04/2011 09:35 AM, Chris Angelico wrote: On Mon, Dec 5, 2011 at 1:25 AM, Glen Rice wrote: In IPython: import struct struct.calcsize('4s') 4 struct.calcsize('Q') 8 struct.calcsize('4sQ') 16 This doesn't make sense to me. Can anyone explain? Same thing happens in CPython, and it loo

Re: struct calcsize discrepency?

2011-12-04 Thread Duncan Booth
Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. Can anyone explain? > When you mix different types in a struct there can be padding inserted between the items. In this case t

Re: struct calcsize discrepency?

2011-12-04 Thread Chris Angelico
On Mon, Dec 5, 2011 at 1:25 AM, Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me.  Can anyone explain? Same thing happens in CPython, and it looks to be the result of alignment. >

Re: struct pointing to another struct?

2010-08-13 Thread Peter Otten
inhahe wrote: > On Aug 13, 4:07 pm, Peter Otten <__pete...@web.de> wrote: >> inhahe wrote: >> > say i have this definition: >> >> > 1 typedef struct SDL_Surface { >> > 2 Uint32 flags; /* Read-only */ >> > 3 SDL_PixelFormat *format;/* Read-only */ >

Re: struct pointing to another struct?

2010-08-13 Thread inhahe
On Aug 13, 4:07 pm, Peter Otten <__pete...@web.de> wrote: > inhahe wrote: > > say i have this definition: > > >    1 typedef struct SDL_Surface { > >    2     Uint32 flags;                           /* Read-only */ > >    3     SDL_PixelFormat *format;                /* Read-only */ > >    4     in

Re: struct pointing to another struct?

2010-08-13 Thread Peter Otten
inhahe wrote: > say i have this definition: > >1 typedef struct SDL_Surface { >2 Uint32 flags; /* Read-only */ >3 SDL_PixelFormat *format;/* Read-only */ >4 int w, h; /* Read-only */ >5 Uin

Re: struct

2010-05-19 Thread Gary Herron
On 05/19/2010 02:53 PM, Back9 wrote: Can anyone explain the difference between f and d in struct unpack? When using them, some data work in either one not both. To me it seems to be same, TIA 'f' is single precision float (32 bits), and 'd' is a double precision float (64 bits) Gary Herr

Re: Struct on on x86_64 mac os x

2009-10-21 Thread Dave Angel
Mark Dickinson wrote: On Oct 20, 10:51 pm, Tommy Grav wrote: I have created a binary file that saves this struct from some C code: struct recOneData { char label[3][84]; char constName[400][6]; double timeData[3]; long int numConst; double AU;

Re: Struct on on x86_64 mac os x

2009-10-21 Thread Mark Dickinson
On Oct 21, 9:18 am, Mark Dickinson wrote: > On Oct 20, 10:51 pm, Tommy Grav wrote: > > >      def read_header(cls): > >          hdrData = "84s"*3 > >          constNData = "6s"*400 > > I'm confused:  why is this not "400s"*6 rather than "6s"*400? > Doesn't constName[400][6] mean 6 lots of constN

Re: Struct on on x86_64 mac os x

2009-10-21 Thread Mark Dickinson
On Oct 20, 10:51 pm, Tommy Grav wrote: > I have created a binary file that saves this struct from some C code: > >    struct recOneData { >           char label[3][84]; >           char constName[400][6]; >         double timeData[3]; >       long int numConst; >         double AU; >         doubl

Re: Struct on on x86_64 mac os x

2009-10-21 Thread Ulrich Eckhardt
Tommy Grav wrote: > I have created a binary file that saves this struct from some C code: > >struct recOneData { > char label[3][84]; > char constName[400][6]; > double timeData[3]; > long int numConst; > double AU; > double EMRAT; > long

Re: Struct on on x86_64 mac os x

2009-10-21 Thread Mark Tolonen
"Tommy Grav" wrote in message news:d705ab12-0bee-495a-b1e5-c43245e40...@pha.jhu.edu... I have created a binary file that saves this struct from some C code: struct recOneData { char label[3][84]; char constName[400][6]; double timeData[3]; long int numConst;

Re: Struct on on x86_64 mac os x

2009-10-20 Thread Gabriel Genellina
En Tue, 20 Oct 2009 18:51:21 -0300, Tommy Grav escribió: I have created a binary file that saves this struct from some C code: struct recOneData { char label[3][84]; char constName[400][6]; double timeData[3]; long int numConst; double AU;

Re: struct curiosity

2009-10-16 Thread pjcoup
Yes, this is basically what I expected as well. I would have expected some size that you can coax gcc to give, either 12 (as here), or 10 (with directives). Thanks to everyone for the responses! Pete On Oct 16, 4:30 am, Peter Otten <__pete...@web.de> wrote: > > I would have expected "native size

Re: struct curiosity

2009-10-16 Thread Gabriel Genellina
En Thu, 15 Oct 2009 15:07:04 -0300, pjcoup escribió: import struct struct.calcsize('BB') 11 struct.calcsize('@BB') 11 struct.calcsize(' 10 struct.calcsize('>BB') 10 I would have expected calcsize('BB') to be either 10 or 12 (padding), but 11? There are no pad bytes foll

Re: struct curiosity

2009-10-16 Thread Gabriel Genellina
En Thu, 15 Oct 2009 15:07:04 -0300, pjcoup escribió: import struct struct.calcsize('BB') 11 struct.calcsize('@BB') 11 struct.calcsize(' 10 struct.calcsize('>BB') 10 I would have expected calcsize('BB') to be either 10 or 12 (padding), but 11? There are no pad bytes foll

Re: struct curiosity

2009-10-16 Thread Mark Dickinson
On Oct 15, 7:07 pm, pjcoup wrote: > I was fooling around with python's struct lib, looking on how we'd > unpack some data.  I was a little confused by its behavior: > Python 2.5.2 (r252:60911, Jul 22 2009, 15:33:10) > [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2 > Type "help", "copyright", "credi

Re: struct curiosity

2009-10-16 Thread Peter Otten
Richard Brodie wrote: > "pjcoup" wrote in message > news:b1537079-6e3a-43e1-814b-7ccf185fb...@v15g2000prn.googlegroups.com... > > >> I would have expected calcsize('BB') to be either 10 or 12 >> (padding), but 11? Is there a simple explanation of what is going >> on here? > > The purpose

Re: struct curiosity

2009-10-16 Thread Richard Brodie
"pjcoup" wrote in message news:b1537079-6e3a-43e1-814b-7ccf185fb...@v15g2000prn.googlegroups.com... > I would have expected calcsize('BB') to be either 10 or 12 > (padding), but 11? Is there a simple explanation of what is going > on here? The purpose of the padding is to align the words

Re: Struct class random access

2008-08-26 Thread castironpi
On Aug 26, 12:41 am, castironpi <[EMAIL PROTECTED]> wrote: > On Aug 25, 11:47 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > > > On Mon, 25 Aug 2008 14:49:14 -0700, castironpi wrote: > > > I'm interested in the speed benefit, so you don't have to reconstruct > > > the entire 'record' jus

Re: Struct class random access

2008-08-25 Thread castironpi
On Aug 25, 11:47 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Mon, 25 Aug 2008 14:49:14 -0700, castironpi wrote: > > I'm interested in the speed benefit, so you don't have to reconstruct > > the entire 'record' just to read/write one 'field'.  How in ctypes? > > Only the field acces

Re: Struct class random access

2008-08-25 Thread Marc 'BlackJack' Rintsch
On Mon, 25 Aug 2008 14:49:14 -0700, castironpi wrote: > I'm interested in the speed benefit, so you don't have to reconstruct > the entire 'record' just to read/write one 'field'. How in ctypes? Only the field accessed is converted. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.o

Re: Struct class random access

2008-08-25 Thread castironpi
On Aug 25, 4:49 pm, castironpi <[EMAIL PROTECTED]> wrote: > On Aug 25, 4:25 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > > > > > On Mon, 25 Aug 2008 13:03:09 -0700, castironpi wrote: > > > struct.Struct lets you encode Python objects into structured memory. It > > > accepts a format st

Re: Struct class random access

2008-08-25 Thread castironpi
On Aug 25, 4:25 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Mon, 25 Aug 2008 13:03:09 -0700, castironpi wrote: > > struct.Struct lets you encode Python objects into structured memory. It > > accepts a format string, and optionally a buffer and offset to/from > > which to read/write

Re: Struct class random access

2008-08-25 Thread Marc 'BlackJack' Rintsch
On Mon, 25 Aug 2008 13:03:09 -0700, castironpi wrote: > struct.Struct lets you encode Python objects into structured memory. It > accepts a format string, and optionally a buffer and offset to/from > which to read/write the structure. What do you think of random access > for the results? > > (un

Re: struct unpack issue

2008-06-13 Thread Miles
On Fri, Jun 13, 2008 at 9:27 PM, Ping Zhao <[EMAIL PROTECTED]> wrote: > However, when I tried to rewrite them in short: > > header = struct.unpack('2s1i', self.__read(src, 6)) > ... > It was weired that the required argument length increased to 8. This is an alignment issue; if you don't specify a

Re: struct unpack issue

2008-06-13 Thread Peter Otten
Ping Zhao wrote: > I am writing a small program to decode MS bitmap image. When I use > statements as follow, it works fine: > > header['sig'] = str(struct.unpack('2s', self.__read(src, 2))[0]) > header['len'] = int(struct.unpack('1i', self.__read(src, 4))[0]) > > However, when I tried to rewr

Re: Struct usages in Python

2008-05-29 Thread Alex Gusarov
> Yes. That is the somewhat unfortunate difference between new-style and > old-style classes. > Use new-style if you can, and that means that "object" must be part of the > inheritance graph. ... >You are wrong for Python 2.X, but right for Python 3 where old-style > >classes are gone f

Re: Struct usages in Python

2008-05-29 Thread Alex Gusarov
> > Yes. That is the somewhat unfortunate difference between new-style and > old-style classes. Use new-style if you can, and that means that "object" > must be part of the inheritance graph. > ... > You are wrong for Python 2.X, but right for Python 3 where old-style > classes are gone for good.

Re: Struct usages in Python

2008-05-29 Thread Alex Gusarov
> > Yes. That is the somewhat unfortunate difference between new-style and > old-style classes. Use new-style if you can, and that means that "object" > must be part of the inheritance graph. > ... > You are wrong for Python 2.X, but right for Python 3 where old-style > classes are gone for good.

Re: Struct usages in Python

2008-05-28 Thread Alok Kumar
Thanks to everyone for your help. I am able to use array of structure (here Event is a class) in the following manner. But here I am fixing the array index as 4. Is there any easy way to keep it appending dynamically. self.event = [Event() for x in range(4)] # Event is a class as posted in origi

Re: Struct usages in Python

2008-05-28 Thread Arnaud Delobelle
Arnaud Delobelle <[EMAIL PROTECTED]> writes: > "Alex Gusarov" <[EMAIL PROTECTED]> writes: > >>> class Event(object): >>> >>> Always subclass object, unless you have a very compelling reason not to, >>> or you are subclassing something else. >>> >> >> I've thought that if I write >> >> class Event

Re: Struct usages in Python

2008-05-28 Thread Arnaud Delobelle
"Alex Gusarov" <[EMAIL PROTECTED]> writes: >> class Event(object): >> >> Always subclass object, unless you have a very compelling reason not to, >> or you are subclassing something else. >> > > I've thought that if I write > > class Event: > pass > > , it'll be subclass of object too, I was

Re: Struct usages in Python

2008-05-28 Thread Diez B. Roggisch
Alex Gusarov schrieb: class Event(object): Always subclass object, unless you have a very compelling reason not to, or you are subclassing something else. I've thought that if I write class Event: pass , it'll be subclass of object too, I was wrong? Yes. That is the somewhat unfortu

Re: Struct usages in Python

2008-05-28 Thread Alex Gusarov
> class Event(object): > > Always subclass object, unless you have a very compelling reason not to, > or you are subclassing something else. > I've thought that if I write class Event: pass , it'll be subclass of object too, I was wrong? -- Best regards, Alex Gusarov -- http://mail.python.

Re: Struct usages in Python

2008-05-28 Thread J. Cliff Dyer
On Wed, 2008-05-28 at 09:31 -0400, Alok Kumar wrote: > I am getting following error when tried as you suggested. > > self.event = [] #Create an empty list, bind to the name "event" under > the "self" namespace >self.event.append(Event()) #Create an event object and > append to the e

Re: Struct usages in Python

2008-05-28 Thread Alok Kumar
I am getting following error when tried as you suggested. self.event = [] #Create an empty list, bind to the name "event" under the "self" namespace self.event.append(Event()) #Create an event object and append to the end of the list *class Event(): ^ SyntaxError: in

Re: Struct usages in Python

2008-05-28 Thread Alok Kumar
while traversing I get out of index error as mentioned below. class EventTimeFilter: def __init__(self): * self.event = [Event()]* def populateScheduleData(self): self.doc = libxml2.parseFile(self.FILENAME) for eachcamera in self.doc.xpathEval('SetDeviceConfigura

Re: Struct usages in Python

2008-05-27 Thread Casey McGinty
>self.event[] = Event() *# Seems this is not allowed ?? * > self.event = [Event()] - Casey -- http://mail.python.org/mailman/listinfo/python-list

Re: Struct usages in Python

2008-05-27 Thread Patrick Mullen
I don't know if this will go through (my posts seem to have become blocked lately), but I'll give it a shot anyhow. You seem to be under a misconception that a python list is similar to a list in say, Java or other languages that have a rigid idea of variables and types. In python, a list is a li

Re: Struct usage and varying sizes of h, l, etc

2008-05-22 Thread Robert Kern
Ethan Furman wrote: Next question: when using struct.pack to store an integer, I get a deprecation warning if the int is too big... I would rather have an error. Is there a setting somewhere that controls this? http://docs.python.org/dev/library/warnings -- Robert Kern "I have come to bel

Re: Struct usage and varying sizes of h, l, etc

2008-05-22 Thread Ethan Furman
John Machin wrote: Robert Kern wrote: Ethan Furman wrote: Greetings, I'm looking at the struct module for binary packing of ints and floats. The documentation refers to C datatypes. It's been many years since I looked at C, but I seem to remember that the data type sizes were not fixed

Re: Struct usage and varying sizes of h, l, etc

2008-05-21 Thread [EMAIL PROTECTED]
On May 21, 10:04 am, Grant Edwards <[EMAIL PROTECTED]> wrote: > Yes, C defines "char" to be one byte, but it doesn't define the > size of a "byte" other than it's at least big enough to hold > one character (or something like that). In practice, a byte is > pretty much guaranteed to be at least 8

Re: Struct usage and varying sizes of h, l, etc

2008-05-21 Thread Ethan Furman
John Machin wrote: Robert Kern wrote: Ethan Furman wrote: Greetings, I'm looking at the struct module for binary packing of ints and floats. The documentation refers to C datatypes. It's been many years since I looked at C, but I seem to remember that the data type sizes were not fixed

Re: Struct usage and varying sizes of h, l, etc

2008-05-21 Thread Grant Edwards
On 2008-05-20, Robert Kern <[EMAIL PROTECTED]> wrote: >> I'm looking at the struct module for binary packing of ints >> and floats. The documentation refers to C datatypes. It's >> been many years since I looked at C, but I seem to remember >> that the data type sizes were not fixed -- for examp

Re: Struct usage and varying sizes of h, l, etc

2008-05-21 Thread Matthieu Brucher
> > This is all true if you want to operate in "native" mode; however in > "standard" mode the sizes are fixed -- otherwise there'd be no easy way of > reading/writing the fixed-size fields in many common file formats. > > As the manual says: > """ > Native size and alignment are determined using t

Re: Struct usage and varying sizes of h, l, etc

2008-05-21 Thread John Machin
Robert Kern wrote: Ethan Furman wrote: Greetings, I'm looking at the struct module for binary packing of ints and floats. The documentation refers to C datatypes. It's been many years since I looked at C, but I seem to remember that the data type sizes were not fixed -- for example, an int

Re: Struct usage and varying sizes of h, l, etc

2008-05-20 Thread Dan Bishop
On May 20, 5:59 pm, Robert Kern <[EMAIL PROTECTED]> wrote: > Ethan Furman wrote: > > Greetings, > > > I'm looking at the struct module for binary packing of ints and floats.   > > The documentation refers to C datatypes.  It's been many years since I > > looked at C, but I seem to remember that the

Re: Struct usage and varying sizes of h, l, etc

2008-05-20 Thread Robert Kern
Ethan Furman wrote: Greetings, I'm looking at the struct module for binary packing of ints and floats. The documentation refers to C datatypes. It's been many years since I looked at C, but I seem to remember that the data type sizes were not fixed -- for example, an int might be two byes o

Re: struct unpack

2008-03-17 Thread Mark Tolonen
"brnstrmrs" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > If I run: > > testValue = '\x02\x00' > junk = struct.unpack('h', testValue) > > Everything works but If I run > > testValue = raw_input("Enter Binary Code..:") inputting at the > console '\x02\x00' > junk = struct.unpack('

Re: struct unpack

2008-03-17 Thread Ivan Illarionov
On Mar 17, 11:00 pm, brnstrmrs <[EMAIL PROTECTED]> wrote: > If I run: > > testValue = '\x02\x00' > junk = struct.unpack('h', testValue) > > Everything works but If I run > > testValue = raw_input("Enter Binary Code..:") inputting at the > console '\x02\x00' > junk = struct.unpack('h', testValue) >

Re: struct unpack

2008-03-17 Thread Michael Wieher
> > testValue = '\x02\x00' > junk = struct.unpack('h', testValue) #Works > > testValue = raw_input("Enter Binary Code..:") inputting at the > console '\x02\x00' > junk = struct.unpack('h', testValue) > > error: unpack requires a string argument of length 2 Well, it thinks the length of the test

Re: struct,long on 64-bit machine

2007-11-19 Thread Hrvoje Niksic
Neal Becker <[EMAIL PROTECTED]> writes: > What's wrong with this? > type(struct.unpack('l','\00'*8)[0]) > > > Why I am getting 'int' when I asked for 'long'? C longs are converted to Python integers; see the table on http://docs.python.org/lib/module-struct.html. If you really need the Python l

Re: struct,long on 64-bit machine

2007-11-19 Thread Robin Becker
Neal Becker wrote: > What's wrong with this? > type(struct.unpack('l','\00'*8)[0]) > > > Why I am getting 'int' when I asked for 'long'? > > This is on python-2.5.1-15.fc8.x86_64 > On my AMD 64 I think int is 64 bits $ python -c "import sys; print sys.maxint" 9223372036854775807 -- Robin Be

Re: struct is saving 4 bytes instead of 2

2007-09-13 Thread Bjoern Schliessmann
TonyB wrote: > I tried > > f.write(struct.pack('=h',thevariable)) > > and it works. Great! :) Regards, Björn -- BOFH excuse #45: virus attack, luser responsible -- http://mail.python.org/mailman/listinfo/python-list

Re: struct is saving 4 bytes instead of 2

2007-09-12 Thread TonyB
On Sep 12, 6:45 pm, Bjoern Schliessmann wrote: > TonyB wrote: > > When I inspect the file with a hex editor it show that the > > variable is being saved as 4 bytes. How can I make it save the > > value as 2 bytes? > > Use an aligment specifier in the format string (you want "standard" > alignment

Re: struct is saving 4 bytes instead of 2

2007-09-12 Thread Bjoern Schliessmann
TonyB wrote: > When I inspect the file with a hex editor it show that the > variable is being saved as 4 bytes. How can I make it save the > value as 2 bytes? Use an aligment specifier in the format string (you want "standard" alignment). See: http://docs.python.org/lib/module-struct.html Rega

Re: struct: type registration?

2006-06-02 Thread [EMAIL PROTECTED]
using struct for the stuff you're up to and you'll finish with weird unmaintainable code. save yourself a lot of pain and use construct instead. http://pyconstruct.wikispaces.com/ -- http://mail.python.org/mailman/listinfo/python-list

Re: struct: type registration?

2006-06-02 Thread Serge Orlov
John Machin wrote: > On 2/06/2006 4:18 AM, Serge Orlov wrote: > > If you want to parse binary data use pyconstruct > > > > > > Looks promising on the legibility and functionality fronts. Can you make > any comment on the speed? I don't know really. I used it fo

Re: struct: type registration?

2006-06-01 Thread John Machin
On 2/06/2006 4:18 AM, Serge Orlov wrote: > Giovanni Bajo wrote: >> John Machin wrote: >>> I am an idiot, so please be gentle with me: I don't understand why you >>> are using struct.pack at all: >> Because I want to be able to parse largest chunks of binary datas with custom >> formatting. Did you

Re: struct: type registration?

2006-06-01 Thread John Machin
On 2/06/2006 3:44 AM, Giovanni Bajo wrote: > John Machin wrote: > >>> Looks like you totally misread my message. >> Not at all. >> >> Your function: >> >> def mystring_pack(s): >> if len(s) > 20: >> raise ValueError, "a mystring can be at max 20 chars" >> s = (s + "\0"*20)[:20]

Re: struct: type registration?

2006-06-01 Thread Serge Orlov
Giovanni Bajo wrote: > John Machin wrote: > > I am an idiot, so please be gentle with me: I don't understand why you > > are using struct.pack at all: > > Because I want to be able to parse largest chunks of binary datas with custom > formatting. Did you miss the whole point of my message: > > stru

Re: struct: type registration?

2006-06-01 Thread Giovanni Bajo
John Machin wrote: >> Looks like you totally misread my message. > > Not at all. > > Your function: > > def mystring_pack(s): > if len(s) > 20: > raise ValueError, "a mystring can be at max 20 chars" > s = (s + "\0"*20)[:20] > s = struct.pack("20s", s) > return s > > c

Re: struct: type registration?

2006-06-01 Thread John Machin
On 1/06/2006 9:52 PM, Giovanni Bajo wrote: > John Machin wrote: > >>> given the ongoing work on struct (which I thought was a dead >>> module), I was wondering if it would be possible to add an API to >>> register custom parsing codes for struct. Whenever I use it for >>> non-trivial tasks, I alwa

Re: struct: type registration?

2006-06-01 Thread Giovanni Bajo
Giovanni Bajo wrote: > You need struct.unpack() to parse these datas, and you need custom > packer/unpacker to avoid post-processing the output of unpack() just > because it just knows of basic Python types. In binary structs, there > happen to be *types* which do not map 1:1 to Python types, nor

Re: struct: type registration?

2006-06-01 Thread Giovanni Bajo
John Machin wrote: >> given the ongoing work on struct (which I thought was a dead >> module), I was wondering if it would be possible to add an API to >> register custom parsing codes for struct. Whenever I use it for >> non-trivial tasks, I always happen to write small wrapper functions >> to ad

Re: struct: type registration?

2006-05-31 Thread John Machin
On 1/06/2006 10:50 AM, Giovanni Bajo wrote: > Hello, > > given the ongoing work on struct (which I thought was a dead module), I was > wondering if it would be possible to add an API to register custom parsing > codes for struct. Whenever I use it for non-trivial tasks, I always happen to > write

RE: struct size confusion:

2006-03-22 Thread Michael Yanowitz
), but I am content that it is working now. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Fredrik Lundh Sent: Wednesday, March 22, 2006 9:28 AM To: python-list@python.org Subject: Re: struct size confusion: Michael Yanowitz wrote: >I am relatively n

Re: struct size confusion:

2006-03-22 Thread Fredrik Lundh
Michael Yanowitz wrote: >I am relatively new to Python and this is my first post on > this mailing list. > >I am confused as to why I am getting size differences in the following > cases: > > >>> print struct.calcsize("I") > 4 > >>> print struct.calcsize("H") > 2 > >>> print struct.calcsiz

Re: struct size confusion:

2006-03-22 Thread Kent Johnson
Michael Yanowitz wrote: > Hello: > >I am relatively new to Python and this is my first post on > this mailing list. > >I am confused as to why I am getting size differences in the following > cases: > > print struct.calcsize("I") > > 4 > print struct.calcsize("H") > > 2 > >>

Re: struct size confusion:

2006-03-22 Thread Diez B. Roggisch
Michael Yanowitz wrote: >Why is it 8 bytes in the third case and why would it be only 6 bytes > in the last case if it is 8 in the previous? >From TFM: """ Native size and alignment are determined using the C compiler's sizeof expression. This is always combined with native byte order. Sta

Re: * 'struct-like' list *

2006-02-10 Thread Bengt Richter
On Tue, 07 Feb 2006 18:10:05 GMT, [EMAIL PROTECTED] (Bengt Richter) wrote: [...] >< ernesto.py >- [...] Just noticed: >substrings = line.split() >if substrings and isinstance(substrings, list) and substrings[0] == > 'Name:

Re: * 'struct-like' list *

2006-02-07 Thread Ernesto
Thanks tons ! -- http://mail.python.org/mailman/listinfo/python-list

Re: * 'struct-like' list *

2006-02-07 Thread Bengt Richter
On 6 Feb 2006 09:03:09 -0800, "Ernesto" <[EMAIL PROTECTED]> wrote: >I'm still fairly new to python, so I need some guidance here... > >I have a text file with lots of data. I only need some of the data. I >want to put the useful data into an [array of] struct-like >mechanism(s). The text file l

Re: * 'struct-like' list *

2006-02-07 Thread Ernesto
Thanks ! -- http://mail.python.org/mailman/listinfo/python-list

Re: * 'struct-like' list *

2006-02-07 Thread Schüle Daniel
Ernesto wrote: > Thanks for the approach. I decided to use regular expressions. I'm > going by the code you posted (below). I replaced the line re.findall > line with my file handle read( ) like this: > > print re.findall(pattern, myFileHandle.read()) > > This prints out only brackets []. Is

Re: * 'struct-like' list *

2006-02-07 Thread Ernesto
Thanks for the approach. I decided to use regular expressions. I'm going by the code you posted (below). I replaced the line re.findall line with my file handle read( ) like this: print re.findall(pattern, myFileHandle.read()) This prints out only brackets []. Is a 're.compile' perhaps necess

Re: * 'struct-like' list *

2006-02-06 Thread Raymond Hettinger
[Ernesto] > I'm still fairly new to python, so I need some guidance here... > > I have a text file with lots of data. I only need some of the data. I > want to put the useful data into an [array of] struct-like > mechanism(s). The text file looks something like this: > > [BUNCH OF NOT-USEFUL DAT

Re: * 'struct-like' list *

2006-02-06 Thread Paul McGuire
"Ernesto" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I'm still fairly new to python, so I need some guidance here... > > I have a text file with lots of data. I only need some of the data. I > want to put the useful data into an [array of] struct-like > mechanism(s). The text

Re: * 'struct-like' list *

2006-02-06 Thread Schüle Daniel
> I would like to have an array of "structs." Each struct has > > struct Person{ > string Name; > int Age; > int Birhtday; > int SS; > } the easiest way would be class Person: pass john = Person() david = Person() john.name = "John Brown" john.age = 35 etc think of

Re: * 'struct-like' list *

2006-02-06 Thread Rene Pijlman
Ernesto: >1. How to search for the keywords "Name:", "Age:", etc. in the file... You could use regular expression matching: http://www.python.org/doc/lib/module-re.html Or plain string searches: http://www.python.org/dev/doc/devel/lib/string-methods.html >2. How to implement some organized "li

Re: struct, IEEE-754 and internal representation

2005-11-09 Thread ej
Ah! Well! That explains it. I started to suspect that but (obviously) did not know that. LOL Thanks for your prompt reply, Grant. :) -ej -- http://mail.python.org/mailman/listinfo/python-list

Re: struct, IEEE-754 and internal representation

2005-11-09 Thread jepler
Use 'd' as the format character for 64-bit double precision numbers with struct. >>> x = 148.73 >>> unpack(">> unpack(" pgpB2b9owxZs7.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list

Re: struct, IEEE-754 and internal representation

2005-11-09 Thread Robert Kern
ej wrote: > If that's true, then I guess I am confused why Python is displaying > 148.72999572753906 when you unpack the 4 bytes, implying a lot more > precision that was available in the original 32-bits? Python is doing > 64-bit floating point here? I'm obviously not understanding somethin

Re: struct, IEEE-754 and internal representation

2005-11-09 Thread Grant Edwards
On 2005-11-09, ej <> wrote: > If that's true, then I guess I am confused why Python is displaying > 148.72999572753906 when you unpack the 4 bytes, implying a lot more > precision that was available in the original 32-bits? Python is doing > 64-bit floating point here? Yes. C-Python "float"

Re: struct.(un)pack and ASCIIZ strrings

2005-06-18 Thread John Machin
Terry Reedy wrote: > "Sergey Dorofeev" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>I can use string.unpack if string in struct uses fixed amount of bytes. > > > I presume you mean struct.unpack(format, string). The string len must be > known when you call, but need not b

Re: struct.(un)pack and ASCIIZ strrings

2005-06-18 Thread Terry Reedy
"Sergey Dorofeev" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >I can use string.unpack if string in struct uses fixed amount of bytes. I presume you mean struct.unpack(format, string). The string len must be known when you call, but need not be fixed across multiple calls with

Re: struct unpack newline

2005-05-31 Thread grant
Good point. Hadn't thouhgt of that. Thanks Grant -- http://mail.python.org/mailman/listinfo/python-list

Re: struct unpack newline

2005-05-31 Thread Peter Otten
[EMAIL PROTECTED] wrote: > what concerns me though is that although the file is opened in binary > mode, > "for line.." has a problem reading the file correctly. There is _no_ correct way of splitting a file containing binary data in lines because binary data may contain newline bytes that do not

Re: struct unpack newline

2005-05-30 Thread grant
Hi , Thanks for the tip regarding checking the length of line. I discovered that on the problem record it was short by a few bytes. After changing the read method from "for line in.." to "infile.read(n)" my problem was solved, what concerns me though is that although the file is opened in binary m

Re: struct unpack newline

2005-05-27 Thread Richard Brodie
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > except when line[86:90] contains "carriage-return" "linefeed" > which are valid binary packed values. You probably don't want to be reading binary data a line at a time, if that's what you're doing. -- http://mail.python.org/mailma

Re: struct unpack newline

2005-05-27 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > I am pretty new to python and am having a problem > intepreting binary data using struct.unpack. > I am reading a file containing binary packed data > using open with "rb". All the values are coming through > fine when using (integer1,) = struct.unpack('l', line[86:90])

Re: struct enhancements?

2005-04-07 Thread Tim Peters
[EMAIL PROTECTED] > I would like to be able to pack/unpack 8-byte longs, ... Have you tried struct's 'q' or 'Q' format codes? -- http://mail.python.org/mailman/listinfo/python-list

  1   2   >