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

Commit0975678

Browse files
feat: add anasdict() method to the Gitlab Objects
Add an `asdict()` method that returns a dictionary representation copyof the Gitlab Object. This is a copy and changes made to it will haveno impact on the Gitlab Object.The `asdict()` method name was chosen as both the `dataclasses` and`attrs` libraries have an `asdict()` function which has the similarpurpose of creating a dictionary represenation of an object.
1 parent64d01ef commit0975678

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

‎docs/api-usage.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ You can print a Gitlab Object. For example:
192192
# Or explicitly via `pformat()`. This is equivalent to the above.
193193
print(project.pformat())
194194
195+
You can get a dictionary representation copy of the Gitlab Object. Modifications made to
196+
the dictionary will have no impact on the GitLab Object.
197+
198+
..code-block::python
199+
200+
project= gl.projects.get(1)
201+
project_dict= project.asdict()
202+
195203
196204
Base types
197205
==========

‎gitlab/base.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
importcopy
1819
importimportlib
1920
importpprint
2021
importtextwrap
@@ -144,15 +145,16 @@ def __getattr__(self, name: str) -> Any:
144145
def__setattr__(self,name:str,value:Any)->None:
145146
self.__dict__["_updated_attrs"][name]=value
146147

147-
def__str__(self)->str:
148-
data=self._attrs.copy()
148+
defasdict(self)->Dict[str,Any]:
149+
data=copy.deepcopy(self._attrs)
149150
data.update(self._updated_attrs)
150-
returnf"{type(self)} =>{data}"
151+
returndata
152+
153+
def__str__(self)->str:
154+
returnf"{type(self)} =>{self.asdict()}"
151155

152156
defpformat(self)->str:
153-
data=self._attrs.copy()
154-
data.update(self._updated_attrs)
155-
returnf"{type(self)} =>\n{pprint.pformat(data)}"
157+
returnf"{type(self)} =>\n{pprint.pformat(self.asdict())}"
156158

157159
defpprint(self)->None:
158160
print(self.pformat())

‎tests/unit/test_base.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,13 @@ def test_pprint(self, capfd, fake_manager):
249249
" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}\n"
250250
)
251251
assertstderr==""
252+
253+
deftest_asdict(self,fake_manager):
254+
fake_object=FakeObject(fake_manager, {"attr1":"foo"})
255+
assertfake_object.attr1=="foo"
256+
result=fake_object.asdict()
257+
assertresult== {"attr1":"foo"}
258+
259+
result["attr1"]="testing"
260+
assertresult== {"attr1":"testing"}
261+
assertfake_object.attr1=="foo"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp