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/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 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()