-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamel_2_snake_case.py
More file actions
28 lines (21 loc) · 905 Bytes
/
Copy pathcamel_2_snake_case.py
File metadata and controls
28 lines (21 loc) · 905 Bytes
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
import argparse
import re
# Example: getValue -> get_value
def camel_2_snake_case(s):
if s.isdigit() or not s.strip():
return s # Nothing to be done
regex = (
"(?<=[a-z])(?=[0-9A-Z])|" # Handles transitions like a1 or aA
"(?<=[0-9])(?=[a-zA-Z])" # Handles transitions like 0a or 1B
# "|(?<=[A-Z0-9]{3,})(?=[a-z])" # Handles transitions for Acronyms like SGCUfinished --> re.error: look-behind requires fixed-width pattern
)
replacement = "_"
s = re.sub(regex, replacement, s).lower()
return s
# Parsing command line arguments
parser = argparse.ArgumentParser(description="Converts an input value from camel case to snake case.")
parser.add_argument("value", type=str, help="Value to be converted from camel case to snake case.")
args = parser.parse_args()
# Call the split_file function
value = camel_2_snake_case(args.value)
print(value)