tl;dr: A beautiful pattern exposes the "innards" of any class to g.printObj, Leo's best way of showing data.
Here is the new __repr__ for the g.GeneralSetting class:
def __repr__(self):
# Better for g.printObj.
val = str(self.val).replace('\n', ' ')
return 'GS: %20s %7s = %s' % (
g.shortFileName(self.path), self.kind, g.truncate(val, 50))
This puts the crucial data on a single line. This is exactly what I'll need when working in the settings branch.
Here is the __repr__ for both the g.TypedDict and g.TypedDictOfLists classes:
def __repr__(self):
"""Suitable for g.printObj"""
return '%s\n%s\n' % (g.dictToString(self.d), str(self))
g.dictToString is one of g.printObj's helpers. It aligns all the dictionary keys. Crucially, g.printObj need not know about the "inner" dict, self.d. g.printObj "just works".
The __str__ method of the two g.TypedDict classes is a summary. For example, for g.TypedDict:
def __str__(self):
"""Concise: used by repr."""
return '<TypedDict name:%s keys:%s values:%s len(keys): %s>' % (
self._name,
self.keyType.__name__,
self.valType.__name__,
len(list(self.keys())))
Summary
This pattern leverages all the good work I have done with g.printObj. When a class's __repr__ method returns g.objToString (or one of it's helpers), __repr__ will do a better job than a hand-written dump method.
I'm glad I didn't discover this pattern until well into the work on
#1316. Had I done so, I probably would not have done the work I did on cleaning g.TypedDict and g.TypedDictOfLists, to be described in another post.
Edward