QString::simplified() in Python
Just in case anybody searches for a Python pendant of QString::simplified(): This is surprisingly easy to do:
def simplify(string):
return ' '.join(string.strip().split())
If one wants to subclass a Python string and add the above function, this can be done e. g. like so (subclassing a string is a bit tricky, as one needs to mess with the __new__ function instead of the "normal" __init__ one):
class NewString(str):
def __new__(self, value):
return str.__new__(self, value)
def simplified(self):
return ' '.join(self.strip().split())
This can then be used like so:
>>> someString = NewString(' a lot of tabs and spaces ')
>>> someString
'\ta lot of tabs\tand spaces '
>>> someString.simplified()
'a lot of tabs and spaces'