kr8s-org / kr8s

A batteries-included Python client library for Kubernetes that feels familiar for folks who already know how to use kubectl
https://kr8s.org
BSD 3-Clause "New" or "Revised" License
834 stars 45 forks source link

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

Open jacobtomlinson opened 1 month ago

jacobtomlinson commented 1 month ago

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 commented 1 month ago

I opened https://github.com/cdgriffith/Box/issues/279 to see if this is something that could be implemented upstream in box.