
| Current Path : /usr/local/lib/python3.8/dist-packages/txt/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : //usr/local/lib/python3.8/dist-packages/txt/convert_camel_to_snake.py |
import re
from .remove_non_alphanumeric import remove_non_alphanumeric as remove_non_alpha
_first_cap_re = re.compile('(.)([A-Z][a-z]+)')
_all_cap_re = re.compile('([a-z0-9])([A-Z])')
def convert_camel_to_snake(string, remove_non_alphanumeric=True):
"""
converts CamelCase to snake_case
:type string: str
:rtype: str
"""
if remove_non_alphanumeric:
string = remove_non_alpha(string, replace_with='_', keep_underscore=True)
s1 = _first_cap_re.sub(r'\1_\2', string)
result = _all_cap_re.sub(r'\1_\2', s1).lower()
result = re.sub(pattern='\s*_+', repl="_", string=result)
return result