FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

chore: add `pprint()` and `pformat()` methods to RESTObject by JohnVillalovos · Pull Request #1812 · python-gitlab/python-gitlab · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 14 additions & 0 deletions docs/api-usage.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ 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.
project.pprint()

# Or explicitly via `pformat()`. This is equivalent to the above.
print(project.pformat())


Base types
==========

Expand Down
9 changes: 9 additions & 0 deletions gitlab/base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -147,6 +148,14 @@ def __str__(self) -> str:
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()}>"
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,33 @@ def test_inequality_no_id(self, fake_manager):
obj1 = FakeObject(fake_manager, {"attr1": "foo"})
obj2 = FakeObject(fake_manager, {"attr1": "bar"})
assert obj1 != obj2

def test_dunder_str(self, fake_manager):
fake_object = FakeObject(fake_manager, {"attr1": "foo"})
assert str(fake_object) == (
"<class 'tests.unit.test_base.FakeObject'> => {'attr1': 'foo'}"
)

def test_pformat(self, fake_manager):
fake_object = FakeObject(
fake_manager, {"attr1": "foo" * 10, "ham": "eggs" * 15}
)
assert fake_object.pformat() == (
"<class 'tests.unit.test_base.FakeObject'> => "
"\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n"
" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}"
)

def test_pprint(self, capfd, fake_manager):
fake_object = FakeObject(
fake_manager, {"attr1": "foo" * 10, "ham": "eggs" * 15}
)
result = fake_object.pprint()
assert result is None
stdout, stderr = capfd.readouterr()
assert stdout == (
"<class 'tests.unit.test_base.FakeObject'> => "
"\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n"
" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}\n"
)
assert stderr == ""

Back | FazBrowse Home | New Git URL