I got this following function in Ruby which uses gsub
.
def class_to_endpoint(klass)
klass.name.split('::').last.
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
How can I implement this in Python re? Please help me
Tried this on irb console and and I can give few examples, basically adds an underscore bw each word in camelcase syntax
UserProfile
->user_profile
LastModifiedTime
->last_modified_time
User-Profile
->user_profile
Answer: I think this is all I wanted -> Elegant Python function to convert CamelCase to snake_case?
Copied from the above link and modified a bit
def class_to_endpoint(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).replace('-', '_').lower()