Re: How to Read Bytes from a file

2007-03-06 Thread Gabriel Genellina
En Tue, 06 Mar 2007 17:07:45 -0300, Matthias Julius <[EMAIL PROTECTED]> escribió: > "Gabriel Genellina" <[EMAIL PROTECTED]> writes: > >> Dictionary access is highly optimized in Python. In fact, using a >> precomputed dictionary is about 12 times faster: > > Why using a dictionary and not a list

Re: How to Read Bytes from a file

2007-03-06 Thread Matthias Julius
"Gabriel Genellina" <[EMAIL PROTECTED]> writes: > En Fri, 02 Mar 2007 08:22:36 -0300, Bart Ogryczak > <[EMAIL PROTECTED]> escribió: > >> On Mar 1, 7:36 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> >> wrote: >>> Thanks Bart. That's perfect. The other suggestion was to precompute >>> count1 for all

Re: How to Read Bytes from a file

2007-03-05 Thread Hendrik van Rooyen
"Bart Ogryczak" <[EMAIL PROTECTED]> wrote: > On Mar 5, 10:51 am, Piet van Oostrum <[EMAIL PROTECTED]> wrote: > > > "Bart Ogryczak" <[EMAIL PROTECTED]> (BO) wrote: > > >BO> Any system with 8-bit bytes, which would mean any system made after > > >BO> 1965. I'm not aware of any Python implement

Re: How to Read Bytes from a file

2007-03-05 Thread Hendrik van Rooyen
"Piet van Oostrum" <[EMAIL PROTECTED]> wrote: > > "Bart Ogryczak" <[EMAIL PROTECTED]> (BO) wrote: > > >BO> Any system with 8-bit bytes, which would mean any system made after > >BO> 1965. I'm not aware of any Python implementation for UNIVAC, so I > >BO> wouldn't worry ;-) > > 1965? I worked

Re: How to Read Bytes from a file

2007-03-05 Thread Gabriel Genellina
En Fri, 02 Mar 2007 08:22:36 -0300, Bart Ogryczak <[EMAIL PROTECTED]> escribió: > On Mar 1, 7:36 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > wrote: >> Thanks Bart. That's perfect. The other suggestion was to precompute >> count1 for all possible bytes, I guess that's 0-256, right? > > 0-255

Re: How to Read Bytes from a file

2007-03-05 Thread Bart Ogryczak
On Mar 5, 10:51 am, Piet van Oostrum <[EMAIL PROTECTED]> wrote: > > "Bart Ogryczak" <[EMAIL PROTECTED]> (BO) wrote: > >BO> Any system with 8-bit bytes, which would mean any system made after > >BO> 1965. I'm not aware of any Python implementation for UNIVAC, so I > >BO> wouldn't worry ;-) > > 1

Re: How to Read Bytes from a file

2007-03-05 Thread Piet van Oostrum
> "Bart Ogryczak" <[EMAIL PROTECTED]> (BO) wrote: >BO> Any system with 8-bit bytes, which would mean any system made after >BO> 1965. I'm not aware of any Python implementation for UNIVAC, so I >BO> wouldn't worry ;-) 1965? I worked with non-8-byte machines (CDC) until the beginning of the 80

Re: How to Read Bytes from a file

2007-03-02 Thread Bart Ogryczak
On Mar 1, 7:36 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Mar 1, 12:46 pm, "Bart Ogryczak" <[EMAIL PROTECTED]> wrote: > > > This solution looks nice, but how does it work? I'm guessing > > > struct.unpack will provide me with 8 bit bytes > > > unpack with 'B' format gives you int val

Re: How to Read Bytes from a file

2007-03-02 Thread Hendrik van Rooyen
<[EMAIL PROTECTED]> wrote: > Thanks Bart. That's perfect. The other suggestion was to precompute > count1 for all possible bytes, I guess that's 0-256, right? 0 to 255 inclusive, actually - that is 256 numbers... The largest number representable in a byte is 255 eight bits, of value 128,64,32

Re: How to Read Bytes from a file

2007-03-01 Thread [EMAIL PROTECTED]
On Mar 1, 12:46 pm, "Bart Ogryczak" <[EMAIL PROTECTED]> wrote: > > This solution looks nice, but how does it work? I'm guessing > > struct.unpack will provide me with 8 bit bytes > > unpack with 'B' format gives you int value equivalent to unsigned char > (1 byte). > > > (will this work on any sys

Re: How to Read Bytes from a file

2007-03-01 Thread Bart Ogryczak
On Mar 1, 4:58 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Mar 1, 8:53 am, "Bart Ogryczak" <[EMAIL PROTECTED]> wrote: > > > > > On Mar 1, 7:52 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > > wrote: > > > > It seems like this would be easy but I'm drawing a blank. > > > > What I want to

Re: How to Read Bytes from a file

2007-03-01 Thread John Machin
On Mar 2, 12:53 am, "Bart Ogryczak" <[EMAIL PROTECTED]> wrote: > > import struct > buf = open('somefile','rb').read() > count1 = lambda x: (x&1)+(x&2>0)+(x&4>0)+(x&8>0)+(x&16>0)+(x&32>0)+ > (x&64>0)+(x&128>0) > byteOnes = map(count1,struct.unpack('B'*len(buf),buf)) byteOnes = map(count1,struct.unp

Re: How to Read Bytes from a file

2007-03-01 Thread [EMAIL PROTECTED]
On Mar 1, 8:53 am, "Bart Ogryczak" <[EMAIL PROTECTED]> wrote: > On Mar 1, 7:52 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > wrote: > > > It seems like this would be easy but I'm drawing a blank. > > > What I want to do is be able to open any file in binary mode, and read > > in one byte (8 bits) a

Re: How to Read Bytes from a file

2007-03-01 Thread Alex Martelli
Leif K-Brooks <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > You should probaby prepare before the loop a mapping from char to number > > of 1 bits in that char: > > > > m = {} > > for c in range(256): > > m[c] = countones(c) > > Wouldn't a list be more efficient? > > m = [countones(c)

Re: How to Read Bytes from a file

2007-03-01 Thread Jussi Salmela
Bart Ogryczak kirjoitti: > On Mar 1, 7:52 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > wrote: >> It seems like this would be easy but I'm drawing a blank. >> >> What I want to do is be able to open any file in binary mode, and read >> in one byte (8 bits) at a time and then count the number of 1 b

Re: How to Read Bytes from a file

2007-03-01 Thread Bart Ogryczak
On Mar 1, 7:52 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > It seems like this would be easy but I'm drawing a blank. > > What I want to do is be able to open any file in binary mode, and read > in one byte (8 bits) at a time and then count the number of 1 bits in > that byte. > > I got as

Re: How to Read Bytes from a file

2007-03-01 Thread Leif K-Brooks
Alex Martelli wrote: > You should probaby prepare before the loop a mapping from char to number > of 1 bits in that char: > > m = {} > for c in range(256): > m[c] = countones(c) Wouldn't a list be more efficient? m = [countones(c) for c in xrange(256)] -- http://mail.python.org/mailman/listin

Re: How to Read Bytes from a file

2007-02-28 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > It seems like this would be easy but I'm drawing a blank. > > What I want to do is be able to open any file in binary mode, and read > in one byte (8 bits) at a time and then count the number of 1 bits in > that byte. > > I got as far as this but it

How to Read Bytes from a file

2007-02-28 Thread [EMAIL PROTECTED]
It seems like this would be easy but I'm drawing a blank. What I want to do is be able to open any file in binary mode, and read in one byte (8 bits) at a time and then count the number of 1 bits in that byte. I got as far as this but it is giving me strings and I'm not sure how to accurately get