470,826 Members | 1,818 Online

# 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?

Mar 30 '06 #1
13 1834
momobear schrieb:
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?

tuples and dicts.

http://docs.python.org/tut/

Diez
Mar 30 '06 #2
momobear wrote:
hi, is there a way to let python operate on sequence of int or short?
In C, we just need declare a point,
I assume you mean a 'pointer'.
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?

<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_with(i)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Mar 30 '06 #3
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?

Mar 30 '06 #4
momobear wrote:
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?
<OT level='slightly'>
There are no "short" in Python. An integer is an integer is an integer...
</OT>
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?

I think you want the struct package from the standard lib:
http://www.python.org/doc/2.4.2/lib/module-struct.html
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Mar 30 '06 #5

momobear wrote:
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?

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
http://www.voidspace.org.uk/python/index.shtml

Mar 30 '06 #6
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.

Mar 30 '06 #7
Hi momobear
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.

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.
Mar 30 '06 #8
momobear wrote:
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.

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("%ih" % len(some_ints), *some_ints)
print struct.unpack("%ih" % len(some_ints), v)

Regards,

Diez

Mar 30 '06 #9

"momobear" <wg****@gmail.com> wrote in message
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.

I'm not sure why you think that the array and struct modules are
unsuitable:
import array
buffer = 'iidfkljkkkkhfpa3'
arr = array.array('h', buffer)
print arr

array('h', [26985, 26212, 27755, 27498, 27499, 26731, 28774, 13153])

Mar 30 '06 #10
momobear wrote:
hi, is there a way to let python operate on sequence of int or short?

If you want to work with arrays of numbers, you might want to
look at NumArray etc.
Mar 30 '06 #11
momobear wrote:
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?

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

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:
s '\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\ x07\x00\x08\x00\t\x00'

You can easily make a list like this:
import struct
l = list(struct.unpack('h'*(len(s)/2), s))
l

[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.
Mar 30 '06 #12
bruno at modulix <on***@xiludom.gro> wrote:
momobear wrote:
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?

<OT level='slightly'>
There are no "short" in Python. An integer is an integer is an integer...
</OT>

However, standard library module 'array' does let you pack a compact
sequence of integers taking two bytes each.
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?

I think you want the struct package from the standard lib:
http://www.python.org/doc/2.4.2/lib/module-struct.html

I think he wants array instead.
Alex
Mar 30 '06 #13
thanks for help. formerly I only know to use the struct like bellow:
unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03') Great, python can work like this: l = list(struct.unpack('h'*(len(s)/2), s)) I will try to use the numarray alsothere are some good exensions for Python,
numarray and numpy and so on ...
(http://numeric.scipy.org/)

Mar 31 '06 #14

### This discussion thread is closed

Replies have been disabled for this discussion.