[Python.NET] Re: RANGE object access from Python
Emmanuel Rutovic <[email protected]> Wed, 2 Dec 2020 12:32:29 -0800
| Newsgroups | gmane.comp.python.dotnet |
|---|---|
| Message-ID | <CAJE6Wx1h-0n93Y_CmKwb26-V0k6=E8+YgRkj0WFaG4GOQDPxtg@mail.gmail.com> |
--===============4649712691048375426==
Content-Type: multipart/alternative; boundary="0000000000006f4ebb05b58124c4"
--0000000000006f4ebb05b58124c4
Content-Type: text/plain; charset="UTF-8"
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-marshalling-between-.NET-and-Python
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
[email protected]
+1 (438) 926-6458
On Dec 2, 2020 at 2:37:39 PM, AlexM <[email protected]> wrote:
> _______________________________________________
> PythonNet mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/pythonnet.python.org/
> Member address: [email protected]
>
--0000000000006f4ebb05b58124c4
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<html><body>
Hi Alex,<div><br></div><div>Not sure about your class but you can u=
se codecs to create custom code to convert your objects from .Net to Python=
.=C2=A0</div><div><br></div><div>Doc :=C2=A0<a href=3D"https://github.com/p=
ythonnet/pythonnet/wiki/Codecs:-customizing-object-marshalling-between-.NET=
-and-Python">https://github.com/pythonnet/pythonnet/wiki/Codecs:-customizin=
g-object-marshalling-between-.NET-and-Python</a></div><div><br></div><div>H=
ere is how I use it:=C2=A0</div><div><br></div><div><p style=3D"margin:0px;=
font-stretch:normal;font-size:9.5px;line-height:normal;font-family:Helvetic=
a;color:rgb(128,128,128);background-color:rgb(0,0,0)">public class DoubleAr=
rayPythonEncoder : IPyObjectEncoder</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
;min-height:11px"><br></p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 {</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 public static DoubleArrayPythonEncoder Instan=
ce { get; } =3D new DoubleArrayPythonEncoder();</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
;min-height:11px"><br></p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 public bool CanEncode(Type type)</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 {</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return type =3D=3D typeof(doubl=
e[]);</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 }</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
;min-height:11px"><br></p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 public PyObject TryEncode(object value)</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 {</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
"><br></p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (value is double[] doublearr=
ay)</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 var pyList =3D ne=
w PyList();</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for (int i =3D 0;=
i < doublearray.Length - 1; i++)</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 pyL=
ist.Append(new PyFloat(doublearray[i]));</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return pyList;</p=
>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return null;</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 }</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
;min-height:11px"><br></p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 public static void Register()</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 {</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 PyObjectConversions.RegisterEnc=
oder(Instance);</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 }</p>
<p style=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:norm=
al;font-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)=
">=C2=A0 =C2=A0 }</p></div><div><br></div><div>In Python, just call Registe=
r() before getting object.</div><div><br></div><div>I hope it helps!=C2=A0<=
/div><div><br></div><div>Regards,</div><div><div><div class=3D"gmail_signat=
ure" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><div style=3D"colo=
r:rgb(34,34,34)"><strong>Emmanuel Rutovic</strong><br><span>Founder</span>,=
AESIM.tech<br><a href=3D"mailto:[email protected]" style=3D"color:rgb(17,85,20=
4)" target=3D"_blank">[email protected]</a></div><div style=3D"color:rgb(34,34,=
34)">+1 (438) 926-6458</div></div></div></div><div dir=3D"ltr">
<br><br>
<div class=3D"gmail_quote">
<div dir=3D"ltr" class=3D"gmail_attr">On Dec 2, 2020 at 2:37:39 PM,=
AlexM <<a href=3D"mailto:[email protected]">[email protected]</a>&g=
t; wrote:<br></div>
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex=
;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
_______________________________________________<br>PythonNet mailing li=
st -- <a href=3D"mailto:[email protected]">[email protected]</a><br>T=
o unsubscribe send an email to <a href=3D"mailto:[email protected]=
">[email protected]</a><br><a href=3D"https://mail.python.org/mail=
man3/lists/pythonnet.python.org/">https://mail.python.org/mailman3/lists/py=
thonnet.python.org/</a><br>Member address: [email protected]<br>
</div>
</blockquote>
</div>
</div>
=20
</div></body></html>
--0000000000006f4ebb05b58124c4--
--===============4649712691048375426==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
PythonNet mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/pythonnet.python.org/
Member address: [email protected]
--===============4649712691048375426==--