Skip to content

Instantly share code, notes, and snippets.

@irskep
Created November 21, 2010 06:13
Show Gist options
  • Save irskep/708505 to your computer and use it in GitHub Desktop.
Save irskep/708505 to your computer and use it in GitHub Desktop.
Add snake_case version of camelCase methods to classes
import re
camel_re = re.compile(r'([a-z]+[A-Z])+[a-z]+')
def fix_camel_case(camel_class):
for camel_attr in dir(camel_class):
if camel_re.match(camel_attr):
snake_attr = ''.join(char if char.islower() or char.isdigit() else "_%s" % char.lower()
for char in camel_attr)
setattr(camel_class, snake_attr, getattr(camel_class, camel_attr))
@irskep
Copy link
Author

irskep commented Nov 21, 2010

Or the "shorter = better" version:

import re 

fix_camel_case = lambda camel_class: [setattr(camel_class, ''.join(char if char.islower() or char.isdigit() else "_%s" % char.lower() for char in camel_attr), getattr(camel_class, camel_attr)) for camel_attr in dir(camel_class) if re.match(r'([a-z0-9]+[A-Z])+[a-z0-9]+', camel_attr)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment