I'm working on a Python project and I'm hoping to use a few shortcuts to help format class data in a string. More specifically, I would like to be able to use something similar to '{a}{b}{c}'.format(**vars(self), [strlen, strlen, strlen]) and specify the string length of each attribute that is displayed. For example:
class Dummy(object):
def __init__(self):
self.value1 = 'A VALUE'
self.value2 = 'ANOTHER VALUE'
self.value3 = 'THIRD VALUE'
def to_s(self):
# want value1 to be 20 chars
# value2 to be 8 chars
# value3 to be 10 chars
# is something similar to this possible
return '{value1},{value2},{value3}'.format(**vars(self), [20, 8, 10])
def to_s2(self):
# or will I have to reference each explicitly and specify the either padding or slicing?
return '{},{},{}'.format(self.value1.ljust(20), self.value2[:8], self.value3[:10])
I know it's a long shot, but a couple of these classes have 30 or 40 attribs and it would make life so much easier if this is doable.
__str__()method, if you want it to be the default string-formatting for that class?