Skip to content

Instantly share code, notes, and snippets.

@RadwaKamal
Created March 17, 2016 11:51
Show Gist options
  • Save RadwaKamal/c06b8a156f94332a2284 to your computer and use it in GitHub Desktop.
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.
#!/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!"
@RadwaKamal
Copy link
Author

For linux users

  1. After creating the file, run the command chmod +x change_convention.py
  2. To change naming convention in a file run ./change_convention.py <FILE_PATH>

Example ./change_convention.py categories.md

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