Skip to content

Commit

Permalink
Update docs and tests for attr_pattern (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored Apr 29, 2022
1 parent a855ff8 commit 54beee6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,22 @@ Or you can use it for ``op()`` functions individually with some conditions
op(obj, enable=check_do_print())
```

### include/exclude attributes
### attribute selection

You can include/exclude attributes using regular expression so ```objprint``` will only print
out the attributes you are interested in.
You can customize which attribute to print with name filters.

``objprint`` will try to match the attribute name with ``attr_pattern`` regex. The default
``attr_pattern`` is ``r"(!_).*"``, which means anything that does NOT start with an `_`.

You can customize ``attr_pattern`` to select the attributes you want to print:

```python
# This will print all the attributes that do not start with __
op(Player(), attr_pattern=r"(!__).*")
```

You can also use ``include`` and ``exclude`` to specify attributes to print with regular expression
so ```objprint``` will only print out the attributes you are interested in.

```python
op(Player(), include=["name"])
Expand Down Expand Up @@ -271,7 +283,7 @@ op(Player(), exclude=[".*s"])
If you specify both ``include`` and ``exclude``, it will do a inclusive check first, then filter out the attributes
that match exclusive check.

```include``` and ```exclude``` arguments work on ```objprint```, ```objstr``` and ```@add_objprint```.
```attr_pattern```, ```include``` and ```exclude``` arguments work on ```objprint```, ```objstr``` and ```@add_objprint```.

### config

Expand All @@ -288,6 +300,9 @@ that match exclusive check.
* ``arg_name(False)`` - whether to print the argument expression before the argument value
* ``skip_recursion(True)`` - whether skip printing recursive data, which would cause infinite recursion without ``depth`` constraint
* ``honor_existing(True)`` - whether to use the existing user defined ``__repr__`` or ``__str__`` method
* ``attr_pattern(r"(!_).*")`` - the regex pattern for attribute selection
* ``include([])`` - the list of attribute regex to do an inclusive filter
* ``exclude([])`` - the list of attribute regex to do an exclusive filter

You can set the configs globally using ``config`` function

Expand Down
8 changes: 8 additions & 0 deletions tests/test_objprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ def test_arg_name(self):
output = buf.getvalue()
self.assertIn("Unknown", output.split("\n")[0])

def test_config_attr_pattern(self):
with io.StringIO() as buf, redirect_stdout(buf):
obj = ObjTest({"elem1": 1, "elem2": 2, "attr3": 3})
op(obj, indent=1, attr_pattern=r"elem.")
output = buf.getvalue()
expected = r"<ObjTest 0x[0-9a-fA-F]*\n .elem1 = 1,\n .elem2 = 2\n>\n"
self.assertRegex(output, expected)

def test_config_include(self):
with io.StringIO() as buf, redirect_stdout(buf):
obj = ObjTest({"elem1": 1, "elem2": 2, "elem3": 3})
Expand Down
9 changes: 9 additions & 0 deletions tests/test_objstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def test_id(self):
self.assertIn(hex(obj_id), objstr(obj, color=True))
self.assertIn(hex(obj_id), objstr(obj, color=False))

def test_attr_pattern(self):
t = ObjTest({"pos1": "in", "pos2": "out", "other3": "ex"})
output = objstr(t, attr_pattern=r"pos.")
self.assertIn("pos1", output)
self.assertIn("pos2", output)
self.assertNotIn("other3", output)
with self.assertRaises(TypeError):
output = objstr(t, attr_pattern=[r"pos."])

def test_include(self):
t = ObjTest({"pos1": "in", "pos2": "out", "pos3": "ex"})
output = objstr(t, include=['pos1'])
Expand Down

0 comments on commit 54beee6

Please sign in to comment.