[Tutor] checksum of dictionary

Brad Reisfeld brad.reisfeld@colostate.edu
Wed Feb 12 13:45:03 2003


Hi Drew,
Thanks for the feedback.
Actually, the dictionary in question is already a subclass of dict.
It seems as if using setitem/delitem might be overkill for this since all I
really care about is a gross state change in the dictionary between
'expensive' calls to my function.

In other words, my scenario is
1. create large dict
2. call function on dict
3. perhaps add elements to dict
4. perhaps add elements to dict
5. call function on dict (only if dict is not the same as in step 1).
6. ...

I thought that marshaling the dict would be fairly inexpensive (I could be
wrong); that was the basis for my checksum idea.

-Brad

> -----Original Message-----
> From: Drew Perttula [mailto:drewp@bigasterisk.com]
> Sent: Wednesday, February 12, 2003 10:30 AM
> To: Brad Reisfeld
> Cc: tutor@python.org
> Subject: Re: [Tutor] checksum of dictionary
>
>
>
> > Hi,
> > I have a time-consuming function that operates on a large
> dictionary. Before
> > I call this function, I'd like to confirm that the dictionary
> has changed
> > since the last time I called the function.
> > To check for a change, I am thinking of calculating a checksum on the
> > dictionary.
>
> Nowadays you can subclass the dictionary to make one that notices any
> changes. If you're purely concerned about setitem and delitem calls, you
> could just override those. If you want to catch update(), etc, you might
> like http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117236, which
> is a dict interface defined in terms of getitem/setitem/delitem/keys. It's
> great for constructing custom dict-like objects that use custom code
> for all 20-or-so of the standard dict methods.
>
> Obviously, watching dict accesses would be tremendously more efficient
> than walking the whole dict every time you need to see if it changed.
>
> -Drew
>