[Numpy-discussion] Proposal to support __format__

Gustav Larsson larsson at cs.uchicago.edu
Wed Feb 15 16:48:34 EST 2017


>
> This is great!


Thanks! Glad to be met by enthusiasm about this.

1. You basically have a NEP already! Making a PR from it allows to
> give line-by-line comments, so would help!


I will do this soon.

2. Don't worry about supporting python2 specifics; just try to ensure
> it doesn't break; I would not say more about it!


Sounds good to me.

3. On `set_printoptions` -- ideally, it will become possible to use
> this as a context (i.e., `with set_printoption(...)`). It might make
> sense to have an `override_format` keyword argument to it.


Having a `with np.printoptions(...)` context manager is a great idea. It
does sound orthogonal to __format__ though, so it could be addressed
separately.

4. Otherwise, my main suggestion is to start small with the more
> obvious ones, and not worry too much about format validation, but
> rather about getting the simple ones to work well (e.g., for an object
> array, just apply the format given; if it doesn't work, it will error
> out on its own, which is OK).


Sounds good to me. I was thinking of approaching the implementation by
writing unit tests first and group them into different priority tiers. That
way, the unit tests can go through another review before implementation
gets going. I agree that __format__ doesn't have to check format validation
if a ValueError is going to be raised anyway by sub-calls.

5. One bit of detail: the "g" one does confuse me.


I will re-write this a bit to make it clearer. Basically, the 'g' with the
mix of 'e'/'f' depending on max/min>1000 is all from the current numpy
behavior, so it is not something I had much creative input on at all.
Although, as it is written right now it may seem so. That is, the goal is
to have {:} == {:g} for float arrays, analogous to how {:} == {:g} for
built-in floats. Then, if the user departs a bit, like {:.2g}, it will
simply be identical to calling np.set_printoptions(precision=2) first.

Gustav

On Wed, Feb 15, 2017 at 8:03 AM, Marten van Kerkwijk <
m.h.vankerkwijk at gmail.com> wrote:

> Hi Gustav,
>
> This is great!  A few quick comments (mostly echo-ing Stephan's).
>
> 1. You basically have a NEP already! Making a PR from it allows to
> give line-by-line comments, so would help!
>
> 2. Don't worry about supporting python2 specifics; just try to ensure
> it doesn't break; I would not say more about it!
>
> 3. On `set_printoptions` -- ideally, it will become possible to use
> this as a context (i.e., `with set_printoption(...)`). It might make
> sense to have an `override_format` keyword argument to it.
>
> 4. Otherwise, my main suggestion is to start small with the more
> obvious ones, and not worry too much about format validation, but
> rather about getting the simple ones to work well (e.g., for an object
> array, just apply the format given; if it doesn't work, it will error
> out on its own, which is OK).
>
> 5. One bit of detail: the "g" one does confuse me.
>
> All the best,
>
> Marten
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170215/bdabc17f/attachment.html>


More information about the NumPy-Discussion mailing list