Skip to content

Commit

Permalink
Fix nested object with include/exclude issue (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored May 10, 2021
1 parent d00438b commit f59302c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/objprint/objprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ def objstr(self, obj, indent_level=0, include=[], exclude=[]):
def _get_custom_object_str(self, obj, indent_level, include=[], exclude=[]):

def _get_line(key):
val = self.objstr(obj.__dict__[key], indent_level + 1, include=include, exclude=exclude)
if self.label and any(re.fullmatch(pattern, key) is not None for pattern in self.label):
return set_color(f".{key} = {self.objstr(obj.__dict__[key], indent_level + 1)}", COLOR.YELLOW)
return set_color(f".{key} = {val}", COLOR.YELLOW)
elif self.color:
return f"{set_color('.'+key, COLOR.GREEN)} = {self.objstr(obj.__dict__[key], indent_level + 1)}"
return f"{set_color('.'+key, COLOR.GREEN)} = {val}"
else:
return f".{key} = {self.objstr(obj.__dict__[key], indent_level + 1)}"
return f".{key} = {val}"

if hasattr(obj, "__dict__"):
keys = []
Expand Down
7 changes: 7 additions & 0 deletions tests/test_objstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,10 @@ def test_add_match(self):
self.assertIn("xyz", actual)
self.assertIn("xyzz", actual)
self.assertNotIn("xyzxz", actual)

def test_nested_match(self):
child = ObjTest({"x": "x_val", "y": "y_val"})
parent = ObjTest({"x": "x_child_val", "y": "y_child_val", "child": child})
s = objstr(parent, exclude=["x"])
self.assertNotIn("x_child_val", s)
self.assertIn("y_child_val", s)

0 comments on commit f59302c

Please sign in to comment.