Skip to content

Commit

Permalink
Move eliminate_empty function into utils (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
caufieldjh authored Aug 6, 2024
2 parents bb98f07 + 297ba8b commit 198a993
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
19 changes: 2 additions & 17 deletions src/ontogpt/io/json_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,10 @@
import json
from typing import Any, Optional, TextIO

import pydantic

logger = logging.getLogger(__name__)
from ontogpt.io.utils import eliminate_empty


def eliminate_empty(obj: Any, preserve=False) -> Any:
"""Eliminate empty lists and dicts from an object."""
if isinstance(obj, list):
return [eliminate_empty(x, preserve) for x in obj if x or preserve]
elif isinstance(obj, dict):
return {k: eliminate_empty(v, preserve) for k, v in obj.items() if v or preserve}
elif isinstance(obj, pydantic.BaseModel):
return eliminate_empty(obj.model_dump(), preserve)
elif isinstance(obj, tuple):
return [eliminate_empty(x, preserve) for x in obj]
elif isinstance(obj, str):
return str(obj)
else:
return obj
logger = logging.getLogger(__name__)


def dump_minimal_json(obj: Any, minimize=True, file: Optional[TextIO] = None) -> str:
Expand Down
21 changes: 21 additions & 0 deletions src/ontogpt/io/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""IO Utilities."""

from typing import Any

import pydantic


def eliminate_empty(obj: Any, preserve=False) -> Any:
"""Eliminate empty lists and dicts from an object."""
if isinstance(obj, list):
return [eliminate_empty(x, preserve) for x in obj if x or preserve]
elif isinstance(obj, dict):
return {k: eliminate_empty(v, preserve) for k, v in obj.items() if v or preserve}
elif isinstance(obj, pydantic.BaseModel):
return eliminate_empty(obj.model_dump(), preserve)
elif isinstance(obj, tuple):
return [eliminate_empty(x, preserve) for x in obj]
elif isinstance(obj, str):
return str(obj)
else:
return obj
19 changes: 2 additions & 17 deletions src/ontogpt/io/yaml_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,11 @@
import logging
from typing import Any, Optional, TextIO

import pydantic
from ruamel.yaml import YAML, RoundTripRepresenter

logger = logging.getLogger(__name__)

from ontogpt.io.utils import eliminate_empty

def eliminate_empty(obj: Any, preserve=False) -> Any:
"""Eliminate empty lists and dicts from an object."""
if isinstance(obj, list):
return [eliminate_empty(x, preserve) for x in obj if x or preserve]
elif isinstance(obj, dict):
return {k: eliminate_empty(v, preserve) for k, v in obj.items() if v or preserve}
elif isinstance(obj, pydantic.BaseModel):
return eliminate_empty(obj.model_dump(), preserve)
elif isinstance(obj, tuple):
return [eliminate_empty(x, preserve) for x in obj]
elif isinstance(obj, str):
return str(obj)
else:
return obj
logger = logging.getLogger(__name__)


def repr_str(dumper: RoundTripRepresenter, data: str):
Expand Down

0 comments on commit 198a993

Please sign in to comment.