Re: finding byte order

2004-12-07 Thread biner . sebastien
Scott David Daniels wrote:
> biner wrote:
>  >   I am using a program that has to read binary data from files
coming
>  > from different machines. The file are always written with big
endian.
>
> Diez B. Roggisch wrote:
>   [Scott David Daniels wrote]
> >>How about sys.byteorder?
> > This doesn't help, as he wants to read files from varying endianess
- what
> > the _current_ endianess is doesn't matter here.
>
> But, in fact, he says the files are always big endian.  So, code like
> the following should address his problem.  Note I use type 'h' as an
> example so I can easily read samples.
>
>  import sys, array
>  f =open('huge.dat')
>  v = array.array('h')   # Or whatever data type
>  v.fromfile(f, 4096)
>  f.close()
>  if sys.byteorder == 'little':
>  v.byteswap()
>
> --Scott David Daniels
> [EMAIL PROTECTED]

This seems to do the what I want. I did not know about array.byteswap
and sys.byteorder.
Thanks for taking the time to answer my silly question.

Ciao!

-- 
http://mail.python.org/mailman/listinfo/python-list


why is this not working? (nested scope question)

2006-07-26 Thread biner . sebastien
I have a problem understanding the scope of variable in nested
function. I think I got it nailed to the following example copied from
Learning Python 2nd edition page 205. Here is the code.

def f1() :
x=88
f2()
def f2() :
print 'x=',x
f1()

that returns an error saying that "NameError: global name 'x' is not
defined". I expected f2 to "see" the value of x defined in f1 since it
is nested at runtime. My reading of the book comforted me in this.

What am I missing? Shouldn't the E of the LEGB rule take care of that.
BTW, I am running this on python 2.3.

Thanks.

Sébastien.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why is this not working? (nested scope question)

2006-07-26 Thread biner . sebastien

Thanks for the answers.

I do understand (and verified) that if I define f2 within f1, it works
as expected. But in the "learning pyton 2nd edition" at page 205 it is
said that "Programs are much simpler if you do not nest defs within
defs" (juste before the code mentioned in my initial message).

In a way, I though the local variables of f1 would in a way add to the
global variable of f2 (because f1 called f2) and that f2 would look in
the global variables when it could not find a variable locally
(following the LEGB rule).

Still the code I put is presented in the book and it does not work for
me. I googled for errata regarding that code but did not find any.

Sébastien.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why is this not working? (nested scope question)

2006-07-27 Thread biner . sebastien

> Actually, the code in the book is:
>
> def f1():
>  x = 88
>  f2(x)
>
> def f2(x):
>  print x
>
> f1()
>
> which makes all the difference in the world. Not to mention that this
> particular section of the book is giving an example of how to write the
> code *without* using nested functions.

Ouch! You got me there, I did not copy the code properly. Now I feel
stupid. Thanks for the enlightment.

I think I am starting to get it.

Sébastien.

-- 
http://mail.python.org/mailman/listinfo/python-list