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

feat: addasdict() andto_json() methods to Gitlab Objects#1872

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 2 commits intomainfromjlvillal/as_dict
Jul 20, 2022

Conversation

JohnVillalovos
Copy link
Member

@JohnVillalovosJohnVillalovos commentedFeb 2, 2022
edited
Loading

Add anasdict() method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

Theasdict() method name was chosen as both thedataclasses and
attrs libraries have anasdict() function which has the similar
purpose of creating a dictionary represenation of an object.

Also add ato_json() method that returns a JSON string
representation of the object.

Closes:#1116

@JohnVillalovosJohnVillalovos changed the titlefeat: add aas_dict() method to the Gitlab Objectsfeat: add anas_dict() method to the Gitlab ObjectsFeb 2, 2022
@JohnVillalovosJohnVillalovos changed the titlefeat: add anas_dict() method to the Gitlab Objectsfeat: add anasdict() method to the Gitlab ObjectsFeb 2, 2022
@codecov-commenter
Copy link

codecov-commenter commentedFeb 2, 2022
edited
Loading

Codecov Report

Merging#1872 (08ac071) intomain (f6b6e18) willdecrease coverage by0.01%.
The diff coverage is93.75%.

@@            Coverage Diff             @@##             main    #1872      +/-   ##==========================================- Coverage   95.41%   95.39%   -0.02%==========================================  Files          79       79                Lines        5166     5170       +4     ==========================================+ Hits         4929     4932       +3- Misses        237      238       +1
FlagCoverage Δ
api_func_v481.21% <81.25%> (+0.07%)⬆️
cli_func_v482.70% <81.25%> (+0.07%)⬆️
unit87.04% <93.75%> (-0.01%)⬇️

Flags with carried forward coverage won't be shown.Click here to find out more.

Impacted FilesCoverage Δ
gitlab/base.py99.49% <93.75%> (-0.51%)⬇️

@nejch
Copy link
Member

The more I compare this to.attributes, the more I think it's doing something very very similar and the property does have more info available.

What if we add a method liketo_json() that reuses our existing property to address#1116? That's another common pattern (see pandas), and we could potentially pass args todumps for things like indenting etc.

I think this would make the example in the issue quite pretty as well:

withopen('data.txt','w')asf:f.write(src_prj.labels.list(all=True)[0].to_json())

@nejch
Copy link
Member

The more I compare this to.attributes, the more I think it's doing something very very similar and the property does have more info available.

What if we add a method liketo_json() that reuses our existing property to address#1116? That's another common pattern (see pandas), and we could potentially pass args todumps for things like indenting etc.

I think this would make the example in the issue quite pretty as well:

withopen('data.txt','w')asf:f.write(src_prj.labels.list(all=True)[0].to_json())

Going back again, seems like the library supported both json and dict method for v3 with a custom encoder back in 2017. So maybe we could just re-add both:

defjson(self):
"""Dump the object as json.
Returns:
str: The json string.
"""
returnjson.dumps(self,cls=jsonEncoder)
defas_dict(self):
"""Dump the object as a dict."""
return {k:vfork,vinsix.iteritems(self.__dict__)
if (notisinstance(v,BaseManager)andnotk[0]=='_')}

@JohnVillalovosJohnVillalovosforce-pushed thejlvillal/as_dict branch 2 times, most recently fromf2ed5e5 tof229f7fCompareFebruary 2, 2022 22:27
@JohnVillalovos
Copy link
MemberAuthor

Going back again, seems like the library supported both json and dict method for v3 with a custom encoder back in 2017. So maybe we could just re-add both:

Sounds good to me. I've gotasdict in there. Let me see aboutto_json

@JohnVillalovosJohnVillalovos marked this pull request as draftFebruary 12, 2022 16:54
@JohnVillalovosJohnVillalovosforce-pushed thejlvillal/as_dict branch 3 times, most recently from4a64a56 to973e68fCompareJuly 6, 2022 03:19
@JohnVillalovosJohnVillalovos marked this pull request as ready for reviewJuly 6, 2022 03:21
@JohnVillalovosJohnVillalovos changed the titlefeat: add anasdict() method to the Gitlab Objects feat: addasdict() andto_json() methods to Gitlab ObjectsJul 6, 2022
@JohnVillalovosJohnVillalovosforce-pushed thejlvillal/as_dict branch 6 times, most recently fromef5aed2 toec643b7CompareJuly 12, 2022 15:09
@max-wittig
Copy link
Member

Very cool!

Previously the `attributes` method would show the original values in aGitlab Object even if they had been updated. Correct this so that theupdated value will be returned.Also use copy.deepcopy() to ensure that modifying the dictionary returned cannot also modify the object.
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.Also add a `to_json()` method that returns a JSON stringrepresentation of the object.Closes:#1116
@nejchnejch merged commitfcbced8 intomainJul 20, 2022
@nejchnejch deleted the jlvillal/as_dict branchJuly 20, 2022 20:11
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nejchnejchAwaiting requested review from nejch

Assignees

@JohnVillalovosJohnVillalovos

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

ProjectLabel objects are not JSON serializable
4 participants
@JohnVillalovos@codecov-commenter@nejch@max-wittig

[8]ページ先頭

©2009-2025 Movatter.jp