How can I create a dict that sets a flag if it's been modified

S

sandravandale

I can think of several messy ways of making a dict that sets a flag if
it's been altered, but I have a hunch that experienced python
programmers would probably have an easier (well maybe more Pythonic)
way of doing this.

It's important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don't think you can tell the difference between a read and a
write without making some sort of wrapper around them.

Still, I'd love to hear how you guys would do it.

Thanks,
-Sandra
 
A

Amit Khemka

Ideally, I would have made a wrapper to add/delete/modify/read from
the dictionay.

But other than this, one way i can think straight off is to "pickle"
the dict, and to see if the picked object is same as current object.

cheers,
amit.

I can think of several messy ways of making a dict that sets a flag if
it's been altered, but I have a hunch that experienced python
programmers would probably have an easier (well maybe more Pythonic)
way of doing this.

It's important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don't think you can tell the difference between a read and a
write without making some sort of wrapper around them.

Still, I'd love to hear how you guys would do it.

Thanks,
-Sandra


--
 
P

Paul Rubin

Still, I'd love to hear how you guys would do it.

Make a subclass of dict, or an object containing a dictionary, that
has a special __setattr__ method that traps updates and sets that
modification flag. There are contorted ways the caller can avoid
triggering the flag, but Python is not Java and it in general makes no
attempt to protect its objects against hostile code inside the
application.

Your suggestion of using pickle is shaky in my opinion, since pickle's
docs don't guarantee that pickling the same dictionary twice will
return the same pickle both times. If it happens to work that way,
it's just an implementation accident.
 
B

Brian van den Broek

(e-mail address removed) said unto the world upon 12/01/06 03:15 AM:
I can think of several messy ways of making a dict that sets a flag if
it's been altered, but I have a hunch that experienced python
programmers would probably have an easier (well maybe more Pythonic)
way of doing this.

It's important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don't think you can tell the difference between a read and a
write without making some sort of wrapper around them.

Still, I'd love to hear how you guys would do it.

Thanks,
-Sandra

Hi Sandra,

here's one attempt. (I'm no expert, so wait for better :)
def __init__(self, *args, **kwargs):
super(ModFlagDict, self).__init__(*args, **kwargs)
self.modified = False
def __setitem__(self, key, value):
self.modified = True
super(ModFlagDict, self).__setitem__(key, value)

>>> md = ModFlagDict(a=4, b=5)
>>> md {'a': 4, 'b': 5}
>>> md.modified False
>>> md[3]=5
>>> md {'a': 4, 3: 5, 'b': 5}
>>> md.modified True
>>>

It's broken in at least one way:

So, as it stands, no integers, floats, tuples, etc can be keys on
initialization. I think that can be be worked around by catching the
exceptions and setting the desired key-value pairs that way. But, it
is almost 4am, and I also suspect there is a much better way I am not
thinking of :)

Best,

Brian vdB
 
B

Brian van den Broek

Brian van den Broek said unto the world upon 12/01/06 03:42 AM:
(e-mail address removed) said unto the world upon 12/01/06 03:15 AM:

here's one attempt. (I'm no expert, so wait for better :)

def __init__(self, *args, **kwargs):
super(ModFlagDict, self).__init__(*args, **kwargs)
self.modified = False
def __setitem__(self, key, value):
self.modified = True
super(ModFlagDict, self).__setitem__(key, value)

It's broken in at least one way:


So, as it stands, no integers, floats, tuples, etc can be keys on
initialization. I think that can be be worked around by catching the
exceptions and setting the desired key-value pairs that way. But, it
is almost 4am, and I also suspect there is a much better way I am not
thinking of :)

Sorry for the self-reply, but I just realized my original code isn't
quite so bad as:

# class ModFlagDict as before
>>> mdict = ModFlagDict({42:"This will work", (7, 6):"Python comes through, again!"})
>>> mdict {42: 'This will work', (7, 6): 'Python comes through, again!'}
>>> mdict.modified False
>>> mdict[42]=":)"
>>> mdict {42: ':)', (7, 6): 'Python comes through, again!'}
>>> mdict.modified True
>>>

I'll wager someone will point out a better way still, though.

Best,

Brian vdB
 
T

Tim N. van der Leeuw

Should the dict flag when the dict itself has been updated? Or also
when any of the items in the dict has been updated?

Say you have a dict consisting of lists...

The dict should be flagged as modified when an item is added; or when
an item is replaced (you call dict.__setitem__ with a key that already
exists).
This is clear, and easy to achieve with a simple wrapper or subclass.

But should the dict also be flagged as 'modified' when I append an item
to one of the lists that is in the dict?
Does that code mean that the dict should be flagged as 'modified' in
your use-case? or not?

If yes, then the only feasible way might be to pickle the dict. And if
repeated pickling of the same dict is not guaranteed to give the same
results, then perhaps pickling d.getitems() would give the right
results since, AFAIK, dict.getitems() is at least guaranteed to
maintain the same order given that A) The dict is not changed and B)
You're using the same Python version.

Right?
 
D

Duncan Booth

It's important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don't think you can tell the difference between a read and a
write without making some sort of wrapper around them.
Detecting when a dictionary is updated is easy, just subclass dict and
override the __setitem__ method.

Detecting when an object contained within the dictionary is mutated is
effectively impossible.

If you are willing to compromise you can do something similar to the
solution used in the ZODB (persistent object database): it marks an object
as dirty when you rebind an attribute, but it doesn't attempt to catch
mutation of contained objects. If you want a contained dictionary or list
to be handled automatically by the persistence machinery you can use a
PersistentDict or PersistentList object, otherwise you can use an ordinary
dict/list and manually flag the container as dirty when you mutate it.

This leads to code such as (assuming self is the container):

....
self.somelist.append(something)
self.somelist = self.somelist

which isn't wonderful but mostly works. Of course:

self.somelist += [something]

also has the desired effect.
 
D

Dan Sommers

It's broken in at least one way:
So, as it stands, no integers, floats, tuples, etc can be keys on
initialization ...

That has nothing to do with your code:
SyntaxError: keyword can't be an expression

The names of keyword arguments have look like Python identifiers; 1 and
4 are *not* valid Python identifiers.

Regards,
Dan
 
S

Steve Holden

Paul said:
Make a subclass of dict, or an object containing a dictionary, that
has a special __setattr__ method that traps updates and sets that

/__setattr__/__setitem__/ ?

regards
Steve
 
M

Mike Meyer

here's one attempt. (I'm no expert, so wait for better :)
You also need to catch __delitem__.

You may need to catch pop, update, clear and setdefault, depending on
whether or not they call the __setitem__/__delitem__. Personally, I'd
catch them all and make sure the flag got set appropriately, because
whether or not they use those methods is an implementation detail that
may change in the future.

<mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top