473,395 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

extending python with array functions

I want to make numerical functions that can be called from python.
I am programming in pascal the last few decades so I had a look at
"python for delphi" (P4D). The demo09 gives as example add(a,b) using
integers and pyarg_parsetuple. That works!

However, I cannot figure out what to do when a, b and the result are
arrays (or matrices) of float (for i:=0 to high(a) do c[i]:=a[i]+b[i];
and then return c to python). Although from the ALGOL60 school and
always tried to keep far from pointers, I might also understand advise in C.

Please get me started e.g. by giving a simple example.
Many thanks,
Janwillem
Feb 4 '08 #1
3 1763
On Mon, 04 Feb 2008 20:56:02 -0200, Gabriel Genellina wrote:
- the array module http://docs.python.org/lib/module-array.html provides
homogeneuos arrays that may be more efficient for your application. arrays
don't have a special API, you have to import the module and use its
functions the same as one would do in pure Python.
There's one special thing about it: the `buffer_info()` method returns a
tuple with the memory address and length (in items) of the current
underlying buffer. Pretty useless information in Python but handy in
extensions that can directly access the "raw" memory.

To the OP: Since PythonÂ*2.5 the `ctypes` module is another way to
interface with "native" code in dynamic libraries from the standard
library.

Ciao,
Marc 'BlackJack' Rintsch
Feb 5 '08 #2
En Tue, 05 Feb 2008 05:28:33 -0200, Marc 'BlackJack' Rintsch
<bj****@gmx.netescribi�:
On Mon, 04 Feb 2008 20:56:02 -0200, Gabriel Genellina wrote:
>- the array module http://docs.python.org/lib/module-array.html provides
homogeneuos arrays that may be more efficient for your application.
arrays
don't have a special API, you have to import the module and use its
functions the same as one would do in pure Python.

There's one special thing about it: the `buffer_info()` method returns a
tuple with the memory address and length (in items) of the current
underlying buffer. Pretty useless information in Python but handy in
extensions that can directly access the "raw" memory.
Good to know! I didn't notice it the (only) time I had to use arrays from
C code.

--
Gabriel Genellina

Feb 5 '08 #3
Gabriel Genellina wrote:
En Tue, 05 Feb 2008 05:28:33 -0200, Marc 'BlackJack' Rintsch
<bj****@gmx.netescribi�:
>On Mon, 04 Feb 2008 20:56:02 -0200, Gabriel Genellina wrote:
>>- the array module http://docs.python.org/lib/module-array.html provides
homogeneuos arrays that may be more efficient for your application.
arrays
don't have a special API, you have to import the module and use its
functions the same as one would do in pure Python.

There's one special thing about it: the `buffer_info()` method returns a
tuple with the memory address and length (in items) of the current
underlying buffer. Pretty useless information in Python but handy in
extensions that can directly access the "raw" memory.

Good to know! I didn't notice it the (only) time I had to use arrays
from C code.
Thanks for the advice. I think I will read the cstype stuff because it
might mean that my calculation intensive functions (non-linear systems
and Monte Carlo stuff) can be kept unchanged ans stay compatible with
e.g. the Excel interfaces I have for them. My first attempt with
sum(i,j) was successful but sum(x,y) (FORTRAN typecast thinking) needs
apparently understanding ctypes.
Janwillem
Feb 5 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John D | last post by:
I'm writing a class definition in python with some array atributes from Numeric 2.0. I'm using UserArray, UserList, to subclass my classes. But some of the methods I need to write in C mainly...
3
by: stefan | last post by:
Hi Folks, I currenty extended some of my C++ functionality to python and also embedded python to use python functionality in my C++ system (and use as well these extended functions). While...
6
by: Eli | last post by:
Hi, I've followed the Python docs about extending the Python interperter and created an extension library. I've added my functions like this: static PyMethodDef pmylib_methods = { {"foo",...
2
by: Henri Sivonen | last post by:
In order to keep subsequent code clear, I added a new method like this HTMLDivElement.prototype.foo = function(arg) { // foo for HTML div trees } Element.prototype.foo = function(arg) { //...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
3
by: Redefined Horizons | last post by:
I'm trying to understand the argument flags that are used in the method table of an extension module written in C. First let me ask this question about the method table. Is it an C array named...
1
by: punitha | last post by:
hai, Iam using python to tie C functions(premitives). Iam calling C functions from python through swig(Extending with C). This working well in Linux,but i dont know how to do it in...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
0
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
Python an easy to program language becomes very powerful when they interact close with your system,for this purpose he gets friendly with this guy named C. Python establishes tightly bound wrappers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.