BlockIt
|
00001 #............................................................................ 00002 # Copyright (c) 2009,2010,2011 David Car, david.car7@gmail.com 00003 # 00004 # This program is free software; you can redistribute it and/or modify it under 00005 # the terms of the GNU General Public License as published by the Free Software 00006 # Foundation; either version 2 of the License, or (at your option) any later 00007 # version. 00008 # 00009 # This program is distributed in the hope that it will be useful, but WITHOUT 00010 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 00012 # 00013 # You should have received a copy of the GNU General Public License along with 00014 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00015 # Place, Suite 330, Boston, MA 02111-1307 USA 00016 #.......................................................................... 00017 __all__ = ['ASet'] 00018 00019 import copy 00020 00021 class ASet(set): 00022 '''A set class that simply has a new "changed" attribute to indicate 00023 whether it has been updated or added to since last check. 00024 00025 ''' 00026 def __init__(self, *args, **kwargs): 00027 super(ASet, self).__init__(*args, **kwargs) 00028 self._changed = False 00029 self._changeSet = set() 00030 00031 def update(self, *args, **kwargs): 00032 '''Wrapper for the set update() method with changed setting. 00033 00034 ''' 00035 l = len(self) 00036 super(ASet, self).update(*args, **kwargs) 00037 if l != len(self): 00038 self._changeSet.update(*args, **kwargs) 00039 self._changed = True 00040 00041 def add(self, *args, **kwargs): 00042 '''Wrapper for the set add() method with changed setting. 00043 00044 ''' 00045 l = len(self) 00046 super(ASet, self).add(*args, **kwargs) 00047 if l != len(self): 00048 self._changeSet.add(*args, **kwargs) 00049 self._changed = True 00050 00051 def changeSet(self): 00052 '''Return a copy of only the changes since last reset. 00053 00054 ''' 00055 return copy.copy(self._changeSet) 00056 00057 def changed(self): 00058 return self._changed 00059 00060 def reset(self): 00061 '''Reset the changed attribute. 00062 00063 ''' 00064 self._changed = False 00065 self._changeSet.clear() 00066