[Python.NET] Re: RANGE object access from Python

AlexM <[email protected]> Wed, 2 Dec 2020 21:02:02 -0500
Newsgroups gmane.comp.python.dotnet
Message-ID <CAHgbZ6D564pimRG0TEsuv9Zw1orBunno2FxoV-OdeJy3j6g-FQ@mail.gmail.com>
--===============6601340170699032629==
Content-Type: multipart/alternative; boundary="00000000000096968b05b585bf4a"

--00000000000096968b05b585bf4a
Content-Type: text/plain; charset="UTF-8"

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 <[email protected]> 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-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]
>>
> _______________________________________________
> 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]
>

--00000000000096968b05b585bf4a
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Thank you very much Emmanuel !<div>I will try your idea, t=
hink this should work ...</div><div>Take care!</div><div>Alex.</div></div><=
br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed,=
 Dec 2, 2020 at 3:35 PM Emmanuel Rutovic &lt;[email protected]&gt; wrote:<br>=
</div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;b=
order-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
        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" target=3D"_blank">https://github.com/pythonnet/pythonnet/wiki/=
Codecs:-customizing-object-marshalling-between-.NET-and-Python</a></div><di=
v><br></div><div>Here is how I use it:=C2=A0</div><div><br></div><div><p st=
yle=3D"margin:0px;font-stretch:normal;font-size:9.5px;line-height:normal;fo=
nt-family:Helvetica;color:rgb(128,128,128);background-color:rgb(0,0,0)">pub=
lic class DoubleArrayPythonEncoder : 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 &lt; 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><div dir=3D"ltr"><div=
 style=3D"color:rgb(34,34,34)"><strong>Emmanuel Rutovic</strong><br><span>F=
ounder</span>, AESIM.tech<br><a href=3D"mailto:[email protected]" style=3D"colo=
r:rgb(17,85,204)" target=3D"_blank">[email protected]</a></div><div style=3D"co=
lor: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 &lt;<a href=3D"mailto:[email protected]" target=3D"_blank">alexmas=
[email protected]</a>&gt; 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]" target=3D"_blank">pythonnet@p=
ython.org</a><br>To unsubscribe send an email to <a href=3D"mailto:pythonne=
[email protected]" target=3D"_blank">[email protected]</a><br><a =
href=3D"https://mail.python.org/mailman3/lists/pythonnet.python.org/" targe=
t=3D"_blank">https://mail.python.org/mailman3/lists/pythonnet.python.org/</=
a><br>Member address: [email protected]<br>
</div>
        </blockquote>
    </div>
</div>
   =20
</div></div>
_______________________________________________<br>
PythonNet mailing list -- <a href=3D"mailto:[email protected]" target=3D=
"_blank">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]=
g" target=3D"_blank">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman3/lists/pythonnet.python.org/" re=
l=3D"noreferrer" target=3D"_blank">https://mail.python.org/mailman3/lists/p=
ythonnet.python.org/</a><br>
Member address: <a href=3D"mailto:[email protected]" target=3D"_blank">al=
[email protected]</a><br>
</blockquote></div>

--00000000000096968b05b585bf4a--

--===============6601340170699032629==
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]

--===============6601340170699032629==--