<div dir="ltr">A thought just occurred to me. Maybe we should just add a Boolean class to numbers? It's a subclass of Integral, presumably. And normally only builtins.bool is registered with it. But np.bool can be added at the same point you register the other np integral types.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 14, 2018 at 10:23 AM, Sylvain MARIE <span dir="ltr"><<a href="mailto:sylvain.marie@schneider-electric.com" target="_blank">sylvain.marie@schneider-electric.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div link="blue" vlink="purple" lang="FR">
<div class="m_1317902404712711250WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">Yes, this is used in combination dynamic type checking, currently using enforce (<a href="https://github.com/RussBaz/enforce" target="_blank">https://github.com/RussBaz/<wbr>enforce</a>
) but I know that others exist (pytypes in particular)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">As per examples…all utility functions that we write that are receiving a number or a boolean in their parameters are now written using
the numbers and additional Boolean classes:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">------------- example where Integral is used instead of int -----------------<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal" style="background:white"><b><span style="font-size:10.0pt;font-family:"Courier New";color:navy" lang="EN-US">from
</span></b><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">numbers
</span><b><span style="font-size:10.0pt;font-family:"Courier New";color:navy" lang="EN-US">import
</span></b><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">Integral<br>
<br>
</span><b><span style="font-size:10.0pt;font-family:"Courier New";color:navy" lang="EN-US">import
</span></b><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">pandas
</span><b><span style="font-size:10.0pt;font-family:"Courier New";color:navy" lang="EN-US">as
</span></b><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">pd<br>
<br>
</span><b><span style="font-size:10.0pt;font-family:"Courier New";color:navy" lang="EN-US">from
</span></b><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">enforce
</span><b><span style="font-size:10.0pt;font-family:"Courier New";color:navy" lang="EN-US">import
</span></b><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">runtime_validation, config<br>
config(</span><span style="font-size:10.0pt;font-family:"Courier New";color:navy" lang="EN-US">dict</span><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">(</span><span style="font-size:10.0pt;font-family:"Courier New";color:#660099" lang="EN-US">mode</span><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">=</span><b><span style="font-size:10.0pt;font-family:"Courier New";color:teal" lang="EN-US">'covariant'</span></b><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">))<wbr>
</span><i><span style="font-size:10.0pt;font-family:"Courier New";color:gray" lang="EN-US"># type validation will accept subclasses too<br>
<br>
<br>
</span></i><span style="font-size:10.0pt;font-family:"Courier New";color:#0000b2" lang="EN-US">@runtime_validation<br>
</span><b><span style="font-size:10.0pt;font-family:"Courier New";color:navy" lang="EN-US">def
</span></b><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US">only_keep_events_lasting_at_<wbr>least(boolean_series: pd.Series, min_nb_occurrences: Integral):<br>
</span><i><span style="font-size:10.0pt;font-family:"Courier New";color:gray" lang="EN-US">"""<br>
Filters boolean flags to keep 'true' only when it appears at least min_nb_occurrences times in a row<br>
<br>
</span></i><b><span style="font-size:10.0pt;font-family:"Courier New";color:gray" lang="EN-US">:param</span></b><i><span style="font-size:10.0pt;font-family:"Courier New";color:gray" lang="EN-US"> boolean_series:<br>
</span></i><b><span style="font-size:10.0pt;font-family:"Courier New";color:gray" lang="EN-US">:param</span></b><i><span style="font-size:10.0pt;font-family:"Courier New";color:gray" lang="EN-US"> min_nb_occurrences:<br>
</span></i><b><span style="font-size:10.0pt;font-family:"Courier New";color:gray" lang="EN-US">:return</span></b><i><span style="font-size:10.0pt;font-family:"Courier New";color:gray" lang="EN-US">:<br>
"""</span></i><span style="font-size:10.0pt;font-family:"Courier New";color:black" lang="EN-US"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"> (contents skipped for clarity)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">------------------------------<wbr>-------<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">Similarly when a bool type hint is in the signature we try to replace it with a Boolean, so that people can call it with a numpy bool.
But maybe that’s too much of type checking for the python philosophy ? I’m wondering if we’re going too far here…<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">Anyway, again, my point is just about consistency: if this is available for numbers, why not for simple Booleans?
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Arial",sans-serif" lang="EN-US">Sylvain
</span><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">De :</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Guido van Rossum [mailto:<a href="mailto:gvanrossum@gmail.com" target="_blank">gvanrossum@gmail.com</a>]
<br>
<b>Envoyé :</b> mercredi 14 février 2018 17:14<br>
<b>À :</b> Sylvain MARIE <<a href="mailto:sylvain.marie@schneider-electric.com" target="_blank">sylvain.marie@schneider-<wbr>electric.com</a>><br>
<b>Cc :</b> Python-Ideas <<a href="mailto:python-ideas@python.org" target="_blank">python-ideas@python.org</a>></span></p><div><div class="h5"><br>
<b>Objet :</b> Re: [Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module<u></u><u></u></div></div><p></p><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">Can you show some sample code that you have written that shows where this would be useful?<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Note that using the numbers package actually makes static type checking through e.g. mypy difficult. So I presume you are talking about dynamic checking?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">--Guido<u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Feb 14, 2018 12:42 AM, "Sylvain MARIE" <<a href="mailto:sylvain.marie@schneider-electric.com" target="_blank">sylvain.marie@schneider-<wbr>electric.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">My point is just that today, I use the ‘numbers’ package classes (Integral, Real, …) for PEP484 type-hinting,
and I find it quite useful in term of input type validation (in combination with PEP484-compliant type checkers, whether static or dynamic). Adding a Boolean ABC with a similar behavior would certainly add consistency to that ‘numbers’ package – only for users
who already find it useful, of course.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">Note that my use case is not about converting an object to a Boolean, I’m just speaking about type validation
of a ‘true’ boolean object, for example to be received as a function argument for a flag option. This is for example for users who want to define strongly-typed APIs for interaction with the ‘outside world’, and keep using duck-typing for internals.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Arial",sans-serif" lang="EN-US">Sylvain
</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">De :</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US"> Python-ideas
[mailto:<a href="mailto:python-ideas-bounces%2Bsylvain.marie" target="_blank">python-ideas-bounces+<wbr>sylvain.marie</a>=<a href="mailto:schneider-electric.com@python.org" target="_blank">schneider-<wbr>electric.com@python.org</a>]
<b>De la part de</b> Chris Barker<br>
<b>Envoyé :</b> mardi 13 février 2018 21:12<br>
<b>À :</b> David Mertz <<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a>><br>
<b>Cc :</b> python-ideas <<a href="mailto:python-ideas@python.org" target="_blank">python-ideas@python.org</a>><br>
<b>Objet :</b> Re: [Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module</span><u></u><u></u></p>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<div>
<p class="MsoNormal"><span lang="EN-US">On Mon, Feb 12, 2018 at 10:07 PM, David Mertz <</span><a href="mailto:mertz@gnosis.cx" target="_blank"><span lang="EN-US">mertz@gnosis.cx</span></a><span lang="EN-US">>
wrote:</span><u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0cm;margin-bottom:5.0pt">
<div>
<p class="MsoNormal"><span lang="EN-US">I'm not sure I'm convinced by Sylvain that Boolean needs to be an ABC in the standard library; Guido expresses skepticism. Of course it is possible to define
it in some other library that actually needs to use `isinstance(x, Boolean)` as Sylvain demonstraits in his post. I'm not sure I'm unconvinced either, I can see a certain value to saying a given value is "fully round-trippable to bool" (as is np.bool_).</span><u></u><u></u></p>
</div>
</blockquote>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">But is an ABC the way to do it? Personally, I'm skeptical that ABCs are a solution to, well, anything (as apposed to duck typing and EAFTP). Take Nick's example:</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt" lang="EN-US">"""</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt" lang="EN-US">The other comparison that comes to mind would be the distinction<br>
between "__int__" ("can be coerced to an integer, but may lose<br>
information in the process") and "__index__" ("can be losslessly<br>
converted to and from a builtin integer").</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">""" </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">I suppose we could have had an Index ABC -- but that seems painful to me.</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">so maybe we could use a __true_bool__ special method? </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">(and an operator.true_bool() function ???)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">(this all makes me wish that python bools were more pure -- but way to late for that!)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">I guess it comes down to whether you want to:</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> - Ask the question: "is this object a boolean?"</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">or</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> - Make this object a boolean</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">__index__ (and operator.index()) is essentially the later -- you want to make an index out of whatever object you have, if you can do so.</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">-CHB</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><span lang="EN-US">--
</span><u></u><u></u></p>
<div>
<p class="MsoNormal"><span lang="EN-US"><br>
Christopher Barker, Ph.D.<br>
Oceanographer<br>
<br>
Emergency Response Division<br>
NOAA/NOS/OR&R <a href="tel:(206)%20526-6959" target="_blank">(206) 526-6959</a> voice<br>
7600 Sand Point Way NE <a href="tel:(206)%20526-6329" target="_blank">(206) 526-6329</a> fax<br>
Seattle, WA 98115 <a href="tel:(206)%20526-6317" target="_blank">(206) 526-6317</a> main reception<br>
<br>
</span><a href="mailto:Chris.Barker@noaa.gov" target="_blank"><span lang="EN-US">Chris.Barker@noaa.gov</span></a><u></u><u></u></p>
</div>
</div>
</div>
</div>
<p class="MsoNormal"><span lang="EN-US"><br>
______________________________<wbr>______________________________<wbr>__________<br>
This email has been scanned by the Symantec Email Security.cloud service.<br>
</span>______________________________<wbr>______________________________<wbr>__________<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
<p class="MsoNormal"><br>
______________________________<wbr>______________________________<wbr>__________<br>
This email has been scanned by the Symantec Email Security.cloud service.<br>
______________________________<wbr>______________________________<wbr>__________<u></u><u></u></p>
</div></div></div>
</div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>