Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: addpprint() andpformat() methods to RESTObject#1812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nejch merged 1 commit intomainfromjlvillal/pprint
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletionsdocs/api-usage.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsgitlab/base.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionstests/unit/test_base.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 == ""

[8]ページ先頭

©2009-2025 Movatter.jp