473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2040
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: bearophileHUGS | last post by:
I'm frequently using Py2.4 sets, I find them quite useful, and I like them, even if they seem a little slower than dicts. Sets also need the same memory of dicts (can they be made to use less...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
11
by: mwt | last post by:
Hi. I'm reworking a little app I wrote, in order to separate the data from the UI. As a start, I wanted to create a iron-clad data recepticle that will hold all the important values, and stand up...
3
by: Gregory Piñero | last post by:
Hey guys, I don't understand why this isn't working for me. I'd like to be able to do this. Is there another short alternative to get this intersection? >>> set().intersection() Traceback...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
3
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
1
by: bearophileHUGS | last post by:
The PEP 3100: http://www.python.org/dev/peps/pep-3100/ says: Return iterators instead of lists where appropriate for atomic type methods (e.g. dict.keys(), dict.values(), dict.items(), etc.);...
6
by: kaens | last post by:
Hey everyone, this may be a stupid question, but I noticed the following and as I'm pretty new to using xml and python, I was wondering if I could get an explanation. Let's say I write a simple...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.