Re: C pointers/Python

2005-03-22 Thread Lonnie Princehouse
len is a built-in function in Python, so you probably don't want to
use it as a variable name.

What is this C code actually trying to do?  Don't try to transliterate
it; instead, read up on how lists and slicing work, and rewrite it in
Python starting from a higher level of abstraction.

One hint- you can copy the same datum many times without a while
loop...

# reassign sub-list interval [i, j-1]
buffer[i:j] = [data] * (j-i)

# reassign the entire list
buffer[:] = [data] * len(buffer)

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


Re: C pointers/Python

2005-03-22 Thread Stephen Thorne
On 21 Mar 2005 19:32:20 -0800, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 Can i do something like this?
 
 if code == CODE1:
 data = 0x0
 While True:
  len = len - 1
  if len == -1:
  break
  buffer = data
 
 Do i need to initialze the buffer?

Please, forget everything you know about memory, pointers, and C idiom.
Also, considering doing the python tutorial. There is one provided
here: http://python.org/doc/tut

Basically, you're not approaching this in a python-like way, and thus
what should be a single line (i.e. 'return data') you're trying to
turn into a painful process of copying data from one memory location
to another. This is acceptable in the C world, and not appropriate in
the python world.

I would suggest you define your problem in broader terms (i.e. I am
trying to interpret data that is coming over a socket, my packet
structure looks like this ..., how do I marshall dispatch that to
callbacks so I can talk the protocol properly? Here is a link to the
working C code http://...;) and there is a large group of wonderful
people here that would love to show you the pythonic way of achieving
your goal.

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


Re: C pointers/Python

2005-03-21 Thread Stephen Thorne
On 21 Mar 2005 15:52:44 -0800, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 Hello,
 
 I am trying to convert some C code into python.
 Here's the C code...
 
 typedef enum {
 CODE1 = 0x1,
 CODE2 = 0x2
 } CODE
 testit(unsigned char *buffer,
unsigned long length,
CODE code)
 {
 unsigned long data
 switch (code)
 {
   case CODE1:
   while(len--)
   {
   *buffer++ = (unsigned long)data++
   }
   break

CODE1 = 1
CODE2 = 2
def testit(code):
data = 'somedata'
if code == CODE1:
return data

In C, you pass a memory location to copy the result into. In Python,
we return the result as an object.

As for pointers, we don't need them.

-- 
Stephen Thorne
Development Engineer, NetBoxBlue.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C pointers/Python

2005-03-21 Thread integer
you never deal directly with pointers in python.
in your case, you need to pass a mutable object as your 'buffer'
argument and perform operations on it such that 'buffer==data'. (for
example, you could use a list here.)
def testit( buffer ):
   for i in range( len ):
  buffer[A+i]=data[B+i]
for some constants A,B

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


Re: C pointers/Python

2005-03-21 Thread Stephen Thorne
On 21 Mar 2005 17:16:18 -0800, integer [EMAIL PROTECTED] wrote:
 you never deal directly with pointers in python.
 in your case, you need to pass a mutable object as your 'buffer'
 argument and perform operations on it such that 'buffer==data'. (for
 example, you could use a list here.)
 def testit( buffer ):
for i in range( len ):
   buffer[A+i]=data[B+i]
 for some constants A,B

That's unpythonic.

The correct solution is to return the result. Anything else is trying
to squeeze a C idiom into python for no gain.

-- 
Stephen Thorne
Development Engineer, NetBoxBlue.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C pointers/Python

2005-03-21 Thread Marcin Mika
agreed.
you might say i was trying to translate his C code word for word,
rather than properly pythonizing the entire chunk of code as a whole.

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


Re: C pointers/Python

2005-03-21 Thread [EMAIL PROTECTED]
Can i do something like this?

if code == CODE1:
data = 0x0
While True:
 len = len - 1
 if len == -1:
 break
 buffer = data

Do i need to initialze the buffer?

-Thanks,
Joe

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


Re: C pointers/Python

2005-03-21 Thread Marcin Mika

 Can i do something like this?

 if code == CODE1:
 data = 0x0
 While True:
  len = len - 1
  if len == -1:
  break
  buffer = data

certainly not!
there are many things wrong with that.
first of all, as was pointed out already: this is highly un-pythonic.
secondly, its completely wrong. if you bind 'buffer' to another object,
then you have no chance of modifying its original contents. thats why i
stated in my previous post that IF you insist on persuing the
un-pythonic path then your function argument 'buffer' _must_ be a
mutable object which you must operate on through its methods, but you
CANNOT modify it with: buffer=data.
doing that will only re-bind 'buffer', it will never change anything
outside the function.

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