Discussion:
[Python.NET] Calling Python functions/objects forom C# code
Avi Mitrani
2013-06-24 13:19:18 UTC
Permalink
Dear mailing list members,



I'm new to PythonDotNet, and to integrating Python & C#, and I have to say
that I'm 'breaking my teeth'

with it; can't find the right examples on the web.



The context: I want to use the rich machine learning resources found in
python (like scikit-learn) inside a big project written in c#.



The problem: can't get started! - haven't even managed to call a function
yet.



Is there an example or tutorial you can direct me to?

Let's say that I want to call the function fib2 from the first example
module in python.org,

called (fibo.py):



-------------------------------------------------------

# Fibonacci numbers module



def fib(n): # write Fibonacci series up to n

a, b = 0, 1

while b < n:

print b,

a, b = b, a+b



def fib2(n): # return Fibonacci series up to n

result = []

a, b = 0, 1

while b < n:

result.append(b)

a, b = b, a+b

return result

-------------------------------------------------------





These lines compile J :

PythonEngine.Initialize();

PyObject fibmodule = PythonEngine.ImportModule("fibo");

PyObject fibfunc = fibmodule.GetAttr("fib2");



Where do I go from here? How do I send input and get the output into c#
array/list?

I guess I would need to use PyInt & PyList. How do I do that?



Thanks in advance,

Avi
Patrick Stewart
2013-07-03 14:32:02 UTC
Permalink
I’m new to PythonDotNet, and to integrating Python & C#, and I have to say
that I’m ‘breaking my teeth’
Is there an example or tutorial you can direct me to?
You need to do something like:
var res = PyTuple.AsTuple(fibmodule.InvokeMethod("fib2", new PyObject[1] { new
PyFloat(10) }));
double r = (double)res.GetItem(0).AsManagedObject(typeof(double));

This sucks, so I've made some modifications so that python can be called from
C# just like normal code, without all the hassle of .GetAttr and .Invoke.
Using my version of pythondotnet you can just run:

double r = fibmodule.fib2(10);

and it does the right thing.
You can find it here: https://github.com/patstew/pythonnet

Cheers,
Patrick

_________________________________________________
Python.NET mailing list - ***@python.org
http://ma
Kyle Rocha
2013-07-07 19:14:40 UTC
Permalink
Hey this is excellent, however after swapping to your branch from Tony
Roberts', It seems that a string returned from .net yields a list
rather than a string.

/ kyle
Post by Patrick Stewart
I’m new to PythonDotNet, and to integrating Python & C#, and I have to say
that I’m ‘breaking my teeth’
Is there an example or tutorial you can direct me to?
var res = PyTuple.AsTuple(fibmodule.InvokeMethod("fib2", new PyObject[1] { new
PyFloat(10) }));
double r = (double)res.GetItem(0).AsManagedObject(typeof(double));
This sucks, so I've made some modifications so that python can be called from
C# just like normal code, without all the hassle of .GetAttr and .Invoke.
double r = fibmodule.fib2(10);
and it does the right thing.
You can find it here: https://github.com/patstew/pythonnet
Cheers,
Patrick
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet

Loading...