class Snapshottable(object):
"""When mixed into an object, provides a way to snapshot the value of some
or all alltributes into a log. The log is only created if at least one snapshot is taken."""
def snapshot(self, attributes=None, note=None):
"""Record the str() values of all attributes in the
log (or the contents of the object's __dict__ if
attributes is None. If note is passed, prepend to
the log entry."""
if not attributes:
attributes = [x for x in self.__dict__.keys() if not x.startswith('__')]
if note:
logentry=note+'\n'
else:
logentry=''
for a in attributes:
if hasattr(self, a):
logentry += "%s=%s\n" % (a,getattr(self,a))
try:
self.log.append(logentry)
except AttributeError:
self.log = [logentry]
def getlog(self):
try:
return self.log
except AttributeError:
self.log = []
return self.log
def clearlog(self):
self.log = []