"YADI" stands for Yet Another Design pattern Implementation, because
GoF design patterns have been reviewed and implemented a bunch of people
already, all over the web. I'd like to present my own implementation for
some of them, though, in this blog.
This first post is about Memento, inspired from Zoran Isailovski's one on
but with a slightly different approach to simplify its use.
Memento: What for ?
Memento DP says: a state of the program data can be saved and reloaded,
thus allowing to make safe transactionnal operations. In other words, some
program data changes can be rolled back at some point, or commited.
The code pattern that describes it the best is:
Depending on what the program does, the scope of data that are to be saved
can vary a lot. Transactions in RDBMS covers the whole database for
The easiest way to implement Memento in Python is to set the transactional
level to objects.
And the easiest way to implement this is to create a decorator that allow
some methods of an object to become transactionnal.
The transaction decorator
The decorator transaction could be:
""" get the object state """
def set_memento(object, state):
""" restore the objet """
""" decorator """
def bind(object, args, **kw):
state = get_memento(object)
return method(object, args, **kw)
This implementation is based on the copy module, that works with the
dict atribute of the object, thus it won't work if the object is a
new-style class that has dropped the use of dict in some ways (slots,
Anyway, it's pretty usefull on most classes, and they can implement the
deepcopy method to control the copy behavior.
Example of use
The example below implements a run() method, that becomes
self.a = 12
self.b = ['a', 32]
self.l = o
self.o = 12
self.a = '14'
self.a += 1
objet = M()
[[email protected] Desktop]$ python memento.py
When run() fails, because a + 1 (a is a string) leads to a TypeError,
the state of M is automatically rolled back.
(Post originally written by Tarek Ziadé on the old Nuxeo blogs.)