Skip to content

Commit 5c708fb

Browse files
chore: add pprint() and pformat() methods to RESTObject
This is useful in debugging and testing. As can easily print out the values from an instance in a more human-readable form.
1 parent 9896340 commit 5c708fb

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

docs/api-usage.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ resources. For example:
179179
project = gl.projects.get(1)
180180
project.star()
181181
182+
You can print a Gitlab Object. For example:
183+
184+
.. code-block:: python
185+
186+
project = gl.projects.get(1)
187+
print(project)
188+
# Or in a prettier format. These two are equivalent.
189+
print(project.pformat())
190+
project.pprint()
191+
192+
182193
Base types
183194
==========
184195

gitlab/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

1818
import importlib
19+
import pprint
1920
import textwrap
2021
from types import ModuleType
2122
from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type
@@ -147,6 +148,14 @@ def __str__(self) -> str:
147148
data.update(self._updated_attrs)
148149
return f"{type(self)} => {data}"
149150

151+
def pformat(self) -> str:
152+
data = self._attrs.copy()
153+
data.update(self._updated_attrs)
154+
return f"{type(self)} => \n{pprint.pformat(data)}"
155+
156+
def pprint(self) -> None:
157+
print(self.pformat())
158+
150159
def __repr__(self) -> str:
151160
if self._id_attr:
152161
return f"<{self.__class__.__name__} {self._id_attr}:{self.get_id()}>"

0 commit comments

Comments
 (0)