From bcb6762165395008ac5746d8a7c2053421cd5f04 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Mon, 4 Jan 2021 14:54:52 +0100 Subject: [PATCH 1/3] Print number of variables in repr --- xarray/core/formatting.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 8dd8d43efab..86fc62940ee 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -370,9 +370,10 @@ def _mapping_repr(mapping, title, summarizer, col_width=None, max_rows=None): col_width = _calculate_col_width(mapping) if max_rows is None: max_rows = OPTIONS["display_max_rows"] - summary = [f"{title}:"] if mapping: - if len(mapping) > max_rows: + len_mapping = len(mapping) + summary = [f"{title} ({min(max_rows, len_mapping)}/{len_mapping}):"] + if len_mapping > max_rows: first_rows = max_rows // 2 + max_rows % 2 items = list(mapping.items()) summary += [summarizer(k, v, col_width) for k, v in items[:first_rows]] @@ -383,6 +384,7 @@ def _mapping_repr(mapping, title, summarizer, col_width=None, max_rows=None): else: summary += [summarizer(k, v, col_width) for k, v in mapping.items()] else: + summary = [f"{title}:"] summary += [EMPTY_REPR] return "\n".join(summary) From f9cf1bbd98e19798543a1970fdd09bcebde8ee84 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Mon, 4 Jan 2021 16:00:43 +0100 Subject: [PATCH 2/3] Tweak title only when there's many rows Workaround to avoid having to redo every single doctest... It is really only necessary when the data rows are limited. But I find it a bit difficult to count the rows quickly past like 7. --- xarray/core/formatting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 86fc62940ee..276b56e8dbb 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -370,10 +370,11 @@ def _mapping_repr(mapping, title, summarizer, col_width=None, max_rows=None): col_width = _calculate_col_width(mapping) if max_rows is None: max_rows = OPTIONS["display_max_rows"] + summary = [f"{title}:"] if mapping: len_mapping = len(mapping) - summary = [f"{title} ({min(max_rows, len_mapping)}/{len_mapping}):"] if len_mapping > max_rows: + summary = [f"{title} ({min(max_rows, len_mapping)}/{len_mapping}):"] first_rows = max_rows // 2 + max_rows % 2 items = list(mapping.items()) summary += [summarizer(k, v, col_width) for k, v in items[:first_rows]] @@ -384,7 +385,6 @@ def _mapping_repr(mapping, title, summarizer, col_width=None, max_rows=None): else: summary += [summarizer(k, v, col_width) for k, v in mapping.items()] else: - summary = [f"{title}:"] summary += [EMPTY_REPR] return "\n".join(summary) From a05b8ef7f12f0b9f3a898c47d65023601a6b2d0f Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Mon, 4 Jan 2021 17:08:11 +0100 Subject: [PATCH 3/3] Remove min() No need to limit max_rows now because the if condition handles that. --- xarray/core/formatting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 276b56e8dbb..282620e3569 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -374,7 +374,7 @@ def _mapping_repr(mapping, title, summarizer, col_width=None, max_rows=None): if mapping: len_mapping = len(mapping) if len_mapping > max_rows: - summary = [f"{title} ({min(max_rows, len_mapping)}/{len_mapping}):"] + summary = [f"{summary[0]} ({max_rows}/{len_mapping})"] first_rows = max_rows // 2 + max_rows % 2 items = list(mapping.items()) summary += [summarizer(k, v, col_width) for k, v in items[:first_rows]]