On 2007-01-25, antred <[EMAIL PROTECTED]> wrote: > While working on a program I encountered a situation where I'd > construct a largish data structure (a tree) from parsing a host > of files and would end up having to throw away parts of my > newly built tree if a file turned out to contain invalid data.
The first idea that occurs to me is to provide a merge function for your data structure, which you use to merge in another tree object when that data is known to be valid. So the process would work like this: temp_tree = process(the_file) if temp_tree.is_valid(): real_tree.merge(temp_tree) ><CODE SNIPPET> > > u = Unrollable() > u.someValue = 3.14 > u.aString = 'Hi there' > > # If we decide we want to keep those changes ... > u.commit() > > # Or we could have reverted to the original. This would have restored > the state prior to the last call to commit() (or simply the > state at the beginning, if there hasn't been a call to commit > yet). > #u.rollback() > ></CODE SNIPPET> > > The basic idea behind this is that each instance of the > Unrollable class keeps an internal dictionary (which, in lieu > of a better name I'm currently calling 'sand box') to which all > changed attribute values are saved (attribute changes are > intercepted via __setattr__). Then, with a call to commit(), > all attributes are transferred to the instance's __dict__ > dictionary and hence become actual attributes per se. A nice use for this class might be to pass large mutable objects to a functions as if it were immutable without needing to copy them. Unfortunately it only works for one level of call. I think. -- Neil Cerutti -- Posted via a free Usenet account from http://www.teranews.com -- http://mail.python.org/mailman/listinfo/python-list