does python could support sequence of short or int?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • momobear

    does python could support sequence of short or int?

    hi, is there a way to let python operate on sequence of int or short?
    In C, we just need declare a point, then I could get the point value,
    just like:
    short* k = buffer, //k is a point to a sequence point of short.
    short i = *k++,
    but python is a dynamic language,
    a = buffer
    i = ? I don't know how to continue, what's a? a seems to be a str?

  • Diez B. Roggisch

    #2
    Re: does python could support sequence of short or int?

    momobear schrieb:[color=blue]
    > hi, is there a way to let python operate on sequence of int or short?
    > In C, we just need declare a point, then I could get the point value,
    > just like:
    > short* k = buffer, //k is a point to a sequence point of short.
    > short i = *k++,
    > but python is a dynamic language,
    > a = buffer
    > i = ? I don't know how to continue, what's a? a seems to be a str?[/color]

    You should read the python tutorial, especially the parts about list,
    tuples and dicts.

    Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax an...


    Diez

    Comment

    • bruno at modulix

      #3
      Re: does python could support sequence of short or int?

      momobear wrote:[color=blue]
      > hi, is there a way to let python operate on sequence of int or short?
      > In C, we just need declare a point,[/color]

      I assume you mean a 'pointer'.
      [color=blue]
      > then I could get the point value,
      > just like:
      > short* k = buffer, //k is a point to a sequence point of short.
      > short i = *k++,
      > but python is a dynamic language,
      > a = buffer
      > i = ? I don't know how to continue, what's a? a seems to be a str?
      >[/color]

      <meta>
      You'd probably get better answers by exposing the problem you're trying
      to solve instead of what you think is the solution
      </meta>

      Anyway, if you want a list if integers, just put integers into a list
      and iterate over that list:

      buffer = [1, 2, 3, 42]
      for i in buffer:
      do_something_wi th(i)


      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • momobear

        #4
        Re: does python could support sequence of short or int?

        but what about buffer is not be declared in python program, it comes
        from a C function. and what about I want to treat a string as a short
        list?
        buffer = foobur() // a invoke from C function, it is encapsulated as a
        string
        but I want to treat it as a short list. how can I?

        Comment

        • bruno at modulix

          #5
          Re: does python could support sequence of short or int?

          momobear wrote:[color=blue]
          > but what about buffer is not be declared in python program, it comes
          > from a C function. and what about I want to treat a string as a short
          > list?[/color]

          <OT level='slightly '>
          There are no "short" in Python. An integer is an integer is an integer...
          </OT>
          [color=blue]
          > buffer = foobur() // a invoke from C function, it is encapsulated as a
          > string
          > but I want to treat it as a short list. how can I?[/color]

          I think you want the struct package from the standard lib:



          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          • Fuzzyman

            #6
            Re: does python could support sequence of short or int?


            momobear wrote:[color=blue]
            > hi, is there a way to let python operate on sequence of int or short?
            > In C, we just need declare a point, then I could get the point value,
            > just like:
            > short* k = buffer, //k is a point to a sequence point of short.
            > short i = *k++,
            > but python is a dynamic language,
            > a = buffer
            > i = ? I don't know how to continue, what's a? a seems to be a str?[/color]

            A byte sequence in Python is a 'str'. They are immutable, so any
            operations that change the object will return a new instance of 'str'.

            In Python although we use references to objects, they aren't pointers -
            so you don't operate directly on the byte sequence in memory. (Not
            using the basic datatypes anyway).

            Fuzzyman


            Comment

            • momobear

              #7
              Re: does python could support sequence of short or int?

              then how can I convert it to a int list? I read about struct and array,
              I think they are not suitable, since I don't know how long will the
              buffer is. I know if I write a plugins modules in C should works, but
              that's really upset to tell to myself there is no way in Python.

              Comment

              • Mirco Wahab

                #8
                Re: does python could support sequence of short or int?

                Hi momobear
                [color=blue]
                > then how can I convert it to a int list? I read about struct and array,
                > I think they are not suitable, since I don't know how long will the
                > buffer is. I know if I write a plugins modules in C should works, but
                > that's really upset to tell to myself there is no way in Python.[/color]

                this was imho a typical non-problem in
                "scripting languages" - regarding the
                areas where they are usually employed.

                there are some good exensions for Python,
                numarray and numpy and so on ...
                (http://numeric.scipy.org/)

                These provide C-like arrays and
                allow you writing programs entirely
                in Python w/C-speed on arrays.

                interesting: Perl6 will be the first
                (afaik) scripting language w/"type arrays"
                build into the core language
                e.g.
                sub hist(Int @vals) returns Array of Int {
                my Int @histogram;
                for @vals { @histogram[$_]++ }
                return @histogram;
                }
                (from http://www.jauu.net/data/foils/perl6/perl_6.html)

                Regards,

                M.

                Comment

                • Diez B. Roggisch

                  #9
                  Re: does python could support sequence of short or int?

                  momobear wrote:
                  [color=blue]
                  > then how can I convert it to a int list? I read about struct and array,
                  > I think they are not suitable, since I don't know how long will the
                  > buffer is. I know if I write a plugins modules in C should works, but
                  > that's really upset to tell to myself there is no way in Python.[/color]

                  You think wrong - they _are_ suitable. You can create the
                  struct.unpack-format string on the fly, which allows you to do this:

                  import struct
                  some_ints = range(1000)
                  v = struct.pack("%i h" % len(some_ints), *some_ints)
                  print struct.unpack(" %ih" % len(some_ints), v)

                  Regards,

                  Diez



                  Comment

                  • Richard Brodie

                    #10
                    Re: does python could support sequence of short or int?


                    "momobear" <wgwigw@gmail.c om> wrote in message
                    news:1143722234 .988260.201690@ t31g2000cwb.goo glegroups.com.. .
                    [color=blue]
                    > then how can I convert it to a int list? I read about struct and array,
                    > I think they are not suitable, since I don't know how long will the
                    > buffer is. I know if I write a plugins modules in C should works, but
                    > that's really upset to tell to myself there is no way in Python.[/color]

                    I'm not sure why you think that the array and struct modules are
                    unsuitable:
                    [color=blue][color=green][color=darkred]
                    >>>import array
                    >>>buffer = 'iidfkljkkkkhfp a3'
                    >>>arr = array.array('h' , buffer)
                    >>> print arr[/color][/color][/color]
                    array('h', [26985, 26212, 27755, 27498, 27499, 26731, 28774, 13153])



                    Comment

                    • Magnus Lycka

                      #11
                      Re: does python could support sequence of short or int?

                      momobear wrote:[color=blue]
                      > hi, is there a way to let python operate on sequence of int or short?[/color]

                      If you want to work with arrays of numbers, you might want to
                      look at NumArray etc.

                      Comment

                      • Magnus Lycka

                        #12
                        Re: does python could support sequence of short or int?

                        momobear wrote:[color=blue]
                        > but what about buffer is not be declared in python program, it comes
                        > from a C function. and what about I want to treat a string as a short
                        > list?[/color]

                        Python is a high level language with much stronger typing than C.
                        If you want a list of integers, use a list of integers. Strings
                        are for text. You might well extract numeric values from a string that
                        you get from another source, and you can use array or struct for that,
                        but don't use that while processing numeric values in Python.

                        Sorry, but when I hear you, I see the image of someone who shoves dirt
                        into the trunk of a car, goes over to the front, tries to lift it with
                        his bare hands, and complains that this is a really clunky wheel-barrow.
                        ;^)

                        Don't try to make Python into some kind of crippled C. It's much more
                        powerful than C if you use it as intended. Used backward, it will only
                        irritate you. I can well understand that you try to use C idioms if that
                        is what you know, but you should understand that this will often lead
                        you to bad Python solutions.

                        I don't quite understand how you intend that the interface between C
                        and Python would look. You can't just pass a raw C pointer to Python.
                        If you want that data to be managed by your C code, you need to provide
                        an API that you can access from Python that will make sense for Python.

                        If you e.g. pass a string as a return value from a wrapped C function,
                        you can e.g. use array or struct in Python to access it in a way that
                        makes sense in Python.

                        Assuming that your C function returns a string with 2 byte integers like
                        this:[color=blue][color=green][color=darkred]
                        >>> s[/color][/color][/color]
                        '\x01\x00\x02\x 00\x03\x00\x04\ x00\x05\x00\x06 \x00\x07\x00\x0 8\x00\t\x00'

                        You can easily make a list like this:
                        [color=blue][color=green][color=darkred]
                        >>> import struct
                        >>> l = list(struct.unp ack('h'*(len(s)/2), s))
                        >>> l[/color][/color][/color]
                        [1, 2, 3, 4, 5, 6, 7, 8, 9]

                        If you don't need to manipulate it, you can skip the 'list()'
                        part and get an immutable tuple instead.

                        The advantage with struct over array in s case like this (as far as I
                        understand) is that you have control over endianness.

                        Comment

                        • Alex Martelli

                          #13
                          Re: does python could support sequence of short or int?

                          bruno at modulix <onurb@xiludom. gro> wrote:
                          [color=blue]
                          > momobear wrote:[color=green]
                          > > but what about buffer is not be declared in python program, it comes
                          > > from a C function. and what about I want to treat a string as a short
                          > > list?[/color]
                          >
                          > <OT level='slightly '>
                          > There are no "short" in Python. An integer is an integer is an integer...
                          > </OT>[/color]

                          However, standard library module 'array' does let you pack a compact
                          sequence of integers taking two bytes each.
                          [color=blue][color=green]
                          > > buffer = foobur() // a invoke from C function, it is encapsulated as a
                          > > string
                          > > but I want to treat it as a short list. how can I?[/color]
                          >
                          > I think you want the struct package from the standard lib:
                          > http://www.python.org/doc/2.4.2/lib/module-struct.html[/color]

                          I think he wants array instead.


                          Alex

                          Comment

                          • momobear

                            #14
                            Re: does python could support sequence of short or int?

                            thanks for help. formerly I only know to use the struct like bellow:[color=blue][color=green][color=darkred]
                            >>> unpack('hhl', '\x00\x01\x00\x 02\x00\x00\x00\ x03')[/color][/color][/color]
                            Great, python can work like this:[color=blue][color=green][color=darkred]
                            >>> l = list(struct.unp ack('h'*(len(s)/2), s))[/color][/color][/color]
                            I will try to use the numarray also[color=blue][color=green][color=darkred]
                            >>>there are some good exensions for Python,
                            >>>numarray and numpy and so on ...
                            >>>(http://numeric.scipy.org/)[/color][/color][/color]

                            Comment

                            Working...