does python could support sequence of short or int?

M

momobear

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

Diez B. Roggisch

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

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

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

Diez
 
B

bruno at modulix

momobear said:
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)
 
M

momobear

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

bruno at modulix

momobear said:
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...
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
 
F

Fuzzyman

momobear said:
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
 
M

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

Mirco Wahab

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

Diez B. Roggisch

momobear said:
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
 
R

Richard Brodie

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:
array('h', [26985, 26212, 27755, 27498, 27499, 26731, 28774, 13153])
 
M

Magnus Lycka

momobear said:
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.
 
M

Magnus Lycka

momobear said:
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
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:'\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:
[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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top