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

Commite34a2e2

Browse files
chore: addpprint() andpformat() methods to RESTObject
This is useful in debugging and testing. As can easily print out thevalues from an instance in a more human-readable form.
1 parent9896340 commite34a2e2

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-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
importimportlib
19+
importpprint
1920
importtextwrap
2021
fromtypesimportModuleType
2122
fromtypingimportAny,Dict,Iterable,NamedTuple,Optional,Tuple,Type
@@ -147,6 +148,14 @@ def __str__(self) -> str:
147148
data.update(self._updated_attrs)
148149
returnf"{type(self)} =>{data}"
149150

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

‎tests/unit/test_base.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,25 @@ def test_inequality_no_id(self, fake_manager):
201201
obj1=FakeObject(fake_manager, {"attr1":"foo"})
202202
obj2=FakeObject(fake_manager, {"attr1":"bar"})
203203
assertobj1!=obj2
204+
205+
deftest_dunder_str(self,fake_manager):
206+
obj1=FakeObject(fake_manager, {"attr1":"foo"})
207+
assertstr(obj1)== (
208+
"<class 'tests.unit.test_base.FakeObject'> => {'attr1': 'foo'}"
209+
)
210+
211+
deftest_pformat(self,fake_manager):
212+
obj1=FakeObject(fake_manager, {"attr1":"foo"})
213+
assertobj1.pformat()== (
214+
"<class 'tests.unit.test_base.FakeObject'> =>\n{'attr1': 'foo'}"
215+
)
216+
217+
deftest_pprint(self,capfd,fake_manager):
218+
obj1=FakeObject(fake_manager, {"attr1":"foo"})
219+
result=obj1.pprint()
220+
assertresultisNone
221+
stdout,stderr=capfd.readouterr()
222+
assertstdout== (
223+
"<class 'tests.unit.test_base.FakeObject'> =>\n{'attr1': 'foo'}\n"
224+
)
225+
assertstderr==""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp