From 32a54c028454c4e6ea4d7ff010d0c0c3ff28c324 Mon Sep 17 00:00:00 2001 From: IanCa Date: Thu, 31 Aug 2023 17:18:46 -0500 Subject: [PATCH 1/2] Add indented group printing from dungscout96 --- hed/models/hed_group.py | 36 +++++++++++++++++++++++++++++++++- tests/models/test_hed_group.py | 11 +++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/hed/models/hed_group.py b/hed/models/hed_group.py index b152ca2f9..ae28709fb 100644 --- a/hed/models/hed_group.py +++ b/hed/models/hed_group.py @@ -157,7 +157,8 @@ def sorted(self): sorted_copy (HedGroup): The sorted copy """ string_copy = self.copy() - return string_copy._sorted(update_self=True) + string_copy._sorted(update_self=True) + return string_copy def _sorted(self, update_self=False): """ Returns a sorted copy of this hed group as a list of it's children @@ -350,6 +351,39 @@ def lower(self): """ Convenience function, equivalent to str(self).lower() """ return str(self).lower() + def get_as_indented(self, tag_attribute="short_tag"): + """Returns the string as a multiline indented format + + Parameters: + tag_attribute (str): The hed_tag property to use to construct the string (usually short_tag or long_tag). + + Returns: + formatted_hed (str): the indented string + """ + hed_string = self.sorted().get_as_form(tag_attribute) + + level_open = [] + level = 0 + indented = "" + prev = '' + for c in hed_string: + if c == "(": + level_open.append(level) + indented += "\n" + "\t" * level + c + level += 1 + elif c == ")": + level = level_open.pop() + if prev == ")": + indented += "\n" + "\t" * level + c + else: + indented += c + + else: + indented += c + prev = c + + return indented + def find_placeholder_tag(self): """ Return a placeholder tag, if present in this group. diff --git a/tests/models/test_hed_group.py b/tests/models/test_hed_group.py index 96d1744c9..117872e8d 100644 --- a/tests/models/test_hed_group.py +++ b/tests/models/test_hed_group.py @@ -3,6 +3,7 @@ from hed import schema from hed.models import HedString +import copy class Test(unittest.TestCase): @@ -121,5 +122,15 @@ def test_sort_and_sorted(self): ] self._compare_strings(hed_strings) + def test_sorted_structure(self): + hed_string = HedString("(Tag3, Tag1, Tag5, Tag2, Tag4)", self.hed_schema) + original_hed_string = copy.deepcopy(hed_string) + + sorted_hed_string = hed_string.sorted() + + self.assertIsInstance(sorted_hed_string, HedString) + self.assertEqual(str(original_hed_string), str(hed_string)) + self.assertIsNot(sorted_hed_string, hed_string) + if __name__ == '__main__': unittest.main() From 840bd4b39d749bed72f09d176313861126989725 Mon Sep 17 00:00:00 2001 From: IanCa Date: Thu, 31 Aug 2023 17:30:21 -0500 Subject: [PATCH 2/2] Bump hed-specification version --- spec_tests/hed-specification | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec_tests/hed-specification b/spec_tests/hed-specification index 25a8b2a69..8f1c01406 160000 --- a/spec_tests/hed-specification +++ b/spec_tests/hed-specification @@ -1 +1 @@ -Subproject commit 25a8b2a69fe01b745c3f0fc8f4fa56d5b7e12aeb +Subproject commit 8f1c014062f629dc7b88d625fbee14f4c88455ba