Re: [Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module

2018-02-12 Thread David Mertz
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_).

But just for anyone who doesn't know NumPy, here's a quick illustration of
what I alluded to:

In [1]: import numpy as np
In [2]: arr = np.array([7,8,12,33])
In [3]: ndx1 = np.array([0,1,1,0], dtype=int)
In [4]: ndx2 = np.array([0,1,1,0], dtype=bool)
In [5]: arr[ndx1]
Out[5]: array([7, 8, 8, 7])
In [6]: arr[ndx2]
Out[6]: array([ 8, 12])

ndx1 and ndx2 are both nice things (and are both often programmatically
constructed by operations in NumPy).  But indexing using ndx1 gives us an
array of the things in the listed *positions* in arr.  In this case, we
happen to choose two each of the things an index 0 and index 1 in the
result.

Indexing by ndx2 gives us a filter of only those positions in arr
corresponding to 'True's.  These are both nice things to be able to do, but
if NumPy's True was a special kind of 1, it wouldn't work out
unambiguously.  However, recent versions of NumPy *have* gotten a bit
smarter about recognizing the special type of Python bools, so it's less of
a trap than it used to be.  Still, contrast these (using actual Python
lists for the indexes:

In [10]: arr[[False, True, True, False]]
Out[10]: array([ 8, 12])
In [11]: arr[[False, True, 1, 0]]
Out[11]: array([7, 8, 8, 7])




On Mon, Feb 12, 2018 at 7:50 PM, Nick Coghlan  wrote:

> On 13 February 2018 at 02:14, David Mertz  wrote:
> > NumPy np.bool_ is specifically not a subclass of any np.int_.  If it
> we're,
> > there would be an ambiguity between indexing with a Boolean array and an
> > array of ints. Both are meaningful, but they mean different things (mask
> vs
> > collection of indices).
> >
> > Do we have other examples a Python ABC that exists to accommodate
> something
> > outside the standard library or builtins? Even if not, NumPy is
> special...
> > the actual syntax for '@' exists primarily for that library!
>
> collections.abc.Sequence and collections.abc.Mapping come to mind -
> the standard library doesn't tend to distinguish between different
> kinds of subscriptable objects, but it's a distinction some third
> party libraries and tools want to be able to make reliably.
>
> The other comparison that comes to mind would be the distinction
> between "__int__" ("can be coerced to an integer, but may lose
> information in the process") and "__index__" ("can be losslessly
> converted to and from a builtin integer").
>
> Right now, we only define boolean coercion via "__bool__" - there's no
> mechanism to say "this *is* a boolean value that can be losslessly
> converted to and from the builtin boolean constants". That isn't a
> distinction the standard library makes, but it sounds like it's one
> that NumPy cares about (and NumPy was also the main driver for
> introducing __index__).
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module

2018-02-12 Thread Nick Coghlan
On 13 February 2018 at 02:14, David Mertz  wrote:
> NumPy np.bool_ is specifically not a subclass of any np.int_.  If it we're,
> there would be an ambiguity between indexing with a Boolean array and an
> array of ints. Both are meaningful, but they mean different things (mask vs
> collection of indices).
>
> Do we have other examples a Python ABC that exists to accommodate something
> outside the standard library or builtins? Even if not, NumPy is special...
> the actual syntax for '@' exists primarily for that library!

collections.abc.Sequence and collections.abc.Mapping come to mind -
the standard library doesn't tend to distinguish between different
kinds of subscriptable objects, but it's a distinction some third
party libraries and tools want to be able to make reliably.

The other comparison that comes to mind would be the distinction
between "__int__" ("can be coerced to an integer, but may lose
information in the process") and "__index__" ("can be losslessly
converted to and from a builtin integer").

Right now, we only define boolean coercion via "__bool__" - there's no
mechanism to say "this *is* a boolean value that can be losslessly
converted to and from the builtin boolean constants". That isn't a
distinction the standard library makes, but it sounds like it's one
that NumPy cares about (and NumPy was also the main driver for
introducing __index__).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module

2018-02-12 Thread David Mertz
NumPy np.bool_ is specifically not a subclass of any np.int_.  If it we're,
there would be an ambiguity between indexing with a Boolean array and an
array of ints. Both are meaningful, but they mean different things (mask vs
collection of indices).

Do we have other examples a Python ABC that exists to accommodate something
outside the standard library or builtins? Even if not, NumPy is special...
the actual syntax for '@' exists primarily for that library!

On Feb 12, 2018 5:51 AM, "Steven D'Aprano"  wrote:

On Mon, Feb 12, 2018 at 09:41:04AM +, Sylvain MARIE wrote:

> The numbers module provides very useful ABC for the 'numeric tower',
> able to abstract away the differences between python primitives and
> for example numpy primitives.
> I could not find any equivalent for Booleans.
> However numpy defines np.bool too, so being able to have an abstract
> Boolean class for both python bool and numpy bool would be great.

I don't know anything about numpy bools, but Python built-in bools are
numbers, and as such already have an ABC: they are a subclass of int.



--
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module

2018-02-12 Thread Guido van Rossum
TBH I've found the numeric tower a questionable addition to Python's
stdlib. For PEP 484 we decided not to use it (using the concrete types,
int, float etc. instead). So I'm not excited about adding more like this,
especially since essentially *everything* can be used in a Boolean context.

If your specific project has a specific style requirement for Booleans that
would be helped by a Boolean ABC, maybe you should add it to your project
and see how it works out, and after a few months report back here.

Finally. How were you planning to use this new ABC?

On Mon, Feb 12, 2018 at 1:41 AM, Sylvain MARIE <
sylvain.ma...@schneider-electric.com> wrote:

> The numbers module provides very useful ABC for the ‘numeric tower’, able
> to abstract away the differences between python primitives and for example
> numpy primitives.
>
> I could not find any equivalent for Booleans.
>
> However numpy defines np.bool too, so being able to have an abstract
> Boolean class for both python bool and numpy bool would be great.
>
>
>
> Here is a version that I included in valid8 in the meantime
>
>
>
> ---
>
> class Boolean(metaclass=ABCMeta):
>
> """
>
> An abstract base class for booleans, similar to what is available in
> numbers
>
> see https://docs.python.org/3.5/library/numbers.html
>
> """
>
> __slots__ = ()
>
>
>
> @abstractmethod
>
> def __bool__(self):
>
> """Return a builtin bool instance. Called for bool(self)."""
>
>
>
> @abstractmethod
>
> def __and__(self, other):
>
> """self & other"""
>
>
>
> @abstractmethod
>
> def __rand__(self, other):
>
> """other & self"""
>
>
>
> @abstractmethod
>
> def __xor__(self, other):
>
> """self ^ other"""
>
>
>
> @abstractmethod
>
> def __rxor__(self, other):
>
> """other ^ self"""
>
>
>
> @abstractmethod
>
> def __or__(self, other):
>
> """self | other"""
>
>
>
> @abstractmethod
>
> def __ror__(self, other):
>
> """other | self"""
>
>
>
> @abstractmethod
>
> def __invert__(self):
>
> """~self"""
>
>
>
>
>
> # register bool and numpy bool_ as virtual subclasses
>
> # so that issubclass(bool, Boolean) = issubclass(np.bool_, Boolean) = True
>
> Boolean.register(bool)
>
>
>
> try:
>
> import numpy as np
>
> Boolean.register(np.bool_)
>
> except ImportError:
>
> # silently escape
>
> pass
>
>
>
> ---
>
>
>
> If that topic was already discussed and settled in the past, please ignore
> this thread – apologies for not being able to find it.
>
> Best regards
>
>
>
> Sylvain
>
>
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>


-- 
--Guido van Rossum (python.org/~guido)
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module

2018-02-12 Thread Steven D'Aprano
On Mon, Feb 12, 2018 at 09:41:04AM +, Sylvain MARIE wrote:

> The numbers module provides very useful ABC for the 'numeric tower', 
> able to abstract away the differences between python primitives and 
> for example numpy primitives.
> I could not find any equivalent for Booleans.
> However numpy defines np.bool too, so being able to have an abstract 
> Boolean class for both python bool and numpy bool would be great.

I don't know anything about numpy bools, but Python built-in bools are 
numbers, and as such already have an ABC: they are a subclass of int.



-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module

2018-02-12 Thread Sylvain MARIE
The numbers module provides very useful ABC for the 'numeric tower', able to 
abstract away the differences between python primitives and for example numpy 
primitives.
I could not find any equivalent for Booleans.
However numpy defines np.bool too, so being able to have an abstract Boolean 
class for both python bool and numpy bool would be great.

Here is a version that I included in valid8 in the meantime

---
class Boolean(metaclass=ABCMeta):
"""
An abstract base class for booleans, similar to what is available in numbers
see https://docs.python.org/3.5/library/numbers.html
"""
__slots__ = ()

@abstractmethod
def __bool__(self):
"""Return a builtin bool instance. Called for bool(self)."""

@abstractmethod
def __and__(self, other):
"""self & other"""

@abstractmethod
def __rand__(self, other):
"""other & self"""

@abstractmethod
def __xor__(self, other):
"""self ^ other"""

@abstractmethod
def __rxor__(self, other):
"""other ^ self"""

@abstractmethod
def __or__(self, other):
"""self | other"""

@abstractmethod
def __ror__(self, other):
"""other | self"""

@abstractmethod
def __invert__(self):
"""~self"""


# register bool and numpy bool_ as virtual subclasses
# so that issubclass(bool, Boolean) = issubclass(np.bool_, Boolean) = True
Boolean.register(bool)

try:
import numpy as np
Boolean.register(np.bool_)
except ImportError:
# silently escape
pass

---

If that topic was already discussed and settled in the past, please ignore this 
thread - apologies for not being able to find it.
Best regards

Sylvain

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/