Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion hed/models/hed_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
11 changes: 11 additions & 0 deletions tests/models/test_hed_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from hed import schema
from hed.models import HedString
import copy


class Test(unittest.TestCase):
Expand Down Expand Up @@ -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()