Created
March 17, 2016 11:51
-
-
Save RadwaKamal/c06b8a156f94332a2284 to your computer and use it in GitHub Desktop.
A simple python script to convert naming convention from lower camel case to snake case in a file.
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
#!/usr/bin/env python | |
#Simple python script to change lower camel case strings in a file to snake case | |
import sys | |
import re | |
#detect if the string is lower camel case | |
def is_camel(word): | |
bol = re.search('\w([a-z][A-Z])', word) | |
return bol | |
#convert string to snake case | |
def convert(word): | |
word = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', word) | |
word = re.sub('([a-z0-9])([A-Z])', r'\1_\2', word).lower() | |
return word | |
FILE_NAME = sys.argv[1] | |
OLD_FILE = open(FILE_NAME).read() | |
NEW_FILE = open(FILE_NAME, 'w') | |
for w in OLD_FILE.split(): | |
if is_camel(w) != None: | |
OLD_FILE = OLD_FILE.replace(w, convert(w)) | |
NEW_FILE.write(OLD_FILE) | |
print "Convention changed to snake case successfully!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For linux users
chmod +x change_convention.py
./change_convention.py <FILE_PATH>
Example
./change_convention.py categories.md