I\'m trying to make an object act like a built-in list
, except that its value be saved once modified.
The implementation I come up with is wrapping a
Here is a way to avoid having to decorate every list method. It makes PersistentList a context manager, so you can use the
with PersistentList('key', db) as persistent:
do_stuff()
syntax. Admittedly, this does not cause the _save
method to be called after each list operation, only when you exit the with-block
. But I think it gives you enough control to save when you want to save, especially since the __exit__
method is guaranteed to be executed no matter how you leave the with-block
, including if it happens because of an exception.
You might be an advantage that _save
is not called after every list operation. Imagine appending to the list 10,000 times. So many individual calls to db.set
(a database?) could be quite time-consuming. I would be better, at least from a performance point of view, to make all the appends and the save once.
class PersistentList(list):
def __init__(self, key, db):
self.key = key
self.extend(db.get(key, []))
def _save(self):
# db.set(self.key, self)
print('saving {x}'.format(x = self))
def __enter__(self):
return self
def __exit__(self,ext_type,exc_value,traceback):
self._save()
db = {}
p = PersistentList('key', db)
with p:
p.append(1)
p.append(2)
with p:
p.pop()
p += [1,2,3]
# saving [1, 2]
# saving [1, 1, 2, 3]