Skip to content

strip_zeros kwarg no longer available in FloatFormat #16036

Closed
@moorepants

Description

@moorepants

Reproducing code example:

I've been using this context manager in some code to control array printing:

import contextlib

import numpy as np
import numpy.core.arrayprint as arrayprint


@contextlib.contextmanager
def printoptions(strip_zeros=False, **kwargs):
    """Allows you to set NumPy array print formatting locally.
    Taken from:
    http://stackoverflow.com/questions/2891790/pretty-printing-of-numpy-array
    """

    origcall = arrayprint.FloatFormat.__call__

    def __call__(self, x, strip_zeros=strip_zeros):
        return origcall.__call__(self, x, strip_zeros)

    arrayprint.FloatFormat.__call__ = __call__
    original = np.get_printoptions()
    np.set_printoptions(**kwargs)
    yield
    np.set_printoptions(**original)
    arrayprint.FloatFormat.__call__ = origcall

FloatFormat was removed in NumPy 1.18.0 but prior to that #9941 removed the strip_zeros kwarg from FloatFormat.__call__. This seems to be a backwards incompatible change. It also seems like that kwarg likely did not have a unit test.

Regardless, I'm trying to figure out how to reproduce the behavior of strip_zeros=False prior to PR #9941.

Error message:

Numpy/Python version information:

In [10]: import sys, numpy; print(numpy.__version__, sys.version)                                                                           
1.18.1 3.6.10 | packaged by conda-forge | (default, Apr  6 2020, 14:52:36) 
[GCC 7.3.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions