This is an Engineering Notebook post, documenting recent progress on
#1316. Perhaps only Vitalije will have any great interest.
Problems subclassing from dict
Cleaning the classes
I spent several happy hours cleaning cruft from both classes, and in all the calls to same. In the process, I discovered the __repr__ pattern just described. This pattern alone was worth the work.
Besides the __repr__ pattern, the most important changes were:
- Using copy.deepcopy(self) to copy instances. The old way did the copy using d.d = dict(self.d). Unless I am mistaken, this does a shallow copy, and that may create subtle problems.
- Removing the replace alias for __setitem__ method. So now callers do d [key] = value, as is usual. This highlights that the add method is something truly new.
Merged, but work continues
I have merged the gs branch into devel, and from there to all other active branches. However, a wonderful simplification beckons: it might be possible to merge the two classes! I will be very happy if this does work. We'll see.
Indeed, modulo a few nits, the only real difference between the two classes is that the TypedDictOfLists contains an add method. This should be renamed, add_to_list.
What are these classes anyway?
The class's names advertise that they do type checking, but happily this is not their primary purpose!
Indeed, these classes just add Leo-specific methods to a plain dict. The new methods are:
- copy: a thin wrapper for copy.deepcopy,
- add: a convenience for adding an object to a list,
- get_setting and get__string_setting, which do Leo's setting munging on dict keys.
Aha! Only the add method might cause problems if the two classes were merged!
Summary
This project has already been worth the effort. I shall be very happy if the two classes can be merged. That's next on the list.
Edward