pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python-gitlab/python-gitlab/commit/d69ba0479a4537bbc7a53f342661c1984382f939

ous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-5efd63e783ac04bb.css" /> chore: add `pprint()` and `pformat()` methods to RESTObject · python-gitlab/python-gitlab@d69ba04 · GitHub
Skip to content

Commit d69ba04

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 d69ba04

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

docs/api-usage.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ 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+
189+
# Or in a prettier format.
190+
project.pprint()
191+
192+
# Or explicitly via `pformat()`. This is equivalent to the above.
193+
print(project.pformat())
194+
195+
182196
Base types
183197
==========
184198

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()}>"

tests/unit/test_base.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,33 @@ def test_inequality_no_id(self, fake_manager):
201201
obj1 = FakeObject(fake_manager, {"attr1": "foo"})
202202
obj2 = FakeObject(fake_manager, {"attr1": "bar"})
203203
assert obj1 != obj2
204+
205+
def test_dunder_str(self, fake_manager):
206+
fake_object = FakeObject(fake_manager, {"attr1": "foo"})
207+
assert str(fake_object) == (
208+
"<class 'tests.unit.test_base.FakeObject'> => {'attr1': 'foo'}"
209+
)
210+
211+
def test_pformat(self, fake_manager):
212+
fake_object = FakeObject(
213+
fake_manager, {"attr1": "foo" * 10, "ham": "eggs" * 15}
214+
)
215+
assert fake_object.pformat() == (
216+
"<class 'tests.unit.test_base.FakeObject'> => "
217+
"\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n"
218+
" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}"
219+
)
220+
221+
def test_pprint(self, capfd, fake_manager):
222+
fake_object = FakeObject(
223+
fake_manager, {"attr1": "foo" * 10, "ham": "eggs" * 15}
224+
)
225+
result = fake_object.pprint()
226+
assert result is None
227+
stdout, stderr = capfd.readouterr()
228+
assert stdout == (
229+
"<class 'tests.unit.test_base.FakeObject'> => "
230+
"\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n"
231+
" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}\n"
232+
)
233+
assert stderr == ""

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy