I’m trying to obtain verbose names from CamelCase strings in Python but have no idea how to approach the problem.
This is the usecase:
class MyClass(object):
def verbose_name(self, camelcase):
return "my class"
# ^-- here I need a way to calculate the
# value using the camelcase argument
def __str__(self):
return self.verbose_name(self.__class__.__name__)
I tried to implement a solution where the chunks my and class are generated detecting transitions from lower to uppercase letters, but it’s very procedural, doesn’t work and it’s becoming too complex for such a simple task.
Any suggestion for a simple implementation to solve the problem?
If I understand your requirement correctly, you want to split a camel case string on the Case boundary. Regex can be quite handy here
Try the following implementation
The above implementation would fail if the name is non CamelCase conforming. In such cases, make the
capsoptional