|
|
|
YADI, part 1: Memento PatternYADI ?"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 ASPN, (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413838) 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:
begin_transaction()
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 instance. 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 decoratorThe decorator transaction could be:
import copy
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, descriptors, etc..). Anyway, it's pretty usefull on most classes, and they can implement the __deepcopy__ method to control the copy behavior. Example of useThe example below implements a run() method, that becomes transactionnal
class M(object):
When run() fails, because a + 1 (a is a string) leads to a TypeError, the state of M is automatically rolled back. Important announcement: Join the Nuxeo team and contribute to the Nuxeo project! We have open positions in France and the UK for open source Java EE developers and sales engineers, both junior and senior. Trackback PingsTrackback URL for this entry:
http://blogs.nuxeo.com/sections/blogs/tarek_ziade/2005_09_23_yadi-part-1-memento-pattern/tbping
Posted by Tarek Ziadé @ 09/23/2005 02:16 PM.
-
Categories:
python
-
0 comments
|
Nuxeo Bloggers: Log in! Search Nuxeo Blogs
About this blog
Tarek Ziadé Nuxeo Bloggers
Photos and Pictures
|
|
Nuxeo -
Indesko -
Nuxeo 5 Project
All content is copyrighted by their author. CPSSkins is Copyright © 2003-2006 by Jean-Marc Orliaguet. | CPS is Copyright © 2002-2006 by Nuxeo SAS. |