Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > C pointers/Python

Reply
Thread Tools

C pointers/Python

 
 
joe@gmail.com
Guest
Posts: n/a
 
      03-21-2005
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
case CODE2 :
.....

}
How does python take care of pointers? Any help with python pointer
code appreciated.
-Joe

 
Reply With Quote
 
 
 
 
Stephen Thorne
Guest
Posts: n/a
 
      03-22-2005
On 21 Mar 2005 15:52:44 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
integer
Guest
Posts: n/a
 
      03-22-2005
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

 
Reply With Quote
 
Stephen Thorne
Guest
Posts: n/a
 
      03-22-2005
On 21 Mar 2005 17:16:18 -0800, integer <(E-Mail Removed)> 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
 
Reply With Quote
 
Marcin Mika
Guest
Posts: n/a
 
      03-22-2005
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.

 
Reply With Quote
 
joe@gmail.com
Guest
Posts: n/a
 
      03-22-2005
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

 
Reply With Quote
 
Marcin Mika
Guest
Posts: n/a
 
      03-22-2005

> 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.

 
Reply With Quote
 
Lonnie Princehouse
Guest
Posts: n/a
 
      03-22-2005
"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)

 
Reply With Quote
 
Stephen Thorne
Guest
Posts: n/a
 
      03-23-2005
On 21 Mar 2005 19:32:20 -0800, (E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments