pack/unpack zero terminated string

2007-05-02 Thread tmp123
Hello,

Thanks for your time.

After review the "struct" documentation, it seems there are no option
to pack/unpack zero terminated strings.

By example, if the packed data contains: byte + zero terminated string
+ zero terminated string + byte, it seems no possible to unpack it
using "struct".

Please, has someone any hint or pointer to another librarian to be
used?

Thanks.

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


Re: pack/unpack zero terminated string

2007-05-03 Thread tmp123
On May 2, 11:13 pm, John Machin <[EMAIL PROTECTED]> wrote:
> On May 3, 12:01 am, Laurent Pointal <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
>
>
> >tmp123a écrit :
>
> > > Hello,
>
> > > Thanks for your time.
>
> > > After review the "struct" documentation, it seems there are no option
> > > to pack/unpack zero terminated strings.
>
> > > By example, if the packed data contains: byte + zero terminated string
> > > + zero terminated string + byte, it seems no possible to unpack it
> > > using "struct".
>
> > > Please, has someone any hint or pointer to another librarian to be
> > > used?
>
> > May look at xstruct too
>
> >http://www.sis.nl/python/xstruct/xstruct.shtml
>
> Hi, Laurent,
>
> It's a reasonable presumption that the OP needs to unpack *variable-
> length* zero-terminated strings, otherwise why is he asking? This
> would need a new format type e.g. "z".
>
> xstruct doesn't appear to offer variable-length strings, and is frozen
> in time (October 1999) -- inspection of the source shows that it is a
> copy of Python 1.5.2 structmodule.c with added stuff.
>
> The OP might like to try a bit of DIY in Python, along the following
> lines:
>
> C:\junk>type unpackz.py
> def getz(strg, start=0):
> zpos = strg.index('\0', start)
> return strg[start:zpos], zpos + 1
>
> def getB(strg, start=0):
> return ord(strg[start]), start + 1
>
> def unpack_BzzB(strg):
> pos = 0
> r0, pos = getB(strg, pos)
> r1, pos = getz(strg, pos)
> r2, pos = getz(strg, pos)
> r3, pos = getB(strg, pos)
> assert pos == len(strg)
> return r0, r1, r2, r3
>
> x = chr(42) + 'foo\0' + 'mumble\0' + '\xff'
> print unpack_BzzB(x)
> print unpack_BzzB('\0' * 4)
>
> C:\junk>unpackz.py
> (42, 'foo', 'mumble', 255)
> (0, '', '', 0)
>
> HTH,
> John

Hello John,

Totally true, the solution you propose is the one I'm using now. The
subject was, before to start from scratch, try to reuse something
existing.

Another possibility was to modify the "struct" package with the new
option, but it seems a mixed C-Python implementation, and I do not
like to start having compatibility problems in the C elements.

Kind regards.

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


newbie: copy base class fields

2007-05-03 Thread tmp123
Hello,

Thanks for your time.

The following small program gives an error:

#!/usr/bin/python
#

class A:
  def __init__(self):
self.v1=1

  def __repr__(self):
 return "v1=%d\n" % self.v1

class B(A):
  def __init__(self,a):
self=a
self.v2=2

  def __repr__(self):
 return A.__repr__(self) + ("v2=%d\n" % self.v2)

x=A()
print x

y=B(x)
print y



$ ./prueba.pl
v1=1

Traceback (most recent call last):
  File "./prueba.pl", line 23, in 
print y
  File "./prueba.pl", line 17, in __repr__
return A.__repr__(self) + ("v2=%d\n" % self.v2)
  File "./prueba.pl", line 9, in __repr__
return "v1=%d\n" % self.v1
AttributeError: B instance has no attribute 'v1'


It seems that the statement "self=a" is not the correct way to copy
all the fields of the base class from the __init__ argument to the new
object.

Of course, it is not an option to copy one by one all the fields of
class A inside the __init__ of B.

Several variants of the program produces similar results.

Please, could someone explain which way is the correct way?

Thanks a lot.

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


Re: newbie: copy base class fields

2007-05-03 Thread tmp123
On May 3, 4:29 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>
> > #!/usr/bin/python
> > #
>
> > class A:
> >   def __init__(self):
> > self.v1=1
>
> >   def __repr__(self):
> >  return "v1=%d\n" % self.v1
>
> > class B(A):
> >   def __init__(self,a):
> > self=a
> > self.v2=2
>
> >   def __repr__(self):
> >  return A.__repr__(self) + ("v2=%d\n" % self.v2)
>
> > x=A()
> > print x
>
> > y=B(x)
> > print y
>
> > $ ./prueba.pl
> > v1=1
>
> > Traceback (most recent call last):
> >   File "./prueba.pl", line 23, in 
> > print y
> >   File "./prueba.pl", line 17, in __repr__
> > return A.__repr__(self) + ("v2=%d\n" % self.v2)
> >   File "./prueba.pl", line 9, in __repr__
> > return "v1=%d\n" % self.v1
> > AttributeError: B instance has no attribute 'v1'
>


Hello Marc,

Thanks for your help.

I'm sorry, I've not correctly explained the subject. It is need to
init class B with the current value of the A instance, not with the
initial ones. A best example is:

x=A()
print x

x.v1=3

y=B(x)
print y

at the end, y.v1 must be equal to 3.

Sorry again.


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


Re: newbie: copy base class fields

2007-05-06 Thread tmp123
On May 3, 4:57 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
> Is it okay to copy them all at once?  Like this:
>
> class B(A):
> def __init__(self, a):
> self.__dict__.update(a.__dict__)
> self.v2 = 2
>

Thanks a lot for the answers, they seem to agree with the requested
funcitionality.

Kind regards.

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


Execute commands from file

2007-05-16 Thread tmp123
Hello,

Thanks for your time.

We have very big files with python commands (more or less, 50
commands each file).

It is possible to execute them command by command, like if the
commands was typed one after the other in a interactive session?

( Better using command flags than with an small script like "while 1:
input()")

Thanks a lot.

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