Compile python code into a dll

2012-09-10 Thread Rolf Wester

Hi,

I have Python code that I would like to compile into a dll (I have to 
deliver a C/C++ callable dll and I don't want to reimpelement the Python 
code in C/C++). It's not for extending Python but I want to call the 
Python functions and classes from C/C++. It's more like extending C/C++ 
with Python. I would be very appreciative for any help.


Thank you in advance

Regards
Rolf
--
http://mail.python.org/mailman/listinfo/python-list


Re: Compile python code into a dll

2012-09-11 Thread Rolf Wester

Thank you all for your help. I'm going to try Cython.

Regards
Rolf


On 10/09/12 14:15, Rolf Wester wrote:

Hi,

I have Python code that I would like to compile into a dll (I have to
deliver a C/C++ callable dll and I don't want to reimpelement the Python
code in C/C++). It's not for extending Python but I want to call the
Python functions and classes from C/C++. It's more like extending C/C++
with Python. I would be very appreciative for any help.

Thank you in advance

Regards
Rolf


--
http://mail.python.org/mailman/listinfo/python-list


exec

2012-03-01 Thread Rolf Wester
Hi,

I would like to define methods using exec like this:

class A:
def __init__(self):
cmd = "def sqr(self, x):\nreturn x**2\nself.sqr = sqr\n"
exec cmd
a = A()
print a.sqr(a, 2)

This works, but I have to call sqr with a.sqr(a, 2), a.sqr(2) does not work
(TypeError: sqr() takes exactly 2 arguments (1 given)).

Is there a possibility to define methods using exec and getting normal behavior?

I would be very appreciative for any help.

With kind regards
Rolf Wester


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exec

2012-03-01 Thread Rolf Wester
Hi,

thanks for your replies so far.

The reason to use exec is just laziness. I have quite a lot of classes
representing material data and every class has a number of parameters.
The parameter are Magnitude objects (they have a value and a unit and overloaded
special functions to correctly handle the units). I want to have getter
functions that either return the Magnitude object or just the value:

iron = Iron()
iron.rho(0) => value
iron.rho() => Magnitude object

def rho(self, uf=1):
if uf == 1:
return self._rho
else:
return self._rho.val

And because this would mean quite a lot of writing I tried to do it with exec.

With kind regards
Rolf Wester
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exec

2012-03-01 Thread Rolf Wester
Thank you, that really made things much easier and admittedly much less nasty 
too.

Regards
Rolf

On 01/03/12 18:14, Peter Otten wrote:
> Rolf Wester wrote:
> 
>> The reason to use exec is just laziness. I have quite a lot of classes
>> representing material data and every class has a number of parameters.
>> The parameter are Magnitude objects (they have a value and a unit and
>> overloaded special functions to correctly handle the units). I want to
>> have getter functions that either return the Magnitude object or just the
>> value:
>>
>> iron = Iron()
>> iron.rho(0) => value
>> iron.rho() => Magnitude object
>>
>> def rho(self, uf=1):
>> if uf == 1:
>> return self._rho
>> else:
>> return self._rho.val
>>
>> And because this would mean quite a lot of writing I tried to do it with
>> exec.
> 
> Make the Magnitude class callable then:
> 
>>>> class Magnitude(object):
> ... def __init__(self, value):
> ... self.value = value
> ... def __call__(self, uf=1):
> ... if uf == 1:
> ... return self
> ... return self.value
> ...
>>>> class Iron(object):
> ... def __init__(self):
> ... self.rho = Magnitude(42)
> ...
>>>> iron = Iron()
>>>> iron.rho()
> <__main__.Magnitude object at 0x7fb94062be10>
>>>> iron.rho(0)
> 42
> 
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Python extension module segmentation fault

2005-10-21 Thread Rolf Wester
Hi,

I' trying to make an extension module that passes Numeric arrays. The
wrapper function is (swig generated and modified by myself):

static PyObject *_wrap_my_func(PyObject *self, PyObject *args) {
PyObject * resultobj = 0 ;
PyObject * obj0 = 0 ;
PyArrayObject * mat = 0 ;

std::cout << __FILE__ << "  " <<  __LINE__ << std::endl;
if(!PyArg_ParseTuple(args,(char *)"O:my_func",&obj0)) goto fail;
std::cout << __FILE__ << "  " <<  __LINE__ <<  "  " << obj0 << 
std::endl;
mat = (PyArrayObject *) PyArray_ContiguousFromObject(obj0,
PyArray_DOUBLE, 1, 1);
std::cout << __FILE__ << "  " <<  __LINE__ <<  "  " << mat << std::endl;

Py_INCREF(Py_None); resultobj = Py_None;
return resultobj;
fail:
return NULL;
}

The shared object is build with:

g++ -c -g -fPIC -I./  -I/usr/local/include/python2.4
-I/usr/local/include/python2.4/Numeric  mytest_wrap.cpp -o mytest_wrap.o

g++ -shared -L/usr/local/lib/python2.4/config/  mytest_wrap.o
-lpython2.4 -lm   -o _mytest.so


the Python file reads:

import _mytest
from Numeric import *
mat = ones(100,Float64)
print _mytest.my_func(mat)

When running this I get the output:

mytest_wrap.cpp  1499
mytest_wrap.cpp  1502  0x402b55e8
Speicherzugriffsfehler (segmentation fault)

I also ran this with valgrind. Part of valgrinds output is:

==15792== Reading syms from
/mnt/pubdsk/A31/2003/DOKUMENTATION/WESTER/pr3/OPT/opt2.0/test/_mytest.so
(0x1BE7E000)
==15792== Reading syms from
/usr/local/lib/python2.4/site-packages/Numeric/multiarray.so (0x1B90F000)
==15792== Reading syms from
/usr/local/lib/python2.4/site-packages/Numeric/_numpy.so (0x1BFDB000)
==15792== Reading syms from
/usr/local/lib/python2.4/site-packages/Numeric/umath.so (0x1BFF1000)
==15792== Reading syms from
/usr/local/lib/python2.4/lib-dynload/strop.so (0x1B91A000)
==15792== Reading syms from /usr/local/lib/python2.4/lib-dynload/math.so
(0x1C103000)
==15792== Reading syms from
/usr/local/lib/python2.4/lib-dynload/struct.so (0x1C209000)
==15792== Reading syms from
/usr/local/lib/python2.4/lib-dynload/binascii.so (0x1C21)
==15792== Reading syms from
/usr/local/lib/python2.4/lib-dynload/cStringIO.so (0x1C216000)
mytest_wrap.cpp  1499
mytest_wrap.cpp  1502  0x1bca7610
==15792== Invalid read of size 4
==15792==at 0x1BECE794: _wrap_my_func (mytest_wrap.cpp:1503)
==15792==by 0x811E685: PyCFunction_Call (methodobject.c:93)
==15792==by 0x80C708F: PyEval_EvalFrame (ceval.c:1499)
==15792==by 0x80C8933: PyEval_EvalCodeEx (ceval.c:2736)
==15792==by 0x80C8B64: PyEval_EvalCode (ceval.c:484)
==15792==by 0x80F74A7: PyRun_SimpleFileExFlags (pythonrun.c:1265)
==15792==by 0x80558D6: Py_Main (main.c:484)
==15792==by 0x8054F86: main (python.c:23)
==15792==  Address 0x38 is not stack'd, malloc'd or (recently) free'd
==15792==
==15792== Process terminating with default action of signal 11 (SIGSEGV)
==15792==  Access not within mapped region at address 0x38
==15792==at 0x1BECE794: _wrap_my_func (mytest_wrap.cpp:1503)
==15792==by 0x811E685: PyCFunction_Call (methodobject.c:93)
==15792==by 0x80C708F: PyEval_EvalFrame (ceval.c:1499)
==15792==by 0x80C8933: PyEval_EvalCodeEx (ceval.c:2736)
==15792==by 0x80C8B64: PyEval_EvalCode (ceval.c:484)
==15792==by 0x80F74A7: PyRun_SimpleFileExFlags (pythonrun.c:1265)
==15792==by 0x80558D6: Py_Main (main.c:484)
==15792==by 0x8054F86: main (python.c:23)
==15792==
==15792== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 8093 from 7)
==15792==


I would be very appreciative for any help.

With kind regards

Rolf Wester




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python extension module segmentation fault

2005-10-21 Thread Rolf Wester
Robert Kern wrote:

> 
> 
> Did you call import_array() in init_mytest()?
> 
No I didn't. Thank you very much for your help. Now it works.

With kind regards

Rolf Wester
-- 
http://mail.python.org/mailman/listinfo/python-list


run region in IDLE

2005-01-02 Thread Rolf Wester
Hi,
I would like to send a marked region of an IDLE editing window to the 
IDLE shell for evaluating, so that I can develop software incrementally.
I know that I could type directly into the shell window (or copy and 
paste between an editing window and the shellwindow) but I'm looking for 
a way of doing it like it's possible with Emacs and Python. Is there any 
way of doing this?

Regards
Rolf Wester
--
http://mail.python.org/mailman/listinfo/python-list


IDLE question

2004-12-26 Thread Rolf Wester
Hi,
I would like to use IDLE as interactively as I can with Emacs. In Emacs 
I can send a marked region to the Python interpreter. Is there any way 
to do the same thing with IDLE?

Thank you in advance
Regards
Rolf Wester
--
http://mail.python.org/mailman/listinfo/python-list


Strange behaviour of Numeric Float32 array?

2006-07-12 Thread Rolf Wester
Hi,

the code:

from Numeric import *

def my_minimum(a):
n=shape(a)[0]
x = 1.0e20
for i in range(n):
if a[i] < x:
x = a[i]
return x



def strange(a):
a[3] = -6303.0
h = my_minimum(a)
for i in range(10):
print i,a[i],
a[i] = a[i] - h
print a[i],h

a = zeros(10,Float32)
strange(a)
b = zeros(10,Float64)
strange(b)

produces the output:

0 0.0 6303.0 -6303.0
1 0.0 6303.0 -6303.0
2 0.0 6303.0 -6303.0
3 -6303.0 0.0 0.0
4 0.0 0.0 0.0
5 0.0 0.0 0.0
6 0.0 0.0 0.0
7 0.0 0.0 0.0
8 0.0 0.0 0.0
9 0.0 0.0 0.0
0 0.0 6303.0 -6303.0
1 0.0 6303.0 -6303.0
2 0.0 6303.0 -6303.0
3 -6303.0 0.0 -6303.0
4 0.0 6303.0 -6303.0
5 0.0 6303.0 -6303.0
6 0.0 6303.0 -6303.0
7 0.0 6303.0 -6303.0
8 0.0 6303.0 -6303.0
9 0.0 6303.0 -6303.0

Can anybody tell me why in the Float32 version h is changed?
Thank you in advance.

Regards

Rolf
-- 
http://mail.python.org/mailman/listinfo/python-list


concatenating numpy arrays

2006-10-31 Thread Rolf Wester
Hi,

I want to concatenate two numpy arrays with shape (n1,n2) and (n1,n3) 
into a single array with shape (n1,n2+n3). I guess there is an elegant 
way to do this but I couldn't figure it out. So any help is very much 
appreciated.

Regards

Rolf
-- 
http://mail.python.org/mailman/listinfo/python-list


Is there a Python __LINE__ ?

2006-07-06 Thread Rolf Wester
Hi,

is there a Python aquivalent to the C __LINE__?

Thank you in advance

Regards

Rolf
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a Python __LINE__ ?

2006-07-06 Thread Rolf Wester
Thank you very much for your help.

Regards

Rolf
-- 
http://mail.python.org/mailman/listinfo/python-list


plotting with Python

2005-05-30 Thread Rolf Wester
Hi,

I have a Python console application that is intended to be used 
interactively and I have to add plotting capabilities (multiple XY plots 
and if possible 2D-surface plots). I'm loocking for a reasonably fast 
plotting library (not GPL'ed, needs not be for free) that can be used 
under Windows. An alternative would also be a standalone application 
that can be controlled via TCP/IP from my Python application. I tried 
matplotlib but this is not fast enough (especially under Windows). I 
considered PyQwt but this is GPL'ed. So I would be very appreciative for 
any help.

With kind regards

Rolf Wester
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: plotting with Python

2005-05-31 Thread Rolf Wester
Philippe C. Martin wrote:
> Look at wxPython
> 
> Regards,
> 
> Philippe
> 
> 
I will do it, thank you for your reply.

Rolf

> 
> Rolf Wester wrote:
> 
> 
>>Hi,
>>
>>I have a Python console application that is intended to be used
>>interactively and I have to add plotting capabilities (multiple XY plots
>>and if possible 2D-surface plots). I'm loocking for a reasonably fast
>>plotting library (not GPL'ed, needs not be for free) that can be used
>>under Windows. An alternative would also be a standalone application
>>that can be controlled via TCP/IP from my Python application. I tried
>>matplotlib but this is not fast enough (especially under Windows). I
>>considered PyQwt but this is GPL'ed. So I would be very appreciative for
>>any help.
>>
>>With kind regards
>>
>>Rolf Wester
> 
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: plotting with Python

2005-05-31 Thread Rolf Wester
[EMAIL PROTECTED] wrote:
> We use dislin in my lab. I don't think it's GPL...
> 
> http://www.linmpi.mpg.de/dislin
> 
Hi,

thank you for your reply. I tried dislin but this didn't work very well 
for me. But I will try it again.

Regards


Rolf

-- 
http://mail.python.org/mailman/listinfo/python-list


Memory problem

2007-11-15 Thread Rolf Wester
Hi,

I have a strange (for me) memory problem. When running a loop in a 
Python program memory usage increases from about 4% up to 100%. I do a 
gc.collect() every loop cycle but this doesn't help. There are about 
67000 objects that are tracked by the garbage collector. This number 
does vary a little bit but does not increase on average whereas the 
memory usage does. The number of garbage objects (len(gc.garbage()) is 
zero. This is a very severe problem for my application, so I would be 
very appreciative for any help.

With kind regards

Rolf

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Memory problem

2007-11-15 Thread Rolf Wester
Sorry, of course your are wright. I'm running Python2.5 on Linux, my 
program imports numpy, matplotlib, sys and a python module of my own.
This module uses numpy and scipy.weave for imbedded C-code but no 
extension modules. I though the code to be to large to show, I hoped you 
could give me hint on what I could try.

Thank you in advance

Rolf

Diez B. Roggisch wrote:
> Rolf Wester wrote:
> 
>> Hi,
>>
>> I have a strange (for me) memory problem. When running a loop in a
>> Python program memory usage increases from about 4% up to 100%. I do a
>> gc.collect() every loop cycle but this doesn't help. There are about
>> 67000 objects that are tracked by the garbage collector. This number
>> does vary a little bit but does not increase on average whereas the
>> memory usage does. The number of garbage objects (len(gc.garbage()) is
>> zero. This is a very severe problem for my application, so I would be
>> very appreciative for any help.
> 
> well, it might be that the relative moon humidity resonates with the
> loop-cycle, creating quantum-flux in the main memory banks.
> 
> Or not.
> 
> Seriously - what help do you expect without showing us _any_ code or at
> least getting into details like usage of libs, python-version, possible
> C-extensions, platform, 
> 
> Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Memory problem

2007-11-16 Thread Rolf Wester
Hi,

thank you for your comments and your hints (I probably deserve some kind 
of subtle irony). I found the problem:

I thought a numpy array A has shape (n,) but actually it had shape 
(n,1). In the loop I sampled a value from that array:

v.append(A[i])

So what happened was that I got a view of A not a single number and 
append obviously appended the whole array not just a single element 
array. And n is about 64000.

So sorry for bothering you with that stupid fault.

Regards

Rolf
-- 
http://mail.python.org/mailman/listinfo/python-list


Tool for translating Matlab to Python?

2007-12-04 Thread Rolf Wester
Hi,

is there a tool to automatically translate Matlab source to Python/numpy 
code?

Regards

Rolf
-- 
http://mail.python.org/mailman/listinfo/python-list


Strange constructor behaviour (or not?)

2006-04-28 Thread Rolf Wester
Hi,

when defining:

class A:
def __init__(self, l=[]):
self.l = l
a = A()
a.l.append()
b = A()
print a.l

I get the output

[]

instead of an empty list. I guess it's because the default value in the 
constructor is constructed once and whenever the constructor is called 
without l being specified.

My work around is:

class A:
def __init__(self, l=None):
if l == None:
self.l = []
else:
self.l = l

Is there a way to take the first definition but force the constructor to 
create a new empty list every time it is called?

Thanks in advance

Rolf Wester
-- 
http://mail.python.org/mailman/listinfo/python-list


C-extension 2 times slower than exe

2009-06-23 Thread Rolf Wester
Hi,

I have a C++ program that I would like to steer using Python. I made the
wrapper using swig and linked the code (without the main function) into
a shared object. My Python script loads the extension and calls a
function of the C-extension, the rest runs entirely within the
C-extension. For comparison I compiled the code including the main
function with the same compilation options and linked all into an exe.
The main function of the exe calls the same function as my Python script
does. Surprisingly the code in the Python C-extension runs twice as long
as the same code in the exe. Does anyone know what could be the reason
for this behaviour?

Thank you in advance

Rolf
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C-extension 2 times slower than exe

2009-06-23 Thread Rolf Wester
Philip Semanchuk wrote:
> 
> On Jun 23, 2009, at 9:51 AM, Rolf Wester wrote:
> 
>> Hi,
>>
>> I have a C++ program that I would like to steer using Python. I made the
>> wrapper using swig and linked the code (without the main function) into
>> a shared object. My Python script loads the extension and calls a
>> function of the C-extension, the rest runs entirely within the
>> C-extension. For comparison I compiled the code including the main
>> function with the same compilation options and linked all into an exe.
>> The main function of the exe calls the same function as my Python script
>> does. Surprisingly the code in the Python C-extension runs twice as long
>> as the same code in the exe. Does anyone know what could be the reason
>> for this behaviour?
> 
> If the runtime of the C/C++ code is short, the time spent initializing
> the Python interpreter might have a big impact on the runtime of the
> Python version.
> 
> 
The runtime is about 2.5 sec and 5.0 sec respectively. I not only use
the time command to measure the time consumed but I also measure the
time within the C-code using clock() and get similar result. So the
Python startup time is not the reason for the runtime difference I
found. Thank you anyway.

Rolf
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C-extension 2 times slower than exe

2009-06-24 Thread Rolf Wester
Hello,

thank you all very much for your replies.

I tried to simplify things and make the two versions as comparable as
possible. I put the C++ part of the program into a shared object
libff.so. For the exe the main function is linked against this shared
object. For the python stuff I made an interface consiting of only one
function call_solver with the same code that has the main function used
for the exe. Then I created a wrapper for this interface using swig and
linked interface.o, ff_warp.o and libff.so into _ff.so. The Python code
just imports _ff and calls call_solver wich creates an object of the
class Solver and calls its member solve (the main function of the exe
does the same).

I included some code for timing into the C++-code.

#include 

//beginning of solve
clock_t t0 = clock();
...
clock_t t1 = clock();
//end of solve
cout << "time used = " << (t1-t0)/CLOCKS_PER_SEC << endl;

I'm using gcc4.5 (latest snapshot) and Python2.6 under Suse 10.3. The
sources are compiled using the flags -fPIC -O3.

Timing:

1) time python ff.py
time used = 3.74
real0m3.234s
user0m3.712s
sys 0m0.192s
2) time ff
time used = 2.19
real0m3.170s
user0m2.088s
sys 0m0.168s

I tried some other optimizations:

-O0:

1)
time used = 21.91
real0m21.568s
user0m22.001s
sys 0m0.160s

2)
time used = 20.87
real0m22.206s
user0m20.989s
sys 0m0.148s

-O1
1)
time used = 4.04
real0m3.660s
user0m4.000s
sys 0m0.160s

2)
time used = 2.72
real0m3.454s
user0m2.648s
sys 0m0.164s

It seems that there is an overhead of about 2 secs within the C++-code.
I'm going to try this out with a problem that takes much more time than
the present one.

Regards

Rolf

Carl Banks wrote:
> On Jun 23, 7:20 am, Rolf Wester  wrote:
>> Philip Semanchuk wrote:
>>
>>> On Jun 23, 2009, at 9:51 AM, Rolf Wester wrote:
>>>> Hi,
>>>> I have a C++ program that I would like to steer using Python. I made the
>>>> wrapper using swig and linked the code (without the main function) into
>>>> a shared object. My Python script loads the extension and calls a
>>>> function of the C-extension, the rest runs entirely within the
>>>> C-extension. For comparison I compiled the code including the main
>>>> function with the same compilation options and linked all into an exe.
>>>> The main function of the exe calls the same function as my Python script
>>>> does. Surprisingly the code in the Python C-extension runs twice as long
>>>> as the same code in the exe. Does anyone know what could be the reason
>>>> for this behaviour?
>>> If the runtime of the C/C++ code is short, the time spent initializing
>>> the Python interpreter might have a big impact on the runtime of the
>>> Python version.
>> The runtime is about 2.5 sec and 5.0 sec respectively. I not only use
>> the time command to measure the time consumed but I also measure the
>> time within the C-code using clock() and get similar result. So the
>> Python startup time is not the reason for the runtime difference I
>> found. Thank you anyway.
> 
> We can't really help you much unless you give us more details about
> what you did (how did you built it, how did you time it, and how are
> you calling the C extension from Python).  All we can do is throw out
> possible ideas, and I will toss out a few, but if that's not it you'll
> have to post details.
> 
> Are you building the extension with the same optimization flags as the
> compiler?
> 
> Are you calling the C code repeatedly from inside a Python loop?
> 
-- 
http://mail.python.org/mailman/listinfo/python-list