Skip to content
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

Camel killer box, support snake case attributes on data structures non-destructively #495

Open
jacobtomlinson opened this issue Sep 17, 2024 · 1 comment
Labels
enhancement New feature or request idea

Comments

@jacobtomlinson
Copy link
Member

Which project are you requesting an enhancement for?

kr8s

What do you need?

We use box to make accessing nested data structures more pleasant and pythonic.

Right now we just use the default box settings, which means any data structures that use camel case keys needs to be accessed via camel case attributes.

>>> import box
>>> data = box.Box({"loadBalancer": True})
>>> data.loadBalancer
True

Box supports a camel killer box to make keys like loadBalancer more pythonic by optionally converting them to snake case like load_balancer.

>>> import box
>>> data = box.Box({"loadBalancer": True}, camel_killer_box=True)

You can access the loadBalancer key using either .loadBalancer or .load_balancer. Which is very pleasant.

>>> data.load_balancer
True
>>> data.loadBalancer
True

Unfortunately this operation in box is destructive and modifies the underlying data structure to store data in the snake case form. Which means when converting back to a dictionary you don't get the same structure that you put in.

>>> data.to_dict()
{'load_balancer': True}

Given that kr8s needs to be able to update data back to the Kubernetes API we can't be mangling the data structures like this.

It would be great if we could somehow support this snake case attribute access without modifying the data structure so that we can serialize data back out transparently.

@jacobtomlinson jacobtomlinson added enhancement New feature or request idea labels Sep 17, 2024
@jacobtomlinson jacobtomlinson changed the title Camel killer box, support snake case attributes on data structures Camel killer box, support snake case attributes on data structures non-destructively Sep 17, 2024
@jacobtomlinson
Copy link
Member Author

I opened cdgriffith/Box#279 to see if this is something that could be implemented upstream in box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request idea
Projects
None yet
Development

No branches or pull requests

1 participant