title: python recursively rename dictionary keys with function
body: <p>I want to take a nested dictionary and rename all the keys from camelcase to snake_case.  I have the current code but it is not working.</p>
  <pre><code>def camel_to_snake(name):
      name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
      return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
  
  def alter_keys(dictionary, func):
      empty = {}
      for k, v in dictionary.items():
          if isinstance(v, dict):
              alter_keys(v, func)
          if isinstance(v, str):
              empty[func(k)] = dictionary.pop(k)
          
      return empty
  
  x = alter_keys(random_nested_dict, camel_to_snake)
  </code></pre>