cleverdict
cleverdict copied to clipboard
Autosave not triggered with list.extend() / list.append()
When a CleverDict attribute or value is changed using "=" assignment it triggers the .save()
method as intended.
list.append()
and list.extend()
do NOT trigger the autosave function however... not sure why, and I don't have any understanding of the internals, but just wondered if there it might be desirable/possible to specify a list of methods (outside of the CleverDict class) which, if called, would also trigger a mapped CleverDict method... mainly .save
and .delete
but could be other things as well.
That might be overly complicated, or it might provide another rich layer of cleverness to CleverDict. Please let me know your initial thoughts?
I see that you have closed this issue, but I think it is not.
If I understand you well, you like to call the save method when the value in a CleverDict is updated with extend or append. Well, that is not possible at all as the CleverDict object is not aware of the change of a mutable object.
The following program demonstrates that:
from cleverdict import CleverDict
class MyDict(CleverDict): def save(self, k, v): print(f'save[{k}] = {v}') def init(self, *args, **kwargs): super().init(**kwargs)
a = MyDict() a[3]=4 l = [2,3] a[1]= l l.extend([4,5]) # this does not call the save method! print(a)
I don't know what you mean when you say .extend works fine. But certainly not in this example. Maybe you have to explain what you did accomplish with respct to .extend and save.
Parked for now - it may even be an advantage to have .append() method able to bypass setting behaviour e.g. to avoid recursion issues in README example:
class AutoStore(CleverDict):
def __init__(self, *args, **kwargs):
self.setattr_direct('index', [])
super().__init__(*args, **kwargs)
def save(self, name, value):
""" Keep a separate 'store' for data in .index """
self.index.append((name, value))
Labelled as "wontfix" after Version 1.8.0 simply because the main authors don't currently have the time for non essential enhancements. If there's a huge uptake in people using CleverDict and asking for this feature that may change, or if you're reading this why not Fork, develop a solution, and contribute a Pull Request yourself?