Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'attribute': 0.05; 'method.': 0.05; 'append': 0.07; 'attributes': 0.07; 'decorator': 0.07; 'dict': 0.09; 'key)': 0.09; 'def': 0.10; 'yet.': 0.13; 'modification': 0.15; 'stack': 0.15; '"""helper': 0.16; '"""returns': 0.16; 'attribute,': 0.16; 'cleaner': 0.16; 'collects': 0.16; 'dict(': 0.16; 'goal,': 0.16; 'marker': 0.16; 'received:10.50': 0.16; 'subject:object': 0.16; 'subject:when': 0.16; 'time."""': 0.16; 'values."""': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; 'changes': 0.20; 'trying': 0.21; 'flags': 0.22; 'simpler': 0.22; "haven't": 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'bugs': 0.27; 'necessary.': 0.27; 'opposed': 0.27; 'key,': 0.29; 'leaves': 0.29; 'value)': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'maybe': 0.29; 'code': 0.31; "skip:' 20": 0.32; 'could': 0.32; 'comments': 0.33; 'skip:s 30': 0.33; 'cleaning': 0.33; 'values.': 0.33; 'to:addr :python-list': 0.33; 'hi,': 0.33; 'list': 0.35; 'along': 0.35; 'ben': 0.35; 'pm,': 0.35; "won't": 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'should': 0.36; 'previous': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'object': 0.38; 'skip:o 20': 0.38; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'first': 0.61; 'more': 0.63; 'believe': 0.69; 'eyes': 0.69; 'restore': 0.69; 'further,': 0.71; 'special': 0.73; 'subject:its': 0.84; 'dirty': 0.91 X-Virus-Scanned: Debian amavisd-new at ispconfig-mx01-ha03.globe.de Date: Thu, 07 Mar 2013 13:21:00 +0100 From: Schneider User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Creating an object that can track when its attributes are modified References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362659341 news.xs4all.nl 6984 [2001:888:2000:d::a6]:47638 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40758 Hi, maybe you could do this by a decorator on the setattr method. It should look more or less like your implementation, but in my eyes it's a cleaner and can be reused. Further, I would use a stack for each attribute, so that you can restore all previous values. bg, Johannes On 03/06/2013 05:07 PM, Ben Sizer wrote: > I am trying to make an object that can track when its attributes have been assigned new values, and which can rollback to previous values where necessary. I have the following code which I believe works, but would like to know if there are simpler ways to achieve this goal, or if there are any bugs I haven't seen yet. > > > class ChangeTrackingObject(object): > def __init__(self): > self.clean() > > def clean(self): > """Mark all attributes as unmodified.""" > object.__setattr__(self, '_dirty_attributes', dict()) > > def dirty_vals(self): > """Returns all dirty values.""" > return dict( [ (k,v) for k,v in self.__dict__.iteritems() if k in self._dirty_attributes] ) > > def get_changes_and_clean(self): > """Helper that collects all the changes and returns them, cleaning the dirty flags at the same time.""" > changes = self.dirty_vals() > self.clean() > return changes > > def rollback(self): > """Reset attributes to their previous values.""" > for k,v in self._dirty_attributes.iteritems(): > object.__setattr__(self, k, v) > self.clean() > > def __setattr__(self, key, value): > # If the first modification to this attribute, store the old value > if key not in self._dirty_attributes: > if key in self.__dict__: > self._dirty_attributes[key] = object.__getattribute__(self, key) > else: > self._dirty_attributes[key] = None > # Set the new value > object.__setattr__(self, key, value) > > > I am aware that adding a new attribute and then calling rollback() leaves the new attribute in place with a None value - maybe I can use a special DeleteMe marker object in the _dirty_attributes dict along with a loop that calls delattr on any attribute that has that value after a rollback. > > I also believe that this won't catch modification to existing attributes as opposed to assignments: eg. if one of the attributes is a list and I append to it, this system won't notice. Is that something I can rectify easily? > > Any other comments or suggestions? > > Thanks,