diff options
author | John L. Villalovos <john@sodarock.com> | 2022-01-07 12:12:24 -0800 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2022-01-07 12:12:24 -0800 |
commit | e34a2e28f2aee2ab3314927bb51891b6ace78908 (patch) | |
tree | fc331ea13daca0b10d95174a861f50c4855906e0 | |
parent | 989634055b0c5ab622ac7774b546928a564a31ef (diff) | |
download | gitlab-jlvillal/pprint.tar.gz |
chore: add `pprint()` and `pformat()` methods to RESTObjectjlvillal/pprint
This is useful in debugging and testing. As can easily print out the
values from an instance in a more human-readable form.
-rw-r--r-- | docs/api-usage.rst | 11 | ||||
-rw-r--r-- | gitlab/base.py | 9 | ||||
-rw-r--r-- | tests/unit/test_base.py | 22 |
3 files changed, 42 insertions, 0 deletions
diff --git a/docs/api-usage.rst b/docs/api-usage.rst index 66e5887..a544bdd 100644 --- a/docs/api-usage.rst +++ b/docs/api-usage.rst @@ -179,6 +179,17 @@ resources. For example: project = gl.projects.get(1) project.star() +You can print a Gitlab Object. For example: + +.. code-block:: python + + project = gl.projects.get(1) + print(project) + # Or in a prettier format. These two are equivalent. + print(project.pformat()) + project.pprint() + + Base types ========== diff --git a/gitlab/base.py b/gitlab/base.py index 50f09c5..6a6e992 100644 --- a/gitlab/base.py +++ b/gitlab/base.py @@ -16,6 +16,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import importlib +import pprint import textwrap from types import ModuleType from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type @@ -147,6 +148,14 @@ class RESTObject(object): data.update(self._updated_attrs) return f"{type(self)} => {data}" + def pformat(self) -> str: + data = self._attrs.copy() + data.update(self._updated_attrs) + return f"{type(self)} => \n{pprint.pformat(data)}" + + def pprint(self) -> None: + print(self.pformat()) + def __repr__(self) -> str: if self._id_attr: return f"<{self.__class__.__name__} {self._id_attr}:{self.get_id()}>" diff --git a/tests/unit/test_base.py b/tests/unit/test_base.py index 3ca0206..eec6af0 100644 --- a/tests/unit/test_base.py +++ b/tests/unit/test_base.py @@ -201,3 +201,25 @@ class TestRESTObject: obj1 = FakeObject(fake_manager, {"attr1": "foo"}) obj2 = FakeObject(fake_manager, {"attr1": "bar"}) assert obj1 != obj2 + + def test_dunder_str(self, fake_manager): + obj1 = FakeObject(fake_manager, {"attr1": "foo"}) + assert str(obj1) == ( + "<class 'tests.unit.test_base.FakeObject'> => {'attr1': 'foo'}" + ) + + def test_pformat(self, fake_manager): + obj1 = FakeObject(fake_manager, {"attr1": "foo"}) + assert obj1.pformat() == ( + "<class 'tests.unit.test_base.FakeObject'> => \n{'attr1': 'foo'}" + ) + + def test_pprint(self, capfd, fake_manager): + obj1 = FakeObject(fake_manager, {"attr1": "foo"}) + result = obj1.pprint() + assert result is None + stdout, stderr = capfd.readouterr() + assert stdout == ( + "<class 'tests.unit.test_base.FakeObject'> => \n{'attr1': 'foo'}\n" + ) + assert stderr == "" |