Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions lib/iris/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from abc import ABCMeta, abstractmethod
from collections.abc import Hashable, Iterable
from contextlib import contextmanager
import copy
import functools
import inspect
Expand Down Expand Up @@ -1054,18 +1053,20 @@ def format_array(arr):

"""

summary_insert = ""
summary_threshold = 85
summary_insert = "..." if arr.size > summary_threshold else ""
edge_items = 3
ffunc = str
formatArray = np.core.arrayprint._formatArray
max_line_len = 50
legacy = "1.13"
if arr.size > summary_threshold:
summary_insert = "..."
options = np.get_printoptions()
options["legacy"] = legacy
with _printopts_context(**options):

# Format the array with version 1.13 legacy behaviour
with np.printoptions(legacy="1.13"):
# Use this (private) routine for more control.
formatArray = np.core.arrayprint._formatArray
# N.B. the 'legacy' arg had different forms in different numpy versions
# -- fetch the required form from the internal options dict
legacy_key = np.core.arrayprint._format_options["legacy"]

result = formatArray(
arr,
ffunc,
Expand All @@ -1074,29 +1075,12 @@ def format_array(arr):
separator=", ",
edge_items=edge_items,
summary_insert=summary_insert,
legacy=legacy,
legacy=legacy_key,
)

return result


@contextmanager
def _printopts_context(**kwargs):
"""
Update the numpy printoptions for the life of this context manager.

Note: this function can be removed with numpy>=1.15 thanks to
https://github.com/numpy/numpy/pull/10406

"""
original_opts = np.get_printoptions()
np.set_printoptions(**kwargs)
try:
yield
finally:
np.set_printoptions(**original_opts)


def new_axis(src_cube, scalar_coord=None):
"""
Create a new axis as the leading dimension of the cube, promoting a scalar
Expand Down