Created
August 25, 2018 15:10
-
-
Save eht16/52067a31f2d8cfc3c0fac87d2ab70e28 to your computer and use it in GitHub Desktop.
CamelCase <-> lower_case_underscore conversion fpr Geany
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 | |
# -*- coding: utf-8 -*- | |
import sys | |
def camel_case_to_lower_case_underscore(string): | |
""" | |
Split string by upper case letters. | |
F.e. useful to convert camel case strings to underscore separated ones. | |
@return words (list) | |
""" | |
words = [] | |
from_char_position = 0 | |
for current_char_position, char in enumerate(string): | |
if char.isupper() and from_char_position < current_char_position: | |
words.append(string[from_char_position:current_char_position].lower()) | |
from_char_position = current_char_position | |
words.append(string[from_char_position:].lower()) | |
return '_'.join(words) | |
def lower_case_underscore_to_camel_case(string): | |
"""Convert string or unicode from lower-case underscore to camel-case""" | |
splitted_string = string.split('_') | |
# use string's class to work on the string to keep its type | |
class_ = string.__class__ | |
return splitted_string[0] + class_.join('', map(class_.capitalize, splitted_string[1:])) | |
def read_data(): | |
return sys.stdin.read() | |
def detect_conversion_method(data): | |
if '_' in data: | |
return lower_case_underscore_to_camel_case | |
else: | |
return camel_case_to_lower_case_underscore | |
def main(): | |
data = read_data() | |
conversion_method = detect_conversion_method(data) | |
result = conversion_method(data) | |
sys.stdout.write(result) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment