Skip to content

Commit 9613bd4

Browse files
separate repr and str
1 parent 48a6465 commit 9613bd4

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

linkml_runtime/utils/yamlutils.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import pdb
12
from copy import copy
23
from json import JSONDecoder
34
from typing import Union, Any, List, Optional, Type, Callable, Dict
45
from pprint import pformat
6+
import textwrap
57

68
import yaml
79
from deprecated.classic import deprecated
@@ -11,7 +13,7 @@
1113
from yaml.constructor import ConstructorError
1214

1315
from linkml_runtime.utils.context_utils import CONTEXTS_PARAM_TYPE, merge_contexts
14-
from linkml_runtime.utils.formatutils import is_empty, remove_empty_items
16+
from linkml_runtime.utils.formatutils import is_empty, remove_empty_items, is_list, is_dict, items
1517

1618
YAMLObjTypes = Union[JsonObjTypes, "YAMLRoot"]
1719

@@ -278,10 +280,20 @@ def MissingRequiredField(self, field_name: str) -> None:
278280
""" Generic loader error handler """
279281
raise ValueError(f"{field_name} must be supplied")
280282

283+
def __repr__(self):
284+
"""Only reformat 1-layer deep to preserve __repr__ of child objects"""
285+
res = {}
286+
for key, val in items(self):
287+
if val == [] or val == {} or val is None:
288+
continue
289+
res[key] = val
290+
return self.__class__.__name__ + '(' + pformat(res, indent=2,
291+
compact=True) + ')'
292+
281293
def __str__(self):
294+
"""Dump everything into a dict, recursively, stringifying it all"""
282295
res = remove_empty_items(self)
283-
return pformat(as_dict(res),indent=2,compact=True)
284-
296+
return self.__class__.__name__ + '(' + pformat(res, indent=2, compact=True) + ')'
285297

286298

287299
def root_representer(dumper: yaml.Dumper, data: YAMLRoot):

0 commit comments

Comments
 (0)