From 908d1a491aa1f18c3e9fbb46ab2a09e8af6bd94d Mon Sep 17 00:00:00 2001 From: Bill Little Date: Mon, 10 Jan 2022 12:39:57 +0000 Subject: [PATCH 1/2] address np printoptions pre/post v1.22 --- lib/iris/util.py | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/lib/iris/util.py b/lib/iris/util.py index 9ab413a493..9d36acc66e 100644 --- a/lib/iris/util.py +++ b/lib/iris/util.py @@ -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 @@ -1054,18 +1053,19 @@ 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 = "..." + + formatArray = np.core.arrayprint._formatArray + format_options = np.core.arrayprint._format_options + options = np.get_printoptions() - options["legacy"] = legacy - with _printopts_context(**options): + options["legacy"] = "1.13" + + with np.printoptions(**options): result = formatArray( arr, ffunc, @@ -1074,29 +1074,12 @@ def format_array(arr): separator=", ", edge_items=edge_items, summary_insert=summary_insert, - legacy=legacy, + legacy=format_options["legacy"], ) 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 From 3508d625e5d0009450044977df2d98653c64e30c Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Mon, 10 Jan 2022 16:14:07 +0000 Subject: [PATCH 2/2] Clarify odd print-control usage. --- lib/iris/util.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/iris/util.py b/lib/iris/util.py index 9d36acc66e..43ff6c9271 100644 --- a/lib/iris/util.py +++ b/lib/iris/util.py @@ -1059,13 +1059,14 @@ def format_array(arr): ffunc = str max_line_len = 50 - formatArray = np.core.arrayprint._formatArray - format_options = np.core.arrayprint._format_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"] - options = np.get_printoptions() - options["legacy"] = "1.13" - - with np.printoptions(**options): result = formatArray( arr, ffunc, @@ -1074,7 +1075,7 @@ def format_array(arr): separator=", ", edge_items=edge_items, summary_insert=summary_insert, - legacy=format_options["legacy"], + legacy=legacy_key, ) return result