C pointers/Python

Discussion in 'Python' started by joe@gmail.com, Mar 21, 2005.

  1. Guest

    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
    , Mar 21, 2005
    #1
    1. Advertising

  2. On 21 Mar 2005 15:52:44 -0800, <> 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
    Stephen Thorne, Mar 22, 2005
    #2
    1. Advertising

  3. integer Guest

    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
    integer, Mar 22, 2005
    #3
  4. On 21 Mar 2005 17:16:18 -0800, integer <> 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
    Stephen Thorne, Mar 22, 2005
    #4
  5. Marcin Mika Guest

    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.
    Marcin Mika, Mar 22, 2005
    #5
  6. Guest

    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
    , Mar 22, 2005
    #6
  7. Marcin Mika Guest


    > 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.
    Marcin Mika, Mar 22, 2005
    #7
  8. "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)
    Lonnie Princehouse, Mar 22, 2005
    #8
  9. On 21 Mar 2005 19:32:20 -0800, <> 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
    Stephen Thorne, Mar 23, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    638
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    749
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    436
    Alan Kelon
    Oct 29, 2003
  4. Xamalek
    Replies:
    7
    Views:
    679
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    652
Loading...

Share This Page