From cfdc90f8a7df04afd589cd4a8fca0e81ab3fa473 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Wed, 4 Oct 2023 01:30:37 -0400 Subject: [PATCH] fix: Fix TableSet.print_structure for nested tablesets, #765 --- CHANGELOG.rst | 1 + agate/tableset/print_structure.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 03109549..1afef6ad 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,7 @@ Unreleased * feat: Lowercase the ``null_values`` provided to individual data types, since all comparisons to ``null_values`` are case-insensitive. (#770) * feat: :class:`.Mean` works with :class:`.TimeDelta`. (#761) +* fix: Fix :meth:`.TableSet.print_structure` for nested tablesets. (#765) 1.7.1 - Jan 4, 2023 ------------------- diff --git a/agate/tableset/print_structure.py b/agate/tableset/print_structure.py index e8422494..0ceb9295 100644 --- a/agate/tableset/print_structure.py +++ b/agate/tableset/print_structure.py @@ -2,6 +2,15 @@ from agate.data_types import Text from agate.table import Table +from agate.tableset import TableSet + + +def _items(key, value): + if isinstance(value, TableSet): + for k, v in value.items(): + yield from _items(key + (k,), v) + else: + yield key, value def print_structure(self, max_rows=20, output=sys.stdout): @@ -16,10 +25,11 @@ def print_structure(self, max_rows=20, output=sys.stdout): :returns: None """ - max_length = min(len(self.items()), max_rows) + items = list(_items((), self)) + max_length = min(len(items), max_rows) - name_column = self.keys()[0:max_length] - type_column = [str(len(table.rows)) for key, table in self.items()[0:max_length]] + name_column = ['.'.join(key) for key, value in items][0:max_length] + type_column = [str(len(table.rows)) for key, table in items[0:max_length]] rows = zip(name_column, type_column) column_names = ['table', 'rows'] text = Text()