1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add function to convert CamelCased key names to snake_names

This commit is contained in:
Rob White 2016-03-26 18:46:50 +11:00
parent eff89d6290
commit ef8c1124d0

View file

@ -262,3 +262,24 @@ def paging(pause=0):
return page
return wrapper
def camel_dict_to_snake_dict(camel_dict):
def camel_to_snake(name):
import re
first_cap_re = re.compile('(.)([A-Z][a-z]+)')
all_cap_re = re.compile('([a-z0-9])([A-Z])')
s1 = first_cap_re.sub(r'\1_\2', name)
return all_cap_re.sub(r'\1_\2', s1).lower()
snake_dict = {}
for k, v in camel_dict.iteritems():
if isinstance(v, dict):
v = camel_dict_to_snake_dict(v)
snake_dict[camel_to_snake(k)] = v
return snake_dict