Skip to content

Commit

Permalink
Added remove_empty_properties to misc_utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Nov 19, 2023
1 parent 17d1d4d commit 5aaaaed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
14 changes: 1 addition & 13 deletions dcicutils/bundle_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .common import AnyJsonData
from .env_utils import EnvUtils, public_env_name
from .ff_utils import get_metadata
from .misc_utils import AbstractVirtualApp, ignored, ignorable, PRINT, to_camel_case
from .misc_utils import AbstractVirtualApp, ignored, ignorable, PRINT, remove_empty_properties, to_camel_case
from .sheet_utils import (
LoadTableError, prefer_number, TabbedJsonSchemas,
Header, Headers, TabbedHeaders, ParsedHeader, ParsedHeaders, TabbedParsedHeaders, SheetCellValue, TabbedSheetData,
Expand Down Expand Up @@ -752,15 +752,3 @@ def load_items(filename: str, tab_name: Optional[str] = None, escaping: Optional
override_schemas=override_schemas)
return checked_items, problems
return checked_items


def remove_empty_properties(data: Optional[Union[list, dict]]) -> None:
if isinstance(data, dict):
for key in list(data.keys()):
if (value := data[key]) in [None, "", {}, []]:
del data[key]
else:
remove_empty_properties(value)
elif isinstance(data, list):
for item in data:
remove_empty_properties(item)
12 changes: 12 additions & 0 deletions dcicutils/misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,18 @@ 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]]) -> None:
if isinstance(data, dict):
for key in list(data.keys()):
if (value := data[key]) in [None, "", {}, []]:
del data[key]
else:
remove_empty_properties(value)
elif isinstance(data, list):
for item in data:
remove_empty_properties(item)


class ObsoleteError(Exception):
pass

Expand Down

0 comments on commit 5aaaaed

Please sign in to comment.