Created
November 21, 2010 06:13
-
-
Save irskep/708505 to your computer and use it in GitHub Desktop.
Add snake_case version of camelCase methods to classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or the "shorter = better" version: