This is an Engineering Notebook post, documenting recent progress on #1316 
<https://github.com/leo-editor/leo-editor/issues/1316>.  Perhaps only 
Vitalije will have any great interest.

*Problems subclassing from dict*

It turns out that subclassing from dict is far from straightforward 
<https://stackoverflow.com/questions/3387691/how-to-perfectly-override-a-dict>. 
Yes, it's possible to use collections.MutableMapping 
<https://docs.python.org/3/library/collections.abc.html#collections.abc.MutableMapping>.
  
I wasn't confident that I could do that, mainly because I wasn't entirely 
clear what these two classes actually do!

*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

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/a4f299b4-0574-4d79-8965-1a77f110e622%40googlegroups.com.

Reply via email to