Camel to _snake helper - where to put?

I’ve written two little helpful functions that I’ve been using with AWS modules because the AWS API returns results in CamelCase and Ansible uses _snake.

Here’s the code:

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'_', name)

    return all_cap_re.sub(r'_', s1).lower()

def camel_dict_to_snake_dict(camel_dict):

    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

Simples.

Just wanted some feedback on where to put this?

I thought just stick it in module_utils/ec2 but i guess this could be useful outside of Amazon land.

Any other location suggestions?

OK, no feedback so i’m just going to plumb for ec2 and it can always be moved later if needs be.

I also do the same in my other modules. I would like to see some tests that can go with this. This would also save me a lot of duplicated code across my other modules.

Example of what I am doing here https://github.com/linuxdynasty/ld-ansible-modules/blob/master/test/cloud/amazon/test_ec2_vpc_nat_gateway.py

`

    def test_convert_to_lower(self):
        example = ng.DRY_RUN_GATEWAY_UNCONVERTED
        converted_example = ng.convert_to_lower(example[0])
        keys = converted_example.keys()
        keys.sort()
        for i in range(len(keys)):
            if i == 0:
                self.assertEqual(keys[i], 'create_time')
            if i == 1:
                self.assertEqual(keys[i], 'nat_gateway_addresses')
                gw_addresses_keys = converted_example[keys[i]][0].keys()
                gw_addresses_keys.sort()
                for j in range(len(gw_addresses_keys)):
                    if j == 0:
                        self.assertEqual(gw_addresses_keys[j], 'allocation_id')
                    if j == 1:
                        self.assertEqual(gw_addresses_keys[j], 'network_interface_id')
                    if j == 2:
                        self.assertEqual(gw_addresses_keys[j], 'private_ip')
                    if j == 3:
                        self.assertEqual(gw_addresses_keys[j], 'public_ip')
            if i == 2:
                self.assertEqual(keys[i], 'nat_gateway_id')
            if i == 3:
                self.assertEqual(keys[i], 'state')
            if i == 4:
                self.assertEqual(keys[i], 'subnet_id')
            if i == 5:
                self.assertEqual(keys[i], 'vpc_id')

`