-
Notifications
You must be signed in to change notification settings - Fork 107
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 without data mangling #279
Comments
Hi @jacobtomlinson unfortunately it's named You could subclass Box and add a function like from box import Box
class CamelBox(Box):
def __init__(cls, *args, **kwargs):
kwargs["camel_killer_box"] = True
super().__init__(*args, **kwargs)
def to_camel_case_dict(self, the_dict=None):
def to_camel_case(snake_str):
components = snake_str.split('_')
return components[0] + ''.join(x.title() for x in components[1:])
if not the_dict:
the_dict = self.to_dict()
out_dict = dict()
for k, v in the_dict.items():
if isinstance(v, dict):
out_dict[to_camel_case(k)] = self.to_camel_case_dict(v)
else:
out_dict[to_camel_case(k)] = v
return out_dict Resulting in:
|
Thanks for the response @cdgriffith. The trouble with that approach is you assume that everything should be camel case. I expect in these large user provided data structures there will be a mix of both. The example you have converts everything to camel case. So the data just gets mangled in a different way. my_dict = {
"BigCamel": "hi",
"sneaky_snake": "hisss, I'm already snakey",
}
print(CamelBox(my_dict).to_camel_case_dict())
# {'bigCamel': 'hi', 'sneakySnake': "hisss, I'm already snakey"} |
The camel killer box is a great feature. I work a lot with data structures from Kubernetes which uses camel case everywhere, and it's really nice to be able to access them via snake case attributes.
I need to be able to modify these data structures and then send them back to the Kubernetes API. Unfortunately because Box defaults to destructively updating the underlying dictionary to use snake case there is no way for me to get a data structure out that the Kubernetes API will understand.
Would it be possible to add some way to round trip a dict through a Box without mangling it, and still get the benefits of accessing snake case attributes?
The text was updated successfully, but these errors were encountered: