Skip to content

Commit

Permalink
more refactoring related to PortalObject.compare for submitr.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Jan 24, 2024
1 parent 133a189 commit 34fd5f4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 7 additions & 4 deletions dcicutils/misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,19 +1148,22 @@ def remove_suffix(suffix: str, text: str, required: bool = False):
return text[:len(text)-len(suffix)]


def remove_empty_properties(data: Optional[Union[list, dict]], isempty: Optional[Callable] = None) -> None:
def remove_empty_properties(data: Optional[Union[list, dict]],
isempty: Optional[Callable] = None,
isempty_array_element: Optional[Callable] = None) -> None:
def _isempty(value: Any) -> bool: # noqa
return isempty(value) if callable(isempty) else value in [None, "", {}, []]
if isinstance(data, dict):
for key in list(data.keys()):
if _isempty(value := data[key]):
del data[key]
else:
remove_empty_properties(value, isempty=isempty)
remove_empty_properties(value, isempty=isempty, isempty_array_element=isempty_array_element)
elif isinstance(data, list):
for item in data:
remove_empty_properties(item, isempty=isempty)
data[:] = [item for item in data if not _isempty(item)]
remove_empty_properties(item, isempty=isempty, isempty_array_element=isempty_array_element)
if callable(isempty_array_element):
data[:] = [item for item in data if not isempty_array_element(item)]


class ObsoleteError(Exception):
Expand Down
4 changes: 3 additions & 1 deletion dcicutils/structured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ def isempty(value: Any) -> bool: # noqa
if value == RowReader.CELL_DELETION_SENTINEL:
return True
return self._prune and value in [None, "", {}, []]
def isempty_array_element(value: Any) -> bool: # noqa
return value == RowReader.CELL_DELETION_SENTINEL
data = copy.deepcopy(data)
remove_empty_properties(data, isempty=isempty)
remove_empty_properties(data, isempty=isempty, isempty_array_element=isempty_array_element)
return data
if self._validated and not force:
return
Expand Down

0 comments on commit 34fd5f4

Please sign in to comment.