I'm trying to implement pretty looking results from analysis while working in interactive shell (IPython in my case). This is some sort of mapping, also the order is important to me, so I have tried to inherit from OrderedDict and to override __repr__ method but this approach did not work. The minimum test example:
In[1]: from collections import OrderedDict
In[2]: class Result(OrderedDict):
def __repr__(self):
if self.keys():
report = []
for k, v in self.items():
report.append(k.ljust(10) + ': ' + repr(v))
return "\n".join(report)
else:
return self.__class__.__name__ + "()"
In[3]: res = Result()
res['all'] = 10
res['some'] = 11
res
Out[3]: Result([('all', 10), ('some', 11)])
But when I inherit from dict (class Result(dict):) this approach works as expected, and the result looks as:
Out[3]: all : 10
some : 11
But the order is not guaranteed. I have no idea, why the first approach does not work?
EDIT:
Python 3.6.1
IPython 6.0.0