Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add to_dict method to ResourceField #1886

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2023
Merged

Add to_dict method to ResourceField #1886

merged 1 commit into from
Feb 25, 2023

Conversation

0xFelix
Copy link
Contributor

@0xFelix 0xFelix commented Sep 12, 2022

What type of PR is this?

/kind feature

What this PR does / why we need it:

This allows to recursively convert ResourceFields to dicts.

Does this PR introduce a user-facing change?

Add to_dict method to ResourceField

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Sep 12, 2022
@k8s-ci-robot
Copy link
Contributor

Welcome @0xFelix!

It looks like this is your first PR to kubernetes-client/python 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/python has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Sep 12, 2022
@roycaihw
Copy link
Member

/assign @fabianvf

@0xFelix
Copy link
Contributor Author

0xFelix commented Oct 7, 2022

@fabianvf Ping

@pmareke
Copy link
Contributor

pmareke commented Oct 10, 2022

Sorry @0xFelix and @roycaihw I think is good practice if you put first in the code the to_dict and later the __serialize as the first one calls the second and the code is more readable.

Great PR by the way!

@0xFelix
Copy link
Contributor Author

0xFelix commented Oct 11, 2022

@pmareke Thanks! Can you describe this in a little more detail? I'm not sure how you would change it.

__serialize is called recursively on every ResourceField beginning with the root object. to_dict is starting that recursion by calling __serialize on the root object.

@pmareke
Copy link
Contributor

pmareke commented Oct 11, 2022

@0xFelix I mean just invert the order of the methods:

Move the to_dict (from line 403 to line 393) method above the __serialize

image

@0xFelix
Copy link
Contributor Author

0xFelix commented Oct 14, 2022

@pmareke Sorry, got it!

@pmareke
Copy link
Contributor

pmareke commented Oct 14, 2022

Great job @0xFelix !!

As you're using returns in each if you can simplify a little bit the method:

def __serialize(self, field):
        if isinstance(field, ResourceField):
            return {
                k: self.__serialize(v) for k, v in field.__dict__.items()
            }
        if isinstance(field, (list, tuple)):
            return [self.__serialize(item) for item in field]
        return field

This allows to recursively convert ResourceFields to dicts.

Signed-off-by: Felix Matouschek <[email protected]>
@0xFelix
Copy link
Contributor Author

0xFelix commented Oct 17, 2022

@pmareke Done!

@0xFelix
Copy link
Contributor Author

0xFelix commented Nov 16, 2022

Ping @roycaihw @pmareke

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 14, 2023
@0xFelix
Copy link
Contributor Author

0xFelix commented Feb 14, 2023

/remove-lifecycle stale

Ping @roycaihw @pmareke

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 14, 2023
@pmareke
Copy link
Contributor

pmareke commented Feb 15, 2023

/remove-lifecycle stale

Ping @roycaihw @pmareke

Hi! My comment was just a suggestion, please @roycaihw go ahead.

@roycaihw
Copy link
Member

/lgtm
/approve
Thank you!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 25, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: 0xFelix, roycaihw

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 25, 2023
@k8s-ci-robot k8s-ci-robot merged commit 0c6714a into kubernetes-client:master Feb 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants