471,601 Members | 1,203 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,601 software developers and data experts.

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

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

Jan 12 '06 #1
11 1879
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.

On 12 Jan 2006 01:15:38 -0800, sa***********@yahoo.com
<sa***********@yahoo.com> wrote:
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

--
http://mail.python.org/mailman/listinfo/python-list

--
----
Endless the world's turn, endless the sun's spinning
Endless the quest;
I turn again, back to my own beginning,
And here, find rest.
Jan 12 '06 #2
sa***********@yahoo.com writes:
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.
Jan 12 '06 #3
sa***********@yahoo.com 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 :-)
class ModFlagDict(dict): 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:
newmd = ModFlagDict(3=4, 1=5) SyntaxError: keyword can't be an expression


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
Jan 12 '06 #4
Brian van den Broek said unto the world upon 12/01/06 03:42 AM:
sa***********@yahoo.com 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.
<snip>
here's one attempt. (I'm no expert, so wait for better :-)
>>> class ModFlagDict(dict): 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)
<snip>
It's broken in at least one way:
>>> newmd = ModFlagDict(3=4, 1=5) SyntaxError: keyword can't be an expression >>>
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
Jan 12 '06 #5
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?
l = d['a']
l.append('1')

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?

Jan 12 '06 #6
sa***********@yahoo.com wrote:
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.
Jan 12 '06 #7
On Thu, 12 Jan 2006 03:42:21 -0600,
Brian van den Broek <br***@cc.umanitoba.ca> wrote:
It's broken in at least one way:
newmd = ModFlagDict(3=4, 1=5) SyntaxError: keyword can't be an expression So, as it stands, no integers, floats, tuples, etc can be keys on
initialization ...


That has nothing to do with your code:
dict(1=4) SyntaxError: keyword can't be an expression int(4=5)

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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jan 12 '06 #8
Brian van den Broek <br***@cc.umanitoba.ca> writes:
It's broken in at least one way:
>>> newmd = ModFlagDict(3=4, 1=5)

SyntaxError: keyword can't be an expression


newmd = ModFlagDict(**{3:4, 1:5})
Jan 12 '06 #9
Paul Rubin wrote:
sa***********@yahoo.com writes:
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


/__setattr__/__setitem__/ ?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Jan 12 '06 #10
Steve Holden <st***@holdenweb.com> writes:
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__/ ?


Yes, thinkographical error. Thanks.
Jan 12 '06 #11
>> here's one attempt. (I'm no expert, so wait for better :-)
>>> class ModFlagDict(dict):

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)


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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jan 12 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by bearophileHUGS | last post: by
11 posts views Thread by mwt | last post: by
3 posts views Thread by Gregory Piñero | last post: by
37 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by Steven Bethard | last post: by
1 post views Thread by bearophileHUGS | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.