RANGE object access from Python
Hello, guys, I have a C# object, I need access to it's properties in Python 2.7. I got this type returned from a C# dll: <class 'Philips.CT.Host.ModelAbstraction.DataStructures.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'> This should be a "Range" data type in C#. Thanks a lot for any suggestion! Alex.
Hi Alex, Not sure about your class but you can use codecs to create custom code to convert your objects from .Net to Python. Doc : https://github.com/pythonnet/pythonnet/wiki/Codecs:-customizing-object-marsh... Here is how I use it: public class DoubleArrayPythonEncoder : IPyObjectEncoder { public static DoubleArrayPythonEncoder Instance { get; } = new DoubleArrayPythonEncoder(); public bool CanEncode(Type type) { return type == typeof(double[]); } public PyObject TryEncode(object value) { if (value is double[] doublearray) { var pyList = new PyList(); for (int i = 0; i < doublearray.Length - 1; i++) { pyList.Append(new PyFloat(doublearray[i])); } return pyList; } return null; } public static void Register() { PyObjectConversions.RegisterEncoder(Instance); } } In Python, just call Register() before getting object. I hope it helps! Regards, *Emmanuel Rutovic* Founder, AESIM.tech manu@simba.io +1 (438) 926-6458 On Dec 2, 2020 at 2:37:39 PM, AlexM <alexmasis@gmail.com> wrote:
_______________________________________________ PythonNet mailing list -- pythonnet@python.org To unsubscribe send an email to pythonnet-leave@python.org https://mail.python.org/mailman3/lists/pythonnet.python.org/ Member address: manu@upsim.tech
Thank you very much Emmanuel ! I will try your idea, think this should work ... Take care! Alex. On Wed, Dec 2, 2020 at 3:35 PM Emmanuel Rutovic <manu@aesim.tech> wrote:
Hi Alex,
Not sure about your class but you can use codecs to create custom code to convert your objects from .Net to Python.
Doc : https://github.com/pythonnet/pythonnet/wiki/Codecs:-customizing-object-marsh...
Here is how I use it:
public class DoubleArrayPythonEncoder : IPyObjectEncoder
{
public static DoubleArrayPythonEncoder Instance { get; } = new DoubleArrayPythonEncoder();
public bool CanEncode(Type type)
{
return type == typeof(double[]);
}
public PyObject TryEncode(object value)
{
if (value is double[] doublearray)
{
var pyList = new PyList();
for (int i = 0; i < doublearray.Length - 1; i++)
{
pyList.Append(new PyFloat(doublearray[i]));
}
return pyList;
}
return null;
}
public static void Register()
{
PyObjectConversions.RegisterEncoder(Instance);
}
}
In Python, just call Register() before getting object.
I hope it helps!
Regards, *Emmanuel Rutovic* Founder, AESIM.tech manu@simba.io +1 (438) 926-6458
On Dec 2, 2020 at 2:37:39 PM, AlexM <alexmasis@gmail.com> wrote:
_______________________________________________ PythonNet mailing list -- pythonnet@python.org To unsubscribe send an email to pythonnet-leave@python.org https://mail.python.org/mailman3/lists/pythonnet.python.org/ Member address: manu@upsim.tech
_______________________________________________ PythonNet mailing list -- pythonnet@python.org To unsubscribe send an email to pythonnet-leave@python.org https://mail.python.org/mailman3/lists/pythonnet.python.org/ Member address: alexmasis@gmail.com
participants (2)
-
AlexM
-
Emmanuel Rutovic