Class update detection
Miki Tebeka
miki.tebeka at zoran.com
Tue May 11 03:26:36 EDT 2004
Hello Steven,
> I need to determine if
> any of the class attributes have been changed to I can issue an update
> query to the database with the changes. Is there a generally accepted
> method to do this? Is there some way to create a checksum of the class
> attributes?
Do you mean something in the lines of:
class C:
def __init__(self, a, b):
self.a, self.b = a, b
self.dirty = 0
def __setattr__(self, k, v):
if k in ("a", "b"):
self.dirty = 1
self.__dict__[k] = v
>> c = C(1,2)
>>> c.dirty
0
>>> c.a = 10
>>> c.a
10
>>> c.dirty
1
>>>
Another option is to generate a checksum (using md5 and str) on all the
attributes you're interested in at the end of __init__ and a function
`dirty' will compute this checksum and return 1 if it was changed.
Note that if you have nested objects this might be a problem.
HTH.
Miki
More information about the Python-list
mailing list