forked from scharlton2/20221024USGS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeutils.py
39 lines (31 loc) · 859 Bytes
/
snakeutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
import re
rx_capword = re.compile(r"[A-Z][^A-Z]+")
rx_nonword = re.compile(r"[^a-zA-Z0-9]+")
def snake_to_camel(name):
# should split FooBar as well
if re.search('[A-Z]', name):
new_name = ""
for word in rx_capword.findall(name):
new_word = split_word(word)
new_name += new_word
else:
new_name = split_word(name)
return new_name
def split_word(word):
words = rx_nonword.split(word)
return ''.join(w.capitalize() for w in words)
if __name__ == '__main__':
test_strings = [
"junk stuff",
"test_case",
"wombat_nest",
"blue_meany",
"funk-ball",
"BigOld_world",
"Spam.Toast",
"JunkFile",
"blorb",
]
for t in test_strings:
print("{:12s} {}".format(t, snake_to_camel(t)))