finding byte order

2004-12-03 Thread biner
Hello,

  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.
I am using the struct module to read the data and it is fine because I
can specify in the format if the data are to be read with big or small
endian convention.

  I would like to use the array module instead of struct because it is
suppose to be faster for big arrays. However, this module does not
provide a format specifier to say if the data are writtent with big or
small endian. The result is that it works on big-endian machine and
not on small-endian machine.

  I would like to have a test to tell me if the current machine is
using big or small endian, this way I could use the array module in
the first case and the *slower* struct module on the second. I looked 
but did not find. Is there a python function to know that?

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


getting env variable from bourne shell script

2004-12-21 Thread biner
Hello,

  I have file called PARAMETRES that is used in bourne shell script to
define variable. In order to do so I put a ". PARAMETRES" line and the
script has acces to all the variable defined in the PARAMETRES file.

  Now, I would like to be able to get the same thing in python. I
googled and played with os.system to try to come up with something but
no success so far. I also tryed exec but it doesn't work becaus a lot
of string variable difined in the PARAMETRES file do not have the '
sign needed in python string.

  for example. Let's say I have a file PARAMETRES containing
TOTO=whatever, I would like to have a command to read that file and
have the variable TOTO='whatever' in python.

Thanks for any help.

Sébastien
-- 
http://mail.python.org/mailman/listinfo/python-list


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