# Camel to \_snake helper - where to put?

**URL:** <https://forum.ansible.com/t/camel-to-snake-helper-where-to-put/20932>\
**Category:** Ansible Developer\
**Tags:** aws\
**Created:** [March 20, 2016, 9:07am UTC](https://forum.ansible.com/t/camel-to-snake-helper-where-to-put/20932 "2016-03-20T09:07:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [March 20, 2016, 9:07am UTC](https://forum.ansible.com/t/camel-to-snake-helper-where-to-put/20932/1 "2016-03-20T09:07:53Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [March 26, 2016, 7:38am UTC](https://forum.ansible.com/t/camel-to-snake-helper-where-to-put/20932/2 "2016-03-26T07:38:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Allen\_Sanabria1](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Allen\_Sanabria1](https://forum.ansible.com/u/Allen_Sanabria1)\
**Post date:** [March 28, 2016, 12:09pm UTC](https://forum.ansible.com/t/camel-to-snake-helper-where-to-put/20932/3 "2016-03-28T12:09:20Z")

</div>

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](https://github.com/linuxdynasty/ld-ansible-modules/blob/master/test/cloud/amazon/test_ec2_vpc_nat_gateway.py)…

`

```auto
    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')

```

`
